[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: State machines and goto (Was: Next Version of Lua?)
- From: Robert Raschke <rtrlists@...>
- Date: Thu, 11 Jun 2009 14:12:37 +0100
On Thu, Jun 11, 2009 at 12:57 PM, David Given <firstname.lastname@example.org>
Robert Raschke wrote:
This means looking stuff up dynamically from the table, which gives you an order of magnitude performance hit. Table lookups are *slow*. Simply moving the program counter from one bytecode address to another is fast. Plus using table lookups means that (assuming I've understood you correctly), the handler for each state needs to be a function, which means you run into the problem of efficiently sharing data between the handlers.
I find using a "machine state" Lua table that you can pass around to be pretty ideal.
Yes, indeed. Each state is a function, state transitions are tail calls, and the machine state is passed around as a table. This may be slower than goto with implicit shared state, but not slow enough for me to care in what I'm doing. Especially, since I can read the code and understand it.
From your comments, I must assume you are running in an environment with time constraints short enough to make you need something faster. Just out of interest, what's the problem domain of your state machine?