lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

It was thus said that the Great Wesley Smith once stated:
> > It's not the same, though. Apart from having to reorder my logic to suit
> > the restrictions of the language, which is always a pain, there's a
> > performance hit in that shared state has to be accessed via upvalues
> > rather than locals, which are considerably more expensive --- not just
> > to access, but also to create individual closures for each state in your
> > state machine. Say you have a state-machine based parser for network
> > packets. You have two choices: construct and then discard potentially
> > hundreds of closures for every packet, or restructure the entire state
> > machine not to need shared state. One's slow, the other's inconvenient.
> Is that really true?  I'm not sure I buy it.  Why can't you use
> coroutines with locals and and infinite loop to implement your state
> machine?  I've done this in many situations and have never had the
> need for upvalues.

  Or just pass a table around to each state:

	result = state1({})

	function state1(info)
	  -- blah blah blah
	  return state2(info)

	function state2(info)
	  -- yada yada
	  return state3(info)

	function state3(info)
	  if info.blah then return "done" end
	  return state1(info)

  -spc (But I could be missing something ... )