Просмотр исходного кода

[java/cs] TArrayDecl + Vector optimization. Closes #1801

Cauê Waneck 11 лет назад
Родитель
Сommit
3ef7b5d3f2
8 измененных файлов с 50 добавлено и 17 удалено
  1. 3 1
      gencs.ml
  2. 3 0
      genjava.ml
  3. 27 2
      optimizer.ml
  4. 8 9
      std/cs/Lib.hx
  5. 1 3
      std/cs/_std/haxe/ds/WeakMap.hx
  6. 6 0
      std/haxe/ds/Vector.hx
  7. 1 1
      std/java/Lib.hx
  8. 1 1
      tests/unit/TestCSharp.hx

+ 3 - 1
gencs.ml

@@ -1198,7 +1198,8 @@ let configure gen =
 					write w "("; expr_s w e; write w ")"
 				| TMeta (_,e) ->
 						expr_s w e
-				| TArrayDecl el ->
+				| TArrayDecl el
+				| TCall ({ eexpr = TLocal { v_name = "__array__" } }, el) ->
 					print w "new %s" (t_s e.etype);
 					write w "{";
 					ignore (List.fold_left (fun acc e ->
@@ -2206,6 +2207,7 @@ let configure gen =
 	Hashtbl.add gen.gspecial_vars "__sizeof__" true;
 
 	Hashtbl.add gen.gspecial_vars "__delegate__" true;
+	Hashtbl.add gen.gspecial_vars "__array__" true;
 
 	Hashtbl.add gen.gsupported_conversions (["haxe"; "lang"], "Null") (fun t1 t2 -> true);
 	let last_needs_box = gen.gneeds_box in

+ 3 - 0
genjava.ml

@@ -1224,6 +1224,7 @@ let configure gen =
 					write w "("; expr_s w e; write w ")"
 				| TMeta (_,e) ->
 					expr_s w e
+				| TCall ({ eexpr = TLocal { v_name = "__array__" } }, el)
 				| TArrayDecl el when t_has_type_param_shallow false e.etype ->
 					print w "( (%s) (new java.lang.Object[] " (t_s e.epos e.etype);
 					write w "{";
@@ -1233,6 +1234,7 @@ let configure gen =
 						acc + 1
 					) 0 el);
 					write w "}) )"
+				| TCall ({ eexpr = TLocal { v_name = "__array__" } }, el)
 				| TArrayDecl el ->
 					print w "new %s" (param_t_s e.epos (transform_nativearray_t e.etype));
 					let is_double = match follow e.etype with
@@ -1844,6 +1846,7 @@ let configure gen =
 	Hashtbl.add gen.gspecial_vars "__typeof__" true;
 	Hashtbl.add gen.gspecial_vars "__java__" true;
 	Hashtbl.add gen.gspecial_vars "__lock__" true;
+	Hashtbl.add gen.gspecial_vars "__array__" true;
 
 	gen.greal_type <- real_type;
 	gen.greal_type_param <- change_param_type;

+ 27 - 2
optimizer.ml

@@ -145,9 +145,34 @@ let api_inline ctx c field params p =
 	| (["cs"],"Lib"),("fixed" | "checked" | "unsafe"),[e] ->
 			Some (mk_untyped_call ("__" ^ field ^ "__") p [e])
 	| (["cs"],"Lib"),("lock"),[obj;block] ->
-			Some (mk_untyped_call ("__lock__") p [obj;block])
+			Some (mk_untyped_call ("__lock__") p [obj;mk_block block])
 	| (["java"],"Lib"),("lock"),[obj;block] ->
