[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Upvalue efficiency [was: Fast metamethods for Userdata types]
- From: Tim Hill <drtimhill@...>
- Date: Sat, 11 Apr 2015 12:20:17 -0700
> On Apr 8, 2015, at 7:15 AM, David Favro <lua@meta-dynamic.com> wrote:
>
> This reminds me of a coding-style question that comes up often for me. It amounts to, which is the "better" way to get data into a function, accessing an upvalue or passing in a parameter, especially when the use of the function is localized such as a function defined within another. My definition of "better" is typically run-time efficiency when the function is called many times, but I'd be interested in opinions of style as well. I sometimes superstitiously think that there may be some "advantage" in creating a function that has no external references rather than one which requires separate closures, but I honestly have no idea if this is true.
>
I don’t think this is really a “better/worse” thing as upvalues and arguments are different in several respects. First, an upvalue has a lifetime that extends across all calls to the closure. Second, parameters are visible at the “point of call” and are therefore known to the caller. So a pretty simple rule is: If the data is local to a single call, it’s a parameter, if it extends across calls, it might be better as an upvalue.
—Tim