• 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
    1006495

Grammar tweaking for fun and profit

Sign in to follow this  
Followers 0
ApochPiQ

350 views

I finally killed off most of the remaining dynamic memory allocations in my parser. There's still a few lurking in the internals of boost::spirit::lex (it supports "fast" parser backtracking by buffering tokens on the fly) but they're not a huge chunk of the runtime anymore, so I'm not terribly worried.

It's time to switch over to more mundane things, like algorithmic improvements to the grammar spec itself. I'm planning on writing up a full list of tricks I'm using once I finish, so I won't delve too far into the tweaks I've been doing just yet. In brief, though, I'm basically tuning the rules so that productions fail as quickly as possible and don't test for things that are rare unless they absolutely have to.

For instance, it's common to have "optional" stuff in a given piece of Epoch syntax. One big killer is "preoperation" and "postoperation" statements, where postoperations are the real problem.

Consider the Epoch statement below:
foo.deeply.nested.bar++

To match this, we have to first see if we have an identifier (check) followed by a member access operator, the dot (check) followed by any repeated number of such accesses (check) followed by a postfix operator (check).

Now consider this alternative Epoch statement:
foo.deeply.nested.bar.function()

So we have an identifier (check) followed by a dot (check) followed by some repeats (check) followed by a postfix opera--- oops! That's an open parenthesis! Now the parser has to backtrack through all those tokens, discard any work it tried to do in the meantime, and try again from scratch. When the function call rule is tried, it has to re-match all of the member accesses, check again to see if it has a parenthesis (which we should have already known) and then continue.

Needless to say, for a production which appears in the majority of a given program's code, this is painfully inefficient.

Another killer is unary prefixes:
foo = !bar

The rule for this is pretty simple, at least in its naive form:
expression = prefixes* (parenthetical | literal | statement | identifier)

The rule's execution goes something like this:
  • Look ahead by a token
  • Ok, we have a prefix operator, the !
  • Consume the ! and look ahead another token
  • Check the prefix operator list again, this time failing because baz is not a prefix operator
  • Backtrack 1 token
  • Successfully match the Kleene star since we found 1 prefix operator
  • Look ahead 1 token
  • Is it a parenthetical? Nope.
  • How about a literal? Nope.
  • A statement? Well, it begins with an identifier!
  • Look ahead 1 token
  • Is it a parentheses-enclosed list of parameters?
  • Nope. Backtrack 1 token
  • Look ahead 1 token
  • Oh, it's a standalone identifier
  • Successfully match the identifier
  • Successfully match the RHS expression
  • Successfully match the assignment
    Throw in support for multiple assignment in a single statement:
    a = b = c = foo()

    Now we have to repeat all that backtracking and peeking ahead for every single assignment in the chain. Ouch!

    Let's change the rule up a bit.
    expressionchunk = parenthetical | literal | statement | identifier
    expression = expressionchunk | (prefixes+ expressionchunk)


    Now the rule can early-out when it finds an expression that doesn't involve a prefix. In the worst case, if the expression isn't really a valid expression, it looks at the "prefixes+" subrule (which matches one or more prefixes) and can quickly fail there without having to redundantly check for a valid trailing expression chunk.


    This kind of subtle but vital tweaking is pretty much the last bit of fertile ground for optimizing the Epoch parser at this point. I'm still hunting down improvements, and they're getting solid gains - but it's tediously slow work, because in order to test an individual tweak I have to rebuild the Epoch compiler (and even one subset of that can take a couple of minutes to compile), make sure all the different syntactically valid Epoch constructs are still accepted, make sure that illegal constructs are not accepted, and so on.

    "Diminishing returns" is the name of the game now. All the easy, obvious stuff was optimized long ago, and much of it was outright deleted. Memory allocations are down to a tiny fragment of what they used to be, and only really account for about 15% of the execution time, so it's not like we can gain some major wins by further culling allocations.


    Work is ongoing, but current parse times on the original test case are just over 10ms. That's just a hair over a second to parse the 2MB test file.

    Win.

0
Sign in to follow this  
Followers 0


1 Comment


Fun stuff. When writing my LALR grammars, all the tweaking gets done up front, otherwise you get shift-reduce and reduce-reduce errors and won't be able to run the parser at all. The nice benefit of that is that once you've got it all worked out, it's pretty much optimal without any other tweaking.
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