瀏覽代碼

[python] reduce generated output for Sys.print or Lib.print significantly, inline all print/println methods to allow optimized code especially for calls with just a string argument close #6184

frabbit 7 年之前
父節點
當前提交
c84d6b1fb7
共有 2 個文件被更改,包括 8 次插入8 次删除
  1. 6 6
      std/python/Lib.hx
  2. 2 2
      std/python/_std/Sys.hx

+ 6 - 6
std/python/Lib.hx

@@ -36,9 +36,11 @@ class Lib {
 	/**
 		Print the specified value on the default output.
 	**/
-	public static function print(v:Dynamic):Void {
-		var str = Std.string(v);
+	public static inline function print(v:Dynamic):Void {
+		printString(Std.string(v));
+	}
 
+	private static function printString (str:String):Void {
 		PySys.stdout.buffer.write( NativeStringTools.encode(str, "utf-8"));
 		PySys.stdout.flush();
 	}
@@ -46,11 +48,9 @@ class Lib {
 	/**
 		Print the specified value on the default output followed by a newline character.
 	**/
-	public static function println(v:Dynamic):Void {
+	public static inline function println(v:Dynamic):Void {
 		var str = Std.string(v);
-
-		PySys.stdout.buffer.write( NativeStringTools.encode('$str\n', "utf-8"));
-		PySys.stdout.flush();
+		printString('$str\n');
 	}
 
 	/**

+ 2 - 2
std/python/_std/Sys.hx

@@ -46,11 +46,11 @@ class Sys {
 		python.lib.Sys.exit(code);
 	}
 
-	public static function print(v:Dynamic):Void {
+	public static inline function print(v:Dynamic):Void {
 		python.Lib.print(v);
 	}
 
-	public static function println(v:Dynamic):Void {
+	public static inline function println(v:Dynamic):Void {
 		python.Lib.println(v);
 	}