lua-users home
lua-l archive

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


Hi all,

I would like to build a small s-expression ([1]) library in Lua (5.1), so I need to design cons-cells ([2]) first.

I would appreciate some advice in the design of the cons-cells. I am thinking of two approaches:

A) Design cons-cells as Lua tables, in plain Lua, with a "car" and a "cdr" being other Lua objects (possibly other cons-cells). B) Design cons-cells as a C structure (Lua allocated and garbage collected with a __gc metamethod), that keeps references to other Lua objects using luaL_ref and luaL_unref (possibly with references to other cons-cells).

I am thinking that approach B is going to be more performant and consume less memory. These are my assumptions:

1. B is better because using a Lua table as a cons cell is too heavy memory-wise. 2. B Is better because I can abuse LUA_REGISTRYINDEX to store references to Lua objects, that's better than having as many Lua tables in memory. 3. LuaJIT will be as performant in approach B (invoking C functions for "car", "cdr", etc.) as in approach A (using Lua native code). 4. There's no problem in having circular dependencies using luaL_ref's between different cons cells, because the Lua GC will detect that and cleanse cons-cells appropriately.

Am I right in these assumptions? I'm a little worried about point 4.

Thanks in advance,
Antonio


[1] http://en.wikipedia.org/wiki/S-expressions
[2] http://en.wikipedia.org/wiki/Cons