Browse Source

improve performance of Std.is, Std.string(Boot.toString), avoid HxOverride.eq calls when possible

frabbit 11 years ago
parent
commit
9e37e778be
4 changed files with 46 additions and 31 deletions
  1. 9 2
      genpy.ml
  2. 18 16
      std/python/Boot.hx
  3. 18 12
      std/python/_std/Std.hx
  4. 1 1
      std/python/internal/HxOverrides.hx

+ 9 - 2
genpy.ml

@@ -1070,10 +1070,17 @@ module Printer = struct
 				let third (_,_,x) = x in
 				let third (_,_,x) = x in
 				let fst (x,_,_) = x in
 				let fst (x,_,_) = x in
 				let snd (_,x,_) = x in
 				let snd (_,x,_) = x in
+				let is_list_or_anon x = begin match x with
+					| TInst({cl_path = [],("list")},_) -> true
+					| TAnon _ -> true
+					| _ -> false
+				end in
 				(match follow e1.etype, follow e2.etype with
 				(match follow e1.etype, follow e2.etype with
-				| TInst({cl_path = [],("list")},_), TInst({cl_path = [],("list")},_) ->
+				| TInst({cl_path = [],("list")},_), _ ->
 					Printf.sprintf "(%s %s %s)" (print_expr pctx e1) (fst ops) (print_expr pctx e2)
 					Printf.sprintf "(%s %s %s)" (print_expr pctx e1) (fst ops) (print_expr pctx e2)
-				| TDynamic _, _ | _, TDynamic _ ->
+				| TDynamic _, TDynamic _ ->
+					Printf.sprintf "%s(%s,%s)" (third ops) (print_expr pctx e1) (print_expr pctx e2)
+				| TDynamic _, x | x, TDynamic _ when is_list_or_anon x ->
 					Printf.sprintf "%s(%s,%s)" (third ops) (print_expr pctx e1) (print_expr pctx e2)
 					Printf.sprintf "%s(%s,%s)" (third ops) (print_expr pctx e1) (print_expr pctx e2)
 				| _,_ -> Printf.sprintf "(%s %s %s)" (print_expr pctx e1) (snd ops) (print_expr pctx e2))
 				| _,_ -> Printf.sprintf "(%s %s %s)" (print_expr pctx e1) (snd ops) (print_expr pctx e2))
 			| TBinop(OpMod,e1,e2) when (is_type1 "" "Int")(e1.etype) && (is_type1 "" "Int")(e2.etype) ->
 			| TBinop(OpMod,e1,e2) when (is_type1 "" "Int")(e1.etype) && (is_type1 "" "Int")(e2.etype) ->

+ 18 - 16
std/python/Boot.hx

@@ -298,28 +298,26 @@ private class ClassRegistry extends python.lib.Dict<String, HxClassBase> {
 			}
 			}
 
 
 
 
-			if (Internal.hasClassName(o) && Syntax.field(Syntax.field(o, "__class__"), "__name__") != "type") {
-
-				var fields = getInstanceFields(o);
-				var fieldsStr = [for (f in fields) '$f : ${toString1(field(o,f), s+"\t")}'];
+			if (Internal.hasClassName(o)) {
+				if (Syntax.field(Syntax.field(o, "__class__"), "__name__") != "type") {
+					var fields = getInstanceFields(o);
+					var fieldsStr = [for (f in fields) '$f : ${toString1(field(o,f), s+"\t")}'];
 
 
-				var toStr = Internal.fieldClassName(o) + "( " + arrayJoin(fieldsStr, ", ") + " )";
-				return toStr;
+					var toStr = Internal.fieldClassName(o) + "( " + arrayJoin(fieldsStr, ", ") + " )";
+					return toStr;
+				} else {
+					var fields = getClassFields(o);
+					var fieldsStr = [for (f in fields) '$f : ${toString1(field(o,f), s+"\t")}'];
+					var toStr = "#" + Internal.fieldClassName(o) + "( " + arrayJoin(fieldsStr, ", ") + " )";
+					return toStr;
+				}
 			}
 			}
 
 
-			if (Internal.hasClassName(o) && Syntax.field(Syntax.field(o, "__class__"), "__name__") == "type") {
-
-				var fields = getClassFields(o);
-				var fieldsStr = [for (f in fields) '$f : ${toString1(field(o,f), s+"\t")}'];
-
-				var toStr = "#" + Internal.fieldClassName(o) + "( " + arrayJoin(fieldsStr, ", ") + " )";
-				return toStr;
-			}
-			if (o == String) {
+			if (isMetaType(o,String)) {
 				return "#String";
 				return "#String";
 			}
 			}
 
 
-			if (o == Array) {
+			if (isMetaType(o,Array)) {
 				return "#Array";
 				return "#Array";
 			}
 			}
 
 
