Browse Source

[php] better array writing performance

Aleksandr Kuzmenko 6 years ago
parent
commit
4c96335e7a
2 changed files with 16 additions and 0 deletions
  1. 15 0
      src/generators/genphp7.ml
  2. 1 0
      std/php/_std/Array.hx

+ 15 - 0
src/generators/genphp7.ml

@@ -310,6 +310,11 @@ let is_string_type t = match follow t with TInst ({ cl_path = ([], "String") },
 *)
 let is_string expr = is_string_type expr.etype
 
+(**
+	Check if specified type is Array
+*)
+let is_array_type t = match follow t with TInst ({ cl_path = ([], "Array") }, _) -> true | _ -> false
+
 (**
 	Check if specified type represents a function
 *)
@@ -1606,6 +1611,8 @@ class code_writer (ctx:php_generator_context) hx_type_path php_name =
 					vars#used var.v_name;
 					self#write ("$" ^ var.v_name)
 				| TArray (target, index) -> self#write_expr_array_access target index
+				| TBinop (OpAssign, { eexpr = TArray (target, index) }, value) when is_array_type target.etype ->
+					self#write_expr_set_array_item target index value
 				| TBinop (operation, expr1, expr2) when needs_dereferencing (is_assignment_binop operation) expr1 ->
 					self#write_expr { expr with eexpr = TBinop (operation, self#dereference expr1, expr2) }
 				| TBinop (operation, expr1, expr2) -> self#write_expr_binop operation expr1 expr2
@@ -1720,6 +1727,14 @@ class code_writer (ctx:php_generator_context) hx_type_path php_name =
 					List.iter write_field fields;
 					self#indent_less;
 					self#write_with_indentation "]"
+		(**
+			Writes `target[index] = value` assuming `target` is of `Array` type
+		*)
+		method write_expr_set_array_item target index value =
+			self#write_expr target;
+			self#write "->offsetSet(";
+			write_args self#write self#write_expr [index; value];
+			self#write ")"
 		(**
 			Writes TArray to output buffer
 		*)

+ 1 - 0
std/php/_std/Array.hx

@@ -221,6 +221,7 @@ final class Array<T> implements ArrayAccess<Int, T> {
 			length = offset + 1;
 		}
 		arr[offset] = value;
+		Syntax.code("return {0}", value);
 	}
 
 	@:noCompletion