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
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.