lua-users home
lua-l archive

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




On Sat, Jun 1, 2019 at 5:36 PM Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "Coda" == Coda Highland <chighland@gmail.com> writes:

 >> There's a specific idiom that this kind of optimization would break,
 >> which is when one fetches a key or value from a weak table into a
 >> local variable purely to ensure it does not get collected during
 >> some process. In this case the local variable might never be
 >> referenced at all after assigning it.

 Coda> This is often called RAII ("resource acquisition is
 Coda> initialization" but the actual practice of it doesn't quite fit
 Coda> that exact description anymore).

 Coda> The solution is to assert that <toclose> variables are always
 Coda> considered live.

That is no use for this use case because it is not desired or expected
that the value be closed when the variable goes out of scope, just that
the strong reference to it is dropped.

That's a good point, and it suggests that a Lua dialect that does this optimization should offer a similar annotation for variables that need to be retained. (This would have the additional benefit of not disabling tail calls like <toclose> does.) However, <toclose> is sufficient if you have something like this:

local close_noop = { __close = function() end }
function retain(x)
  return setmetatable({ retained = x }, close_noop)
end

local <toclose> foo = retain(bar)

/s/ Adam