瀏覽代碼

[prefab] Pre allocate children array on clone

This results in a 20% perf gain in the clone() phase when cloning big
prefabs
Clement Espeute 1 年之前
父節點
當前提交
e2b7da2e4a
共有 2 個文件被更改,包括 11 次插入3 次删除
  1. 6 1
      hrt/prefab/Macros.hx
  2. 5 2
      hrt/prefab/Prefab.hx

+ 6 - 1
hrt/prefab/Macros.hx

@@ -762,7 +762,12 @@ class Macros {
 							else
 							{
 								var _a : Array<Dynamic> = cast $e{source};
-								[for (_elem in _a) $e{hrt.prefab.Macros.deepCopyRec(params[0], macro @:pos(source.pos) _elem, targetType, null, custom, true)}];
+								var target = [];
+								target.resize(_a.length);
+								for (idx => _elem in _a) {
+									target[idx] = $e{hrt.prefab.Macros.deepCopyRec(params[0], macro @:pos(source.pos) _elem, targetType, null, custom, true)};
+								}
+								target;
 							}
 						};
 					case "String":

+ 5 - 2
hrt/prefab/Prefab.hx

@@ -195,12 +195,15 @@ class Prefab {
 		var inst = Type.createEmptyInstance(thisClass);
 		inst.postCloneInit();		// Macro function that init all the non serializable fields of a prefab
 		inst.children = [];
+		inst.children.resize(children.length);
 		inst.__newInit(parent, sh);// Macro function that contains the code of the new function
 
 		inst.copy(this);
 		if (withChildren) {
-			for (child in children) {
-				child.clone(inst, sh);
+			for (idx => child in children) {
+				var cloneChild = child.clone(null, sh);
+				@:bypassAccessor cloneChild.parent = inst;
+				inst.children[idx] = cloneChild;
 			}
 		}