lua-users home
lua-l archive

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


On 09/02/2014 04:15 PM, Dirk Zoller wrote:
On 09/02/2014 01:58 AM, Rena wrote:
More accurately, it should return the number of values it pushed,
which for most push functions will always be 1.

That would simplify returning a value slightly:

return lua_pushinteger(L, 42);

instead of:
lua_pushinteger(L, 42);
return 1;


Besides the small simplification for the writer of the code,
this would allow the C-compiler to do tail call optimization.

   lua_pushx (L, xxx);
   return 1;

compiles to

   call lua_pushx
   mov 1, register
   return

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.



anyway, the compiler must emit the ``movl $1, %register`` instruction, either for your
function calling lua_pushXXX, or for the lua-pushXXX functions themselves.
the tail call optimization **DOES** eleminate a procedure call, but does not eleminate
the `mov` instruction.

IMHO. since the lua_pushXXX functions are more likely to be used at non-tail positions.
you don't really gain much.

I would rather prefer ``lua_pushXXX(); return 1;``, not ``return lua_pushXXX();``.
bisides, for multiret functions, this won't help either.