Browse Source

[lua] improve speed of array

This fix removes some type checks on __newindex, which will
speed the creation of new index values.

However, note that it's now no longer to specify fields dynamically on an
array using standard access (e.g. enum metatable data, etc.) This now
requires a rawset.  No big deal there, nobody should be doing that
anyways.
Justin Donaldson 9 years ago
parent
commit
7f0447fa59
4 changed files with 14 additions and 15 deletions
  1. 7 8
      src/generators/genlua.ml
  2. 3 2
      std/lua/Boot.hx
  3. 3 4
      std/lua/_lua/_hx_tab_array.lua
  4. 1 1
      std/lua/_std/Reflect.hx

+ 7 - 8
src/generators/genlua.ml

@@ -379,7 +379,7 @@ let rec gen_call ctx e el in_value =
 			| e :: _ -> gen_value ctx e)
 	| TLocal { v_name = "__resources__" }, [] ->
 		(* TODO: Array declaration helper *)
-		spr ctx "_hx_tabArray({";
+		spr ctx "_hx_tab_array({";
 		let count = ref 0 in
 		concat ctx "," (fun (name,data) ->
 			if (!count == 0) then spr ctx "[0]=";
@@ -541,7 +541,7 @@ and gen_expr ?(local=true) ctx e = begin
 		    gen_call ctx e el false;
 		end;
 	| TArrayDecl el ->
-		spr ctx "_hx_tabArray({";
+		spr ctx "_hx_tab_array({";
 		let count = ref 0 in
 		List.iteri (fun i e ->
 		    incr count;
@@ -1527,8 +1527,8 @@ let generate_enum ctx e =
 	    if has_feature ctx "lua.Boot.isEnum" then  begin
 		print ctx " __ename__ = %s," (if has_feature ctx "Type.getEnumName" then "{" ^ String.concat "," ename ^ "}" else "true");
 	    end;
-	    (* TODO :  Come up with a helper function for _hx_tabArray declarations *)
-	    spr ctx " __constructs__ = _hx_tabArray({";
+	    (* TODO :  Come up with a helper function for _hx_tab_array declarations *)
+	    spr ctx " __constructs__ = _hx_tab_array({";
 	    if ((List.length e.e_names) > 0) then begin
 		    spr ctx "[0]=";
 		    spr ctx (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" s) e.e_names));
@@ -1549,18 +1549,17 @@ let generate_enum ctx e =
 		| TFun (args,_) ->
 			let count = List.length args in
 			let sargs = String.concat "," (List.map (fun (n,_,_) -> ident n) args) in
-			print ctx "function(%s) local _x = _hx_tabArray({[0]=\"%s\",%d,%s,__enum__=%s}, %i);" sargs f.ef_name f.ef_index sargs p (count + 2);
+			print ctx "function(%s) local _x = _hx_tab_array({[0]=\"%s\",%d,%s,__enum__=%s}, %i);" sargs f.ef_name f.ef_index sargs p (count + 2);
 			if has_feature ctx "may_print_enum" then
 				(* TODO: better namespacing for _estr *)
 				spr ctx " _x.toString = _estr;";
 			spr ctx " return _x; end ";
 			ctx.separator <- true;
 		| _ ->
-			println ctx "_hx_tabArray({[0]=\"%s\",%d},2)" f.ef_name f.ef_index;
+			println ctx "_hx_tab_array({[0]=\"%s\",%d,__enum__ = %s},2)" f.ef_name f.ef_index p;
 			if has_feature ctx "may_print_enum" then begin
 				println ctx "%s%s.toString = _estr" p (field f.ef_name);
 			end;
-			print ctx "%s%s.__enum__ = %s" p (field f.ef_name) p;
 		);
 		newline ctx
 	) e.e_names;
@@ -1572,7 +1571,7 @@ let generate_enum ctx e =
 				| _ -> true
 		) e.e_names in
 		print ctx "%s.__empty_constructs__ = " p;
-		spr ctx "_hx_tabArray({";
+		spr ctx "_hx_tab_array({";
 		if (List.length ctors_without_args)  > 0 then
 		    begin
 			spr ctx "[0] = ";

+ 3 - 2
std/lua/Boot.hx

@@ -56,7 +56,8 @@ class Boot {
 		if (m == null) return null;
 		// if (m.__id.__ == nil) m.__id__ = _fid + 1;
 		var f: Function = null;
-		if ( o.hx__closures__ == null ) o.hx__closures__ = {};
+		if ( o.hx__closures__ == null )
+			Lua.rawset(o,"hx__closures__", {});
 		else untyped f = o.hx__closures__[m];
 		if (f == null){
 			f = untyped __lua__("function(...) return m(o, ...) end");
@@ -253,7 +254,7 @@ class Boot {
 	*/
 	public inline static function defArray<T>(tab: Table<Int,T>, ?length : Int) : Array<T> {
 		if (length == null) length = Table.maxn(tab) + 1; // maxn doesn't count 0 index
-		return untyped _hx_tabArray(tab, length);
+		return untyped _hx_tab_array(tab, length);
 	}
 
 	/*

+ 3 - 4
std/lua/_lua/_hx_tab_array.lua

@@ -1,13 +1,12 @@
 local _hx_array_mt = {
   __newindex = function(t,k,v)
-    if type(k) == 'number' and k >= t.length then
-      t.length = k + 1
-    end
+    local len = t.length
+    t.length =  k >= len and (k + 1) or len
     rawset(t,k,v)
   end
 }
 
-local function _hx_tabArray(tab,length)
+local function _hx_tab_array(tab,length)
   tab.length = length
   return setmetatable(tab, _hx_array_mt)
 end

+ 1 - 1
std/lua/_std/Reflect.hx

@@ -160,7 +160,7 @@ import lua.Boot;
 				b[k-1] = v
 				l = math.max(k,l)
 			end
-			return f(_hx_tabArray(b, l))
+			return f(_hx_tab_array(b, l))
 		end");
 	}