lua-users home
lua-l archive

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


Alex Queiroz wrote:
[...]
>      ...But I still don't think your problem is caused by the locals,
> because you need that state anyway, in one form or the other. Instead,
> the memory usage is caused by the state functions themselves.

Yeah, but if I use goto, I don't *have* any state functions!

e.g. the earlier example becomes:

local function statemachine()
	local i = 1
	local j = 2
	goto ::state1::

::state1::
	i = i + 1
	goto ::state2::

::state2::
	j = j + 1
	return
end

Now, all the state machine's state is stored on the stack. No upvalues!
No memory allocations anywhere! Admittedly, we now have a few wasted
goto instructions, but they're *really* cheap. (And omitting such is an
optimisation that could plausibly be done with Lua's compiler.)

However, I'll come clean: I haven't actually *done* any of this, which
means I haven't been able to measure anything, and you don't know
anything until you've measured it. In particular, I really ought to
measure Daurnimator's suggestion about passing state around as
parameters to the state functions. That means that the state functions
don't need upvalues and so won't generate garbage. It would be extremely
instructive to compare against a goto-based state machine implementation.

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out
│ how to use my telephone." --- Bjarne Stroustrup

Attachment: signature.asc
Description: OpenPGP digital signature