lua-users home
lua-l archive

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

May be, but this "j" upvalue is not referenced at all in the function; there's then no point at all to create a new separate closure for each distinct value of the const "j".

All this causes unnecessary stress on the memory allocator (and later on the garbage collector): imagine that instead of performing 2 loops, you loop one million times, it will then allocate one million objects with needless contents for the distinct j constant never used: that's what I all a regression, which can be severe and can affect serious projects, where there are lot of loops spread everywhere and the regression will create really too many needless distinct closures, causing a severe degradation in terms of memory usage.

In fact a compiler should make all efforts possible to reduce what is not needed to include at all in the set of upvalues of all closures (this is what _javascript_ does by default and it is even part of its core specifications).

Then if the generated reduced closures are identical, there's no need at all to duplicate them. In the example given, the closure should always have an empty set of upvalues, so the same function closure can be safely shared: the value of j does not matter at all, it's never accessed, never needed by the function body.

So I consider this to be a severe regression bug in Lua 5.4.

Le jeu. 8 août 2019 à 23:06, Soni "They/Them" L. <> a écrit :

On 2019-08-07 8:28 p.m., Egor Skriptunoff wrote:
> t = {}
> for j = 1, 2 do
>    t[j] = function(x) return 2*x end
> end
> print(t[1] == t[2])
> Lua 5.1    false
> Lua 5.2    true
> Lua 5.3    true
> Lua 5.4    false
> It seems that the equality between function values has returned back
> to its Lua 5.1 semantics.
> 1)
> Why "function values caching" has been removed from Lua 5.4?
> 2)
> Could we rely on the fact that in Lua 5.4+ any created function is
> unique value?

Lua 5.4 has const locals in loops. Each loop iteration gets a new j,
which is const.

That just means each closure is using a different upvalue, and gets
closed on each loop iteration. Try calling them!

(Well, I could be wrong, I guess. Correct me if I'm wrong.)