@@ -350,6 +348,10 @@ private class ClassRegistry extends python.lib.Dict<String, HxClassBase> {
 		}
 		}
 	}
 	}
 
 
+	static inline function isMetaType(v:Dynamic, t:Dynamic):Bool {
+		return python.Syntax.binop(v, "==", t);
+	}
+
 	static function fields (o:Dynamic) {
 	static function fields (o:Dynamic) {
 		var a = [];
 		var a = [];
 		if (o != null) {
 		if (o != null) {

+ 18 - 12
std/python/_std/Std.hx

@@ -39,7 +39,12 @@ import python.Syntax;
 		}
 		}
 	}
 	}
 
 
-	@:access(python.Boot.getSuperClass)
+	@:access(python.Boot)
+	static inline function isMetaType(v:Dynamic, t:Dynamic):Bool {
+		return Boot.isMetaType(v,t);
+	}
+
+	@:access(python.Boot)
 	public static function is( v : Dynamic, t : Dynamic ) : Bool {
 	public static function is( v : Dynamic, t : Dynamic ) : Bool {
 
 
 		if (v == null && t == null) {
 		if (v == null && t == null) {
@@ -49,39 +54,40 @@ import python.Syntax;
 
 
 			return false;
 			return false;
 		}
 		}
-		if (t == Dynamic) {
+		if (isMetaType(t,Dynamic)) {
 			return true;
 			return true;
 		}
 		}
 		var isBool = Builtin.isinstance(v, Builtin.bool);
 		var isBool = Builtin.isinstance(v, Builtin.bool);
 
 
-		if (t == Bool && isBool) {
+		if (isMetaType(t, Bool) && isBool) {
 			return true;
 			return true;
 		}
 		}
-		if (!isBool && t != Bool && t == Int && Builtin.isinstance(v, Builtin.int )) {
+		if (!isBool && !isMetaType(t, Bool) && isMetaType(t,Int) && Builtin.isinstance(v, Builtin.int )) {
 			return true;
 			return true;
 		}
 		}
 		var vIsFloat = Builtin.isinstance(v, Builtin.float);
 		var vIsFloat = Builtin.isinstance(v, Builtin.float);
 
 
-		if (!isBool && vIsFloat && t == Int && Math.isFinite(v) && v == Std.int(v) && v <= 2147483647 && v >= -2147483648) {
+		if (!isBool && vIsFloat && isMetaType(t,Int) && Math.isFinite(v) && v == Std.int(v) && v <= 2147483647 && v >= -2147483648) {
 			return true;
 			return true;
 		}
 		}
 
 
 
 
-		if (!isBool &&  t == Float && ( Builtin.isinstance(v, python.Syntax.pythonCode("(float,int)")))) {
+		if (!isBool &&  isMetaType(t,Float) && ( Builtin.isinstance(v, python.Syntax.pythonCode("(float,int)")))) {
 			return true;
 			return true;
 		}
 		}
 
 
-		if ( t == Builtin.str) {
+		if ( isMetaType(t, Builtin.str)) {
 			return Builtin.isinstance(v, String);
 			return Builtin.isinstance(v, String);
 		}
 		}
-		if (t == Enum && Inspect.isclass(v) && Internal.hasConstructs(v)) return true;
-
-		if (t == Enum) return false;
+		var isEnumType = isMetaType(t,Enum);
+		if (isEnumType && Inspect.isclass(v) && Internal.hasConstructs(v)) return true;
 
 
+		if (isEnumType) return false;
 
 
-		if (t == Class && !Builtin.isinstance(v, Enum) && Inspect.isclass(v) && Internal.hasClassName(v) && !Internal.hasConstructs(v)) return true;
+		var isClassType = isMetaType(t,Class);
+		if (isClassType && !Builtin.isinstance(v, Enum) && Inspect.isclass(v) && Internal.hasClassName(v) && !Internal.hasConstructs(v)) return true;
 
 
-		if (t == Class) return false;
+		if (isClassType) return false;
 
 
 		if (try Builtin.isinstance(v, t) catch (e:Dynamic) false) {
 		if (try Builtin.isinstance(v, t) catch (e:Dynamic) false) {
 			return true;
 			return true;

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

@@ -22,7 +22,7 @@ class HxOverrides {
 
 
 	static function eq( a:Dynamic, b:Dynamic ) : Bool {
 	static function eq( a:Dynamic, b:Dynamic ) : Bool {
 		if (Boot.isArray(a) || Boot.isArray(b)) {
 		if (Boot.isArray(a) || Boot.isArray(b)) {
-			return Syntax.pythonCode('$a is $b');
+			return Syntax.pythonCode('a is b');
 		}
 		}
 		return Syntax.binop(a, "==", b);
 		return Syntax.binop(a, "==", b);
 	}
 	}