Discussion:
lua_checkstack necessity
Tyson Nottingham
2014-10-23 18:23:09 UTC
Permalink
I've noticed that many LuaJIT C API functions manage the stack size
without requiring me to use lua_checkstack(). E.g. I can call
lua_pushnumber() up to around LUAI_MAXSTACK times in a loop without
any apparent memory issues. In the source, I can see that many
functions call incr_top(), which grows the stack according to my
understanding.

Does this make calling lua_checkstack() unnecessary before calling the
basic functions that push elements onto the stack (lua_pushxxx(),
lua_next(), etc.) when using the LuaJIT implementation? Are there any
exceptions? Is this implicit stack management something that I can
rely on, or should I consider it an implementation detail that may
change? Also, is this documented anywhere?

Thanks,
Tyson
Daurnimator
2014-10-23 18:29:15 UTC
Permalink
Post by Tyson Nottingham
I've noticed that many LuaJIT C API functions manage the stack size
without requiring me to use lua_checkstack(). E.g. I can call
lua_pushnumber() up to around LUAI_MAXSTACK times in a loop without
any apparent memory issues. In the source, I can see that many
functions call incr_top(), which grows the stack according to my
understanding.
Does this make calling lua_checkstack() unnecessary before calling the
basic functions that push elements onto the stack (lua_pushxxx(),
lua_next(), etc.) when using the LuaJIT implementation? Are there any
exceptions? Is this implicit stack management something that I can
rely on, or should I consider it an implementation detail that may
change? Also, is this documented anywhere?
Thanks,
Tyson
From http://www.lua.org/manual/5.2/manual.html#4.2:

Whenever Lua calls C, it ensures that the stack has at least LUA_MINSTACK
extra slots.
LUA_MINSTACK is defined as 20, so that usually you do not have to worry
about stack space
unless your code has loops pushing elements onto the stack.
Tyson Nottingham
2014-10-23 19:11:50 UTC
Permalink
Thanks. I'm wondering specifically about LuaJIT -- does it make any
additional guarantees about the stack size, beyond Lua's LUA_MINSTACK
guarantee you mentioned? The LuaJIT source suggests that
lua_checkstack calls are unnecessary in many/most cases according to
my understanding. I'm just wondering if this is something that I can
rely on when using LuaJIT going forward. I.e. is this something that
LuaJIT guarantees or is highly unlikely to change in the
implementation in the near future?
Post by Daurnimator
Post by Tyson Nottingham
I've noticed that many LuaJIT C API functions manage the stack size
without requiring me to use lua_checkstack(). E.g. I can call
lua_pushnumber() up to around LUAI_MAXSTACK times in a loop without
any apparent memory issues. In the source, I can see that many
functions call incr_top(), which grows the stack according to my
understanding.
Does this make calling lua_checkstack() unnecessary before calling the
basic functions that push elements onto the stack (lua_pushxxx(),
lua_next(), etc.) when using the LuaJIT implementation? Are there any
exceptions? Is this implicit stack management something that I can
rely on, or should I consider it an implementation detail that may
change? Also, is this documented anywhere?
Thanks,
Tyson
Whenever Lua calls C, it ensures that the stack has at least LUA_MINSTACK
extra slots.
LUA_MINSTACK is defined as 20, so that usually you do not have to worry
about stack space
unless your code has loops pushing elements onto the stack.
Loading...