[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: when do programs set metatable?
- From: Mike Pall <mikelu-0802@...>
- Date: Fri, 8 Feb 2008 22:24:34 +0100
Roberto Ierusalimschy wrote:
> Yes. More specificaly, what would be expensive would be going from a
> metatable without a __gc metamethod to a new metatable with a __gc
> metamethod well after creation.
To avoid deteriorating to O(#userdatas * #gcobjects) behaviour
you may want to use a lazy approach: if the userdata object is
not one of the first (say) 5 objects in the rootgc chain (i.e.
recently created) then stop the search. Instead set a global flag
which causes fallback to the old behaviour of atomically scanning
the whole rootgc chain.
> Note that, if you change the metatable
> itself, creating a __gc metamethod, Lua could not detect the change (and
> so would not run the finalizer). (This would be an incompatibility...)
A similar restriction already exists for __mode. I've not heard
of any problems.