lua-users home
lua-l archive

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


On Fri, Jul 20, 2018 at 12:05:33PM -0700, Gé Weijers wrote:
> On Fri, Jul 20, 2018 at 9:34 AM Roberto Ierusalimschy <
> roberto@inf.puc-rio.br> wrote:
> 
> > I am still curious whether that could work as I suggested, calling
> > __gc as a kind of emergency collection, only when some resource
> > allocation fails. For instance, in the case of file handles in Linux,
> > that would mean ~1 full GC for every 1000 streams created, not
> > one GC per stream.
> >
> >
> If Lua is embedded in a larger program that uses files or other types of
> resources elsewhere you end up having to call the emergency Lua GC from who
> knows where in the code. This may be a 3rd party library, or even a system
> shared library/DLL that you have no control over. A failure to allocate a
> file handle somewhere deep in library code may be unrecoverable.

The same applies to malloc failure. Should Lua also not bother invoking the
emergency collector when malloc fails?

Poor library QoI doesn't justify crippling the runtime or better written
libraries. Rather, the feature should stand on its own merits.

> It's not something I would want to rely on. I have ended up coding a
> version of the Lisp unwind-protect construct to handle this issue. It's not
> perfect because you lose error information when you 'rethrow' an error
> after releasing the resource, and you have to define two local functions in
> the Lua version.

You don't have to rely on it. The nice thing about emergency collection is
that it doesn't really change language semantics or behavior; just fewer
people are accidentally bitten. If it works well enough that the failure to
aggressively deallocate resources results in fewer bugs than all the other
bugs in someone's application, it's a win. The calculus would be different
if implementing the behavior had real costs in terms of language semantics
or implementation complexity, but those costs are effectively nil.

Ideally we'd get a construct that allows us to elegantly request immediate
destruction of particular resources. Emergency collection doesn't make that
construct less likely to emerge unless the Lua authors would be contented by
emergency collection. That's not my impression but maybe I'm wrong about
their motivations. In any event, let's not let the perfect be the enemy of
the better.