Browse Source

[php] minor optimization of simple constructors of enums

Alexander Kuzmenko 7 years ago
parent
commit
952409a012
2 changed files with 17 additions and 24 deletions
  1. 17 7
      src/generators/genphp7.ml
  2. 0 17
      std/php/Boot.hx

+ 17 - 7
src/generators/genphp7.ml

@@ -3049,17 +3049,27 @@ class enum_builder ctx (enm:tenum) =
 			write_args writer#write (writer#write_arg true) args;
 			write_args writer#write (writer#write_arg true) args;
 			writer#write ") {\n";
 			writer#write ") {\n";
 			writer#indent_more;
 			writer#indent_more;
-			writer#write_indentation;
-			writer#write "return ";
 			let index_str = string_of_int field.ef_index in
 			let index_str = string_of_int field.ef_index in
+			let write_construction args =
+				writer#write ("new " ^ self#get_name ^ "('" ^ name ^ "', " ^ index_str ^", [");
+				write_args writer#write (fun (name, _, _) -> writer#write ("$" ^ name)) args;
+				writer#write "])"
+			in
 			(match args with
 			(match args with
-				| [] -> writer#write ((writer#use hxenum_type_path) ^ "::singleton(static::class, '" ^ name ^ "', " ^ index_str ^")")
+				| [] ->
+					(* writer#write ((writer#use hxenum_type_path) ^ "::singleton(static::class, '" ^ name ^ "', " ^ index_str ^")") *)
+					writer#write_line "static $inst = null;";
+					writer#write_indentation;
+					writer#write "if (!$inst) $inst = ";
+					write_construction [];
+					writer#write ";\n";
+					writer#write_line "return $inst;"
 				| args ->
 				| args ->
-					writer#write ("new " ^ self#get_name ^ "('" ^ name ^ "', " ^ index_str ^", [");
-					write_args writer#write (fun (name, _, _) -> writer#write ("$" ^ name)) args;
-					writer#write "])"
+					writer#write_indentation;
+					writer#write "return ";
+					write_construction args;
+					writer#write ";\n";
 			);
 			);
-			writer#write ";\n";
 			writer#indent_less;
 			writer#indent_less;
 			writer#write_line "}"
 			writer#write_line "}"
 		(**
 		(**

+ 0 - 17
std/php/Boot.hx

@@ -597,27 +597,10 @@ private class HxClass {
 @:keep
 @:keep
 @:dox(hide)
 @:dox(hide)
 private class HxEnum {
 private class HxEnum {
-	static var singletons = new Map<String,HxEnum>();
-
 	var tag : String;
 	var tag : String;
 	var index : Int;
 	var index : Int;
 	var params : NativeArray;
 	var params : NativeArray;
 
 
-	/**
-		Returns instances of constructors without arguments
-	**/
-	public static function singleton( enumClass:String, tag:String, index:Int ) : HxEnum {
-		var key = '$enumClass::$tag';
-
-		var instance = singletons.get(key);
-		if (instance == null) {
-			instance = Syntax.construct(enumClass, tag, index);
-			singletons.set(key, instance);
-		}
-
-		return instance;
-	}
-
 	public function new( tag:String, index:Int, arguments:NativeArray = null ) : Void {
 	public function new( tag:String, index:Int, arguments:NativeArray = null ) : Void {
 		this.tag = tag;
 		this.tag = tag;
 		this.index = index;
 		this.index = index;