lua-users home
lua-l archive

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


El 18/01/14 17:58, lua-l-request@lists.lua.org escribió:
Date: Sat, 18 Jan 2014 17:38:31 +0100
From: Philipp Janda <siffiejoe@gmx.net>
Subject: Re: Design questions for a small s-expression library
To: lua-l@lists.lua.org
Message-ID: <lbealr$6nt$1@ger.gmane.org>
Content-Type: text/plain; charset=UTF-8; format=flowed

Am 18.01.2014 15:04 schröbte Antonio Vieiro:
[...]
C) Use closures for cons-cells. See [3].
D) Similar to A, but use the keys `1` and `2` (i.e. the array part of
the table) for head and tail.

`cons`, `car`, `cdr`, `setcar`, and `setcdr` are the only functions that
need to know the difference, so you can even change the implementation
later ...

Memory per cons-cell in bytes (as reported by `collectgarbage"count"`):
table-based Lua 5.x:   144
table-based LuaJIT:     80
closure-based Lua 5.1: 136
closure-based Lua 5.2: 128
closure-based LuaJIT:   76
array-based Lua 5.x:    96
array-based LuaJIT:     56

I thought closures would be more expensive than tables, I may try that. Thanks, Philipp, for the idea!

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.

With good reason. You are basically putting reference counting on top of
garbage collection. Reference counting cannot handle cycles, so the
cycles never become garbage. Even if you have chains instead of cycles
you probably need multiple garbage collection runs before the whole
chain is collected (the `__gc` function has to run before the garbage
collector can realize that the next cons-cell may be collected as well).
So I would advise against B even if you can make sure that there will be
no cycles.


Well, this seems logical: Lua has no way to know that a userdata keeps a reference to another Lua object, so it's reasonable it cannot detect cycles between objects storing luaL_ref's among them.

Another option could be doing GC on top of GC, i.e., building a custom garbage collector for cons-cells. It would be great if one could run this custom garbage-collector immediately before Lua's garbage collector. But as far as I can tell there's no "hook" available in Lua that runs a custom C function before Lua's GC is run.

There's a LUA_MASKCOUNT hook [1], though, that could be used, but only if nobody else is using it. I may explore using it.

Thanks for the advice,
Antonio


[1] http://www.lua.org/manual/5.1/manual.html#lua_sethook