Quantcast

Recycle compiled code objects

Scott Sibley [08-06-10 - 16:19]
Recycle compiled code objects
Filename
Modules/Text.lua
diff --git a/Modules/Text.lua b/Modules/Text.lua
index 1787ccb..d49ad1d 100644
--- a/Modules/Text.lua
+++ b/Modules/Text.lua
@@ -49,17 +49,33 @@ local function errorhandler(err)
     return geterrorhandler()(err)
 end

-local executeCode = function(tag, code)
-    if not code then return end
+local executeCode
+do
+	local pool = setmetatable({},{__mode='v'})
+	executeCode = function(tag, code)
+		if not code then return end

-	local runnable, err = loadstring(code, tag)
+		local runnable = pool[code]
+		local err
+
+		if runnable then
+			StarTip:Print("recycled runnable")
+		end
+
+		if not runnable then
+			runnable, err = loadstring(code, tag)
+			if runnable then
+				pool[code] = runnable
+			end
+		end

-	if not runnable then
-		StarTip:Print(err)
-		return ""
-	end
+		if not runnable then
+			StarTip:Print(err)
+			return ""
+		end

-    return runnable(xpcall, errorhandler)
+		return runnable(xpcall, errorhandler)
+	end
 end

 -- Thanks to ckknight for this