소스 검색

[lua] recreation of metatables each time is a bad practice and lowers performance

peyty 9 년 전
부모
커밋
a0a61bde15
1개의 변경된 파일12개의 추가작업 그리고 10개의 파일을 삭제
  1. 12 10
      genlua.ml

+ 12 - 10
genlua.ml

@@ -1689,18 +1689,20 @@ let generate com =
 
 	List.iter (generate_type_forward ctx) com.types; newline ctx;
 
+	(* delay mt construction and cache, coz Array.prototype printed just after this function *)
+	sprln ctx "local _hx_tabArray_mt = nil";
+	sprln ctx "local function _hx_tabArray_mt_init() _hx_tabArray_mt = {";
+	sprln ctx "   __index = Array.prototype,";
+	sprln ctx "   __newindex = function(t,k,v)";
+	sprln ctx "       if type(k) == 'number' and k >= t.length then";
+	sprln ctx "          t.length = k + 1";
+	sprln ctx "       end";
+	sprln ctx "       rawset(t,k,v)";
+	sprln ctx "   end";
+	sprln ctx "} return _hx_tabArray_mt end";
 	sprln ctx "local _hx_tabArray = function(tab,length)";
 	sprln ctx "   tab.length = length";
-	sprln ctx "   setmetatable(tab, {";
-	sprln ctx "	__index = Array.prototype,";
-	sprln ctx "	__newindex = function(t,k,v)";
-	sprln ctx "	    if _G.type(k) == 'number' and k >= t.length then";
-	sprln ctx "		t.length = k + 1";
-	sprln ctx "	    end";
-	sprln ctx "	    rawset(t,k,v)";
-	sprln ctx "	end";
-	sprln ctx "   })";
-	sprln ctx "   return tab";
+	sprln ctx "   return setmetatable(tab, _hx_tabArray_mt or _hx_tabArray_mt_init())";
 	sprln ctx "end";
 
 	List.iter (gen__init__hoist ctx) (List.rev ctx.inits); newline ctx;