From e8c779736f3029df353038352c14c8ab63728811 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Nov 2018 13:21:00 -0300 Subject: Removed internal cache for closures The mechanism of "caching the last closure created for a prototype to try to reuse it the next time a closure for that prototype is created" was removed. There are several reasons: - It is hard to find a natural example where this cache has a measurable impact on performance. - Programmers already perceive closure creation as something slow, so they tend to avoid it inside hot paths. (Any case where the cache could reuse a closure can be rewritten predefining the closure in some variable and using that variable.) - The implementation was somewhat complex, due to a bad interaction with the generational collector. (Typically, new closures are new, while prototypes are old. So, the cache breaks the invariant that old objects should not point to new ones.) --- testes/closure.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'testes') diff --git a/testes/closure.lua b/testes/closure.lua index 5d090d91..cdeaebaa 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -44,18 +44,17 @@ assert(B.g == 19) -- testing equality a = {} -collectgarbage"stop" -for i = 1, 5 do a[i] = function (x) return x + a + _ENV end end -collectgarbage"restart" -assert(a[3] == a[4] and a[4] == a[5]) for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end assert(a[3] ~= a[4] and a[4] ~= a[5]) -local function f() - return function (x) return math.sin(_ENV[x]) end +do + local a = function (x) return math.sin(_ENV[x]) end + local function f() + return a + end + assert(f() == f()) end -assert(f() == f()) -- testing closures with 'for' control variable -- cgit v1.2.3-55-g6feb