lua-users home
lua-l archive

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


Alex Queiroz wrote:
[...]
>      You only need tail calls for state machines.

Depends on your requirements --- tail calls are, for example, absolutely
not an option for any of my stuff because they're too expensive.

The expense is not the tail call itself, mind, but the upvalues needed
to share data between states:

For example:

local function statemachine()
	local i = 1
	local j = 2

	local state1
	local state2

	state1 = function() i = i + 1 return state2() end
	state2 = function() j = j + 1 end

	return state1()
end

...allocates at least four heap cells *every time* statemachine() is
called; don't forget, the state functions themselves are upvalues...

The only other option is to pass your shared data in and out of every
state. But that's incredibly messy and hard to write and it's *still*
slower than an honest goto.

-- 
┌─── 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