lua-users home
lua-l archive

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


On 02.09.2014 13:57, Roberto Ierusalimschy wrote:
   [...]

where if lua_pushx would return 1, the then possible source code

   return lua_pushx (L, xxx)

often times would be compiled to just:

   jump lua_pushx


So there would actually be a small technical benefit.

Seeing my compilers do this all the time, I was curious how
common it actually is and how much it helps. The lua_pushnumber()
is an ideal case for studying this.

Compilers that do tail call elimination as described are:

 All modern compilers on Linux/unix-amd64: GCC, Sun, Intel, CLang.
 GCC for Arm 32 bits.

Compilers that miss out this opportunity to emit smarter code:

 All I tried for 32 bits Intel including MS.
 GCC for Windows 64 bits.
 GCC for sparc.

And what does it save?

Test with a tiny function adding two numbers returning the sum.
It takes around 45 nsec on my box:

   return lua_pushnumber1(L,lua_tonumber(L,1)+lua_tonumber(L,2))

The 1-returning pushnumber used to allow for tail call elimination
reduces the runtime of this test by measly 1-2 nsec vs. the standard
prodedure. Just enough to see a small improvement.

The time of this simple test is mostly spent in lua_tonumber.

Other interesting point learned: This tiny function to add
two numbers in C is not slower than a Lua function returning
the sum of two numbers. I was always wondering if all those
lua_tothis(), lua_pushthat() weren't quite slow. I think
lua_tonumber() is slow but Lua functions pay that same price.


The idea of changing the return value of lua_pushfoo() is of course
anyway dead because pushstring already does something different.



If you really want to go to this level (which I think is pure
speculation), you should consider also the cost of all useless
"mov $1, %eax" inside those 'lua_pushinteger' not used in returns
(paid by all compilers) :-)

-- Roberto