Discussion:
__gc support for tables
Roman Tsisyk
2014-09-03 17:22:27 UTC
Permalink
Hi Mike,

Do you have any plans to support `__gc' metamethod for tables in
LuaJIT 2.0.x / 2.1.x?
I remember that you scheduled this feature to 2.1.x series.
Currently it doesn't work on latest v2.1 branch, even with enabled LUA52COMPAT.

I found that it is possible to add some cdata structure to Lua table
and set finalizer on it using ffi.gc(). However, this scheme looks
overcomplicated for such simple task.
Are there any alternatives?

Thanks!
--
WBR,
Roman Tsisyk <roman-***@public.gmane.org>
http://tarantool.org/ - an efficient in-memory data store and a Lua
application server
http://try.tarantool.org/ - try your Lua code **online**
Mike Pall
2014-09-03 17:53:03 UTC
Permalink
Post by Roman Tsisyk
Do you have any plans to support `__gc' metamethod for tables in
LuaJIT 2.0.x / 2.1.x?
Scheduled for the new GC, which is not part of 2.x.

In general, finalizers are for cleanup of resources that are NOT
memory resources. A plain table only uses memory resources, but it
may reference objects that do (userdata, cdata). Finalizers should
be attached to these objects, not to the containing table.

An assumed need to attach a finalizer to a table is often misguided
and the overall data structure design should be reviewed. There are
very few valid reasons to do this, which is why there has been no
pressing need to implement finalizers for plain Lua tables. Like
all finalizers, it has it's hidden costs.

--Mike
William Adams
2014-09-03 18:42:24 UTC
Permalink
Just to add fuel to this...

When I first met LuaJIT, I went through the same exercise of wanting finalizers on tables.

then I examined why I really wanted that, and thought about the resources I was handling, as Mike points out.

Then I slipped into a habbit of just coding up the specific CData structures I needed to manage with their attendant constructors and finalizers.

This works out waaaay better, and gives you independence from the Lua table, in terms of lifetime.

If you want to see a ton of examples of doing this with real world APIs (Windows), you might paruse the sources here: http://github.com/wiladams/tinn/

Or, for Linux, here: http://github.com/justincormack/ljsyscall

-- William


===============================
- Shaping clay is easier than digging it out of the ground.


----------------------------------------
Date: Wed, 3 Sep 2014 19:53:03 +0200
Subject: Re: __gc support for tables
Post by Roman Tsisyk
Do you have any plans to support `__gc' metamethod for tables in
LuaJIT 2.0.x / 2.1.x?
Scheduled for the new GC, which is not part of 2.x.
In general, finalizers are for cleanup of resources that are NOT
memory resources. A plain table only uses memory resources, but it
may reference objects that do (userdata, cdata). Finalizers should
be attached to these objects, not to the containing table.
An assumed need to attach a finalizer to a table is often misguided
and the overall data structure design should be reviewed. There are
very few valid reasons to do this, which is why there has been no
pressing need to implement finalizers for plain Lua tables. Like
all finalizers, it has it's hidden costs.
--Mike
Karel Tuma
2014-09-03 18:56:41 UTC
Permalink
Post by William Adams
When I first met LuaJIT, I went through the same exercise of wanting finalizers on tables.
The only legitimate use case I've met for this is when the table is a
proxy object tracking state in another GCed VM (language bridge).

newproxy is not an option - one still needs auxiliary data
associated/destroyed/resurrected, and one gets turtles all
the way down with that.

Because that also needs even more evil feature - resurrection of values in
a weak table from within __gc handler (at least one side of the bridge, and
V8 gc lacks it) I had to port 5.3 GC to LuaJIT :(

Certainly hoping the new 4-colour GC will match 5.2/5.3 semantics more
closely.
Roman Tsisyk
2014-09-04 05:20:41 UTC
Permalink
Post by Mike Pall
Post by Roman Tsisyk
Do you have any plans to support `__gc' metamethod for tables in
LuaJIT 2.0.x / 2.1.x?
Scheduled for the new GC, which is not part of 2.x.
In general, finalizers are for cleanup of resources that are NOT
memory resources. A plain table only uses memory resources, but it
may reference objects that do (userdata, cdata). Finalizers should
be attached to these objects, not to the containing table.
An assumed need to attach a finalizer to a table is often misguided
and the overall data structure design should be reviewed. There are
very few valid reasons to do this, which is why there has been no
pressing need to implement finalizers for plain Lua tables. Like
all finalizers, it has it's hidden costs.
My resource is a file descriptor (int) stored inside Lua table.
ffi.gc() works only on pointers and structures, but not on plain types, .

I can't replace wrapping Lua table with cdata struct, because
this table also contains another Lua table that used internally
by the implementation.
--
WBR,
Roman Tsisyk <roman-***@public.gmane.org>
http://tarantool.org/ - an efficient in-memory data store and a Lua
application server
http://try.tarantool.org/ - try your Lua code **online**
Coda Highland
2014-09-04 05:27:20 UTC
Permalink
Post by Roman Tsisyk
Post by Mike Pall
Post by Roman Tsisyk
Do you have any plans to support `__gc' metamethod for tables in
LuaJIT 2.0.x / 2.1.x?
Scheduled for the new GC, which is not part of 2.x.
In general, finalizers are for cleanup of resources that are NOT
memory resources. A plain table only uses memory resources, but it
may reference objects that do (userdata, cdata). Finalizers should
be attached to these objects, not to the containing table.
An assumed need to attach a finalizer to a table is often misguided
and the overall data structure design should be reviewed. There are
very few valid reasons to do this, which is why there has been no
pressing need to implement finalizers for plain Lua tables. Like
all finalizers, it has it's hidden costs.
My resource is a file descriptor (int) stored inside Lua table.
ffi.gc() works only on pointers and structures, but not on plain types, .
I can't replace wrapping Lua table with cdata struct, because
this table also contains another Lua table that used internally
by the implementation.
--
WBR,
http://tarantool.org/ - an efficient in-memory data store and a Lua
application server
http://try.tarantool.org/ - try your Lua code **online**
Then wrap the fd in a cdata struct and include that inside your Lua
table, and attach the finalizer to that. That's what Mike was trying
to say in the first place.

/s/ Adam
Roman Tsisyk
2014-09-04 05:50:42 UTC
Permalink
Post by Coda Highland
Then wrap the fd in a cdata struct and include that inside your Lua
table, and attach the finalizer to that. That's what Mike was trying
to say in the first place.
Yes, it works for me.
I just try to find a better solution because I have at least 3 similar
cases in my code.
--
WBR,
Roman Tsisyk <roman-***@public.gmane.org>
http://tarantool.org/ - an efficient in-memory data store and a Lua
application server
http://try.tarantool.org/ - try your Lua code **online**
Continue reading on narkive:
Loading...