Browse Source

improve ArrayAccess on Array performance

frabbit 11 years ago
parent
commit
0d195ccef5
4 changed files with 16 additions and 9 deletions
  1. 7 0
      genpy.ml
  2. 4 4
      std/python/_std/Array.hx
  3. 3 3
      std/python/internal/ArrayImpl.hx
  4. 2 2
      std/python/internal/HxOverrides.hx

+ 7 - 0
genpy.ml

@@ -914,6 +914,9 @@ module Printer = struct
 	let is_underlying_string t = match follow t with
 	let is_underlying_string t = match follow t with
 		| TAbstract(a,tl) -> (is_type1 "" "String")(Codegen.Abstract.get_underlying_type a tl)
 		| TAbstract(a,tl) -> (is_type1 "" "String")(Codegen.Abstract.get_underlying_type a tl)
 		| _ -> false
 		| _ -> false
+	let is_underlying_array t = match follow t with
+		| TAbstract(a,tl) -> (is_type1 "" "list")(Codegen.Abstract.get_underlying_type a tl)
+		| _ -> false
 
 
 	let handle_keywords s =
 	let handle_keywords s =
 		KeywordHandler.handle_keywords s
 		KeywordHandler.handle_keywords s
@@ -1035,8 +1038,12 @@ module Printer = struct
 				handle_keywords v.v_name
 				handle_keywords v.v_name
 			| TEnumParameter(e1,_,index) ->
 			| TEnumParameter(e1,_,index) ->
 				Printf.sprintf "%s.params[%i]" (print_expr pctx e1) index
 				Printf.sprintf "%s.params[%i]" (print_expr pctx e1) index
+			| TArray(e1,e2) when (is_type1 "" "list")(e1.etype) || is_underlying_array e1.etype ->
+				Printf.sprintf "python_internal_ArrayImpl._get(%s, %s)" (print_expr pctx e1) (print_expr pctx e2)
 			| TArray(e1,e2) ->
 			| TArray(e1,e2) ->
 				Printf.sprintf "HxOverrides.arrayGet(%s, %s)" (print_expr pctx e1) (print_expr pctx e2)
 				Printf.sprintf "HxOverrides.arrayGet(%s, %s)" (print_expr pctx e1) (print_expr pctx e2)
+			| TBinop(OpAssign, {eexpr = TArray(e1,e2)}, e3) when (is_type1 "" "list")(e1.etype) || is_underlying_array e1.etype ->
+				Printf.sprintf "python_internal_ArrayImpl._set(%s, %s, %s)" (print_expr pctx e1) (print_expr pctx e2) (print_expr pctx e3)
 			| TBinop(OpAssign,{eexpr = TArray(e1,e2)},e3) ->
 			| TBinop(OpAssign,{eexpr = TArray(e1,e2)},e3) ->
 				Printf.sprintf "HxOverrides.arraySet(%s,%s,%s)" (print_expr pctx e1) (print_expr pctx e2) (print_expr pctx e3)
 				Printf.sprintf "HxOverrides.arraySet(%s,%s,%s)" (print_expr pctx e1) (print_expr pctx e2) (print_expr pctx e3)
 			| TBinop(OpAssign,{eexpr = TField(ef1,fa)},e2) ->
 			| TBinop(OpAssign,{eexpr = TField(ef1,fa)},e2) ->

+ 4 - 4
std/python/_std/Array.hx

@@ -117,14 +117,14 @@ extern class Array<T> implements ArrayAccess<T> extends ArrayImpl {
 
 
 
 
 
 
-	@:keep private inline function __get(idx:Int):T
+	@:keep private inline function _get(idx:Int):T
 	{
 	{
-		return ArrayImpl.__get(this, idx);
+		return ArrayImpl._get(this, idx);
 	}
 	}
 
 
-	@:keep private inline function __set(idx:Int, val:T):T
+	@:keep private inline function _set(idx:Int, val:T):T
 	{
 	{
-		return ArrayImpl.__set(this, idx,val);
+		return ArrayImpl._set(this, idx,val);
 	}
 	}
 
 
 	@:keep private inline function __unsafe_get(idx:Int):T
 	@:keep private inline function __unsafe_get(idx:Int):T

+ 3 - 3
std/python/internal/ArrayImpl.hx

@@ -137,11 +137,11 @@ class ArrayImpl {
 		Syntax.callField(a, "reverse");
 		Syntax.callField(a, "reverse");
 	}
 	}
 
 
-	private static inline function __get<T>(x:Array<T>, idx:Int):T {
-		return if (idx < x.length && idx > -1) Syntax.arrayAccess(x, idx) else null;
+	private static inline function _get<T>(x:Array<T>, idx:Int):T {
+		return if (idx > -1 && idx < x.length) Syntax.arrayAccess(x, idx) else null;
 	}
 	}
 
 
-	private static inline function __set<T>(x:Array<T>, idx:Int, v:T):T {
+	private static inline function _set<T>(x:Array<T>, idx:Int, v:T):T {
 		var l = x.length;
 		var l = x.length;
 		while (l < idx) {
 		while (l < idx) {
 			x.push(null);
 			x.push(null);

+ 2 - 2
std/python/internal/HxOverrides.hx

@@ -48,7 +48,7 @@ class HxOverrides {
 
 
 	static public function arrayGet<T>(a:Dynamic, i:Int):Dynamic {
 	static public function arrayGet<T>(a:Dynamic, i:Int):Dynamic {
 		if (Boot.isArray(a)) {
 		if (Boot.isArray(a)) {
-			return ArrayImpl.__get(a, i);
+			return ArrayImpl._get(a, i);
 		} else {
 		} else {
 			return Syntax.arrayAccess(a, i);
 			return Syntax.arrayAccess(a, i);
 		}
 		}
@@ -56,7 +56,7 @@ class HxOverrides {
 
 
 	static public function arraySet(a:Dynamic, i:Int, v:Dynamic) {
 	static public function arraySet(a:Dynamic, i:Int, v:Dynamic) {
 		if (Boot.isArray(a)) {
 		if (Boot.isArray(a)) {
-			return ArrayImpl.__set(a, i, v);
+			return ArrayImpl._set(a, i, v);
 		} else {
 		} else {
 			Syntax.assign(Syntax.arrayAccess(a, i), v);
 			Syntax.assign(Syntax.arrayAccess(a, i), v);
 			return v;
 			return v;