From 5d4035082e702f1907bb32bd05c5c9d1aeab7767 Mon Sep 17 00:00:00 2001 From: Andrew Minnich Date: Fri, 1 Mar 2019 11:28:49 -0500 Subject: [PATCH] cache forwarded functions to reduce memory usage --- gamestate.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gamestate.lua b/gamestate.lua index e3e78c3..e38a5a4 100644 --- a/gamestate.lua +++ b/gamestate.lua @@ -98,14 +98,17 @@ function GS.registerEvents(callbacks) end -- forward any undefined functions +local function_cache = {} + setmetatable(GS, {__index = function(_, func) -- call function only if at least one 'update' was called beforehand -- (see issue #46) if not state_is_dirty or func == 'update' then state_is_dirty = false - return function(...) + function_cache[func] = function_cache[func] or function(...) return (stack[#stack][func] or __NULL__)(stack[#stack], ...) end + return function_cache[func] end return __NULL__ end})