瀏覽代碼

[cs/java] transform array.splice to array.spliceVoid when the result is unused (closes #3962) (#6634)

Dan Korostelev 7 年之前
父節點
當前提交
2f41b4f404
共有 3 個文件被更改,包括 44 次插入0 次删除
  1. 40 0
      src/codegen/gencommon/arraySpliceOptimization.ml
  2. 2 0
      src/generators/gencs.ml
  3. 2 0
      src/generators/genjava.ml

+ 40 - 0
src/codegen/gencommon/arraySpliceOptimization.ml

@@ -0,0 +1,40 @@
+open Common
+open Type
+open Gencommon
+
+(*
+	This filter finds lone array.splice(...) calls within blocks
+	and replaces them with array.spliceVoid(...) calls
+	that don't allocate additional array for removed items.
+*)
+let init com =
+	let rec run e =
+		match e.eexpr with
+		| TBlock el ->
+			let el = List.map (fun e ->
+				match e.eexpr with
+				| TCall ({ eexpr = TField (eobj, FInstance ({ cl_path = [],"Array" } as cl, params, { cf_name = "splice" })) } as e_splice, args) ->
+					let f_spliceVoid = PMap.find "spliceVoid" cl.cl_fields in
+					let e_spliceVoid = { e_splice with
+						eexpr = TField (eobj, FInstance (cl, params, f_spliceVoid));
+						etype = f_spliceVoid.cf_type;
+					} in
+					{ e with
+						eexpr = TCall (e_spliceVoid, args);
+						etype = com.basic.tvoid;
+					}
+				| _ ->
+					run e
+			) el in
+			{ e with eexpr = TBlock el }
+		| _ ->
+			Type.map_expr run e
+	in
+	run
+
+let name = "array_splice_synf"
+let priority = solve_deps name [DAfter ExpressionUnwrap.priority]
+
+let configure gen =
+	let run = init gen.gcon in
+	gen.gsyntax_filters#add name (PCustom priority) run

+ 2 - 0
src/generators/gencs.ml

@@ -3109,6 +3109,8 @@ let generate con =
 
 		UnreachableCodeEliminationSynf.configure gen false;
 
+		ArraySpliceOptimization.configure gen;
+
 		ArrayDeclSynf.configure gen native_arr_cl change_param_type;
 
 		CSharpSpecificSynf.configure gen runtime_cl;

+ 2 - 0
src/generators/genjava.ml

@@ -2591,6 +2591,8 @@ let generate con =
 
 	UnreachableCodeEliminationSynf.configure gen true;
 
+	ArraySpliceOptimization.configure gen;
+
 	ArrayDeclSynf.configure gen native_arr_cl change_param_type;
 
 	JavaSpecificSynf.configure gen runtime_cl;