• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    625
  • comments
    1446
  • views
    1006461

Deploying a new lexer

Sign in to follow this  
Followers 0
ApochPiQ

348 views

When I last left a status update here, I had the new Epoch compiler running on my test case in right around 18.5 milliseconds. For reference, when I started this compiler rewrite, parse times were around 10 seconds. Yes, seconds.

Obviously, a 500-fold speed improvement is nothing to sneeze at, and maybe normal people would have looked at this rocket-disguised-as-compiler and decided that, hey, you know, 18.5 milliseconds is good enough.


Well... I have never claimed to be normal.

18.5 milliseconds may be nice, but it isn't there yet. Profiling the compiler shows that a huge amount of time is spent in the parser doing "backtracking." (For reference, I had to move up to a 2MB file for testing, because the 20KB test module I started with doesn't even register on the profiler anymore.)

Backtracking is when a parser looks at a bit of code, says "hmm, I think I'm looking at a Foo" and then gets another byte down the road and goes "whoops! That wasn't a Foo, it's a Foe!" and has to discard all the work it did assuming it was looking at a Foo.

When you do this on a byte-for-byte basis, in a 2MB file, it's clear that things are not as efficient as they could be.

The correct solution to this is lexical analysis. A lexer is a tool which performs lexical analysis. In a nutshell, this is a way of taking the raw code and treating it as a sequence of atomic "tokens" instead of bytes. Now, the parser doesn't think in terms of "F" followed by "o" followed by "o-- whoops, I mean e." Instead, it sees "Foo" and "Foe" as indivisible and obviously different chunks.

During parsing, this dramatically cuts back on backtracking, because the parser can look at the code at a more coarse level. It's like cutting your input size down by a factor roughly equal to the average length of a token in your code - which, as it happens in my Epoch test, is about 6 bytes.


The Epoch compiler is built on top of boost::spirit::qi, which has a companion library boost::spirit::lex. As you can probably guess from the name, lex is a lexer generator. And since, as we've established, 18.5 milliseconds is just too darn slow, it's time to deploy lex.

As a matter of fact, I started this about 24 hours ago, and I've been dabbling in compiler tweaks ever since. It's slow going, because with all the template library magic that's in the Epoch compiler, the build times of that code are getting into the several-minute range. It's endlessly ironic that I'm sacrificing hours of cumulative compile time in the Epoch compiler to shave milliseconds off the runtime... but oh well.

There have been a lot of dumb brain-dead mistakes made in those hours, mainly because I'm splitting my attention between this project and half a dozen other things, so I'm constantly losing track of what I was thinking or doing.


At the moment, I have a hacked version of things running at long last - which is tremendously rewarding. The problem is, it doesn't actually support the Epoch syntax extension mechanism - so all those cool constructs like "if" and "do" and "function" that the standard library provides... don't work.

There seems to be a fundamental assumption someplace in lex that says that you shouldn't ever need to modify the lexing tables on the fly. This strikes me as painfully naive and limiting, so I'm doing my best to hack around it. So far it's slow going, but there is light on the horizon.


I'll poke at this a bit more tonight and then get some sleep and come at it strong tomorrow. Will keep everyone posted!

0
Sign in to follow this  
Followers 0


5 Comments


Doesn't boost::spirit do piles of template black magic to convert the grammar declarations to other stuff? If ::lex follows that same pattern (and it wouldn't be a huge surprise if it did) then no runtime modification would follow from that.
0

Share this comment


Link to comment
Actually, both spirit and lex have a hefty runtime component in the default use case; all the operator overloading gibberish is actually building the grammar/lexer rules at runtime.

From what I can tell, the main limitation that prevents lex from being mutated at runtime is that it doesn't allow you to specify new precedence orders for the patterns it matches. Specifically, if you have a default rule that matches any token, and then add a more-specific rule that looks for the exact token "foo", the rule added later will never trip because the default rule appears first in the precedence order. This makes it impossible to effectively extend the lexer while it runs.

I'm working on ways around this using custom directive operators for spirit, though; it's actually pretty fun [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]
0

Share this comment


Link to comment
Hmm, that makes sense. I've been reading your posts btw, even if I don't often have many comments.
0

Share this comment


Link to comment
I'm a little pissed off at the moment. Many hours of work and it looks like the lexer is actually slightly slower than running without it!

I'm still looking for obvious mistakes in my implementation that would contribute to this terribleness, but it's pretty disappointing, because it indicates that the implementation of lex isn't good enough to counteract the algorithmic inefficiencies of not lexing before parsing.

I'm hoping I just bungled a copy constructor or something...
0

Share this comment


Link to comment
Hello,

I am the author of lexertl, the lexer-generator library underneath boost::lex. Note that if you are building lexers at runtime and have a non-trivial lex spec, then your 18.5 milliseconds will probably be dwarfed by building the lexer, never mind parsing your file. It is possible to generate code instead to avoid this start-up cost, but that won't help you if you want to regenerate the lexer on the fly.

Regards,

Ben
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now