Re: Mutually recursive functions and local variables

  • From: Szabó Antal <szabo.antal.92@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 23 Jul 2015 12:29:03 +0200

2015-07-23 10:42 GMT+02:00 Geoff Leyland <geoff_leyland@xxxxxxxxxxx>:

Hi,

Is there a trick to writing a pair of mutually recursive functions such
that both functions end up being held in one-time-assigned local variables?

I assume you meant something like this:

local two

local function one(count)
count = count - 1
if count > 0 then
return two(count)
else
return count
end
end

two = function(count)
count = count - 1
if count > 0 then
return one(count)
else
return count
end
end

Calling any of the above functions with a large enough number results in a
tight 4-times unrolled loop on my machine, wich is pretty good (it can only
be improved by vector instructions, but LuaJIT doesn't generate such code).

means that (as I understand it) two cannot be proven constant, and so the
code generated is not as quick as it could be.

LuaJIT is a trace compiler, which means it doesn't have to prove the
variable constant for the whole program, just the current trace, in which
it is pretty obvious that it does not change.

(I haven’t actually done any checking in this case, I’m just interested
in whether there’s a simple answer).

I don't mean to be offensive, but it's literally just running (the correct
version of) your test program with "luajit -jdump foo.lua" and checking the
asm output, which can be done in about 20 seconds most, instead of waiting
for an answer on this list ;)

Other related posts: