[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: questions about closure construction
- From: steve donovan <steve.j.donovan@...>
- Date: Tue, 19 Mar 2013 09:47:38 +0200
On Tue, Mar 19, 2013 at 4:27 AM, Ross Bencina
<rossb-lists@audiomulch.com> wrote:
> function __y_bar(self) print "y" end
> function Y()
> return { foo = __y_bar }
> end
> function Z()
> local function __z_bar(self) print "z" end
> return { foo = __z_bar }
> end
>
> Z runs slower than X or Y, suggesting that there is some runtime overhead in
> creating a named local variable (which surprises me, seems like a simple
> optimisation).
Yes, indeed; in Z you create a closure each time, which is a dynamic
object (much as how {} is a 'table constructor' not a 'table
literal').
I believe Lua 5.2 has made some progress in re-using closures that
have no upvalues. LuaJIT may well optimize it to nothing ;)
steve d.