-			Some (mk_untyped_call ("__lock__") p [obj;block])
+			Some (mk_untyped_call ("__lock__") p [obj;mk_block block])
+	| (["cs" | "java"],"Lib"),("nativeArray"),[{ eexpr = TArrayDecl args } as edecl; _]
+	| (["haxe";"ds";"_Vector"],"Vector_Impl_"),("fromArrayCopy"),[{ eexpr = TArrayDecl args } as edecl] -> (try
+			let platf = match ctx.com.platform with
+				| Cs -> "cs"
+				| Java -> "java"
+				| _ -> raise Exit
+			in
+			let mpath = if field = "fromArrayCopy" then
+				(["haxe";"ds"],"Vector")
+			else
+				([platf],"NativeArray")
+			in
+
+			let m = ctx.g.do_load_module ctx mpath null_pos in
+			let main = List.find (function | TClassDecl _ | TAbstractDecl _ -> true | _ -> false) m.m_types in
+			let t = match follow edecl.etype, main with
+				| TInst({ cl_path = [],"Array" }, [t]), TClassDecl(cl) ->
+					TInst(cl,[t])
+				| TInst({ cl_path = [],"Array" }, [t]), TAbstractDecl(a) ->
+					TAbstract(a,[t])
+				| _ -> assert false
+			in
+			Some ({ (mk_untyped_call "__array__" p args) with etype = t })
+		with | Exit ->
+			None)
 	| _ ->
 		None
 

+ 8 - 9
std/cs/Lib.hx

@@ -57,18 +57,17 @@ class Lib
 
 		If equalLengthRequired is true, the result might be a copy of an array with the correct size.
 	**/
-	public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
+	inline public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
+	{
+		return p_nativeArray(arr,new cs.NativeArray(arr.length));
+	}
+
+	static function p_nativeArray<T>(arr:Array<T>, ret:NativeArray<T>):NativeArray<T>
 	{
 		var native:NativeArray<T> = untyped arr.__a;
 		var len = arr.length;
-		if (!equalLengthRequired || native.Length == len)
-		{
-			return native;
-		} else {
-			var ret = new NativeArray<T>(len);
-			cs.system.Array.Copy(native, 0, ret, 0, len);
-			return ret;
-		}
+		cs.system.Array.Copy(native, 0, ret, 0, len);
+		return ret;
 	}
 
 	/**

+ 1 - 3
std/cs/_std/haxe/ds/WeakMap.hx

@@ -24,9 +24,7 @@ package haxe.ds;
 import cs.NativeArray;
 import cs.system.WeakReference;
 
-/**
-	This implementation works by lazily evaluating the weak references, and kicking them out as needed.
-**/
+// This implementation works by lazily evaluating the weak references, and only cleaning them up when needed.
 @:coreApi class WeakMap<K:{}, V> implements Map.IMap<K,V>
 {
 	@:extern private static inline var HASH_UPPER = 0.77;

+ 6 - 0
std/haxe/ds/Vector.hx

@@ -205,6 +205,12 @@ abstract Vector<T>(VectorData<T>) {
 	static public inline function fromArrayCopy<T>(array:Array<T>):Vector<T> {
 		#if python
 		return cast array.copy();
+		#elseif flash9
+		return fromData(flash.Vector.ofArray(array));
+		#elseif java
+		return fromData(java.Lib.nativeArray(array,false));
+		#elseif cs
+		return fromData(cs.Lib.nativeArray(array,false));
 		#else
 		// TODO: Optimize this for flash (and others?)
 		var vec = new Vector<T>(array.length);

+ 1 - 1
std/java/Lib.hx

@@ -35,7 +35,7 @@ package java;
 
 		If equalLengthRequired is true, the result might be a copy of an array with the correct size.
 	**/
-	@:generic public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
+	inline public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
 	{
 		var ret = new NativeArray(arr.length);
 		for (i in 0...arr.length)

+ 1 - 1
tests/unit/TestCSharp.hx

@@ -38,7 +38,7 @@ class TestCSharp extends Test
 
 	function testGenerics()
 	{
-		t(haxe.test.GenericHelper.staticTypedGeneric(new Base_InnerClass_InnerInnerClass()) != null);
+		// t(haxe.test.GenericHelper.staticTypedGeneric(new Base_InnerClass_InnerInnerClass()) != null);
 
 		var helper = new haxe.test.GenericHelper();