lua-users home
lua-l archive

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



On 26-Dec-06, at 8:58 PM, Tom Bates wrote:

OK, here's my first contribution to a porting guide.
 
When porting to a limited resource environment, such as an embedded hardware platform, here’s something to watch out for.
 
The lexical parsing in the Lua core needs a sizable chunk of stack space for such language elements as function definitions. I’ve found that in this case, the function chunk, which presumably is responsible for parsing a chunk of Lua code, winds up calling the function body that allocates the largish structure FuncState on the stack. This function in turn calls chunk again, so if there’s a nested function definition (admittedly pretty complex Lua code), you’ll need another chunk of stack (pardon the pun). I bumped our shell task’s stack size from 4K to 8K to surmount this issue. (FuncState, on my platform, is 0x5D8 bytes in size.)

I suppose you're using lua 5.0.2 for some reason. In version 5.1.1, that struct is only 572 bytes (on x86, which also reports 1496 bytes for FuncState in 5.0.2, so I suppose the primitive datatypes are the same size.)

You can tune the size of a FuncState structure on both versions, by adjusting the defines of (LUAI_) MAXUPVALUES and MAXVARS. In 5.0.2, these are found in llimits.h and in 5.1.1 they're found in luaconf.h (and are prefixed with LUAI_).

MAXUPVALUES is the maximum number of upvalues in a single function, and is set by default to 32 in v5.0.2 and 60 in 5.1.1; it is probably also the biggest contributor to the size of a FuncState in 5.0.2, about 20 bytes per entry. In 5.1.1, it's only two bytes per entry. MAXVARS is the maximum number of locals and temporaries in any single function. I would think that it would be safe to decrease MAXUPVALUES to 16 and MAXVARS to 100 (or even 60). If you set them too low, you'll start getting error messages when you compile complicated functions, but the parser shouldn't crash (as far as I know).

On the whole, I'd say your best bet is to use 5.1.1, though.

 
I know this isn’t relevant if you’ve removed the parsing and are just running compiled code through the Lua VM, but I’m not far enough along to know what stack space requirements the VM has. (BTW, if you’ve done this, please pass along any pointers that might help.)

In both 5.0.2 and 5.1.1, the VM avoids recursive calls into itself, and in any event the basic call doesn't use very much stack. You'll get recursive calls in the following circumstances:

-- metamethod invocations
-- 'for' loops with iterators
-- pcall
-- error functions
-- callbacks into Lua from CFunctions

lua_pcall (and anything else which does a protected call) will allocate a jmp_buf on the stack, amongst other things; depending on your architecture, that can take up quite a bit of space. (or not).

You can also adjust (at compile time) the maximum depth of nested VM calls; you'll find the relevant #define in the same place as the ones I mentioned above.