瀏覽代碼

[prefab] Properly pre allocate children array

Clement Espeute 1 年之前
父節點
當前提交
5f3a57f0a3
共有 2 個文件被更改,包括 14 次插入3 次删除
  1. 6 1
      hrt/prefab/Macros.hx
  2. 8 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":

+ 8 - 2
hrt/prefab/Prefab.hx

@@ -199,8 +199,14 @@ class Prefab {
 
 		inst.copy(this);
 		if (withChildren) {
-			for (child in children) {
-				child.clone(inst, sh);
+			inst.children.resize(children.length);
+			for (idx => child in children) {
+				var cloneChild = child.clone(null, sh);
+
+				// "parent" setter pushes into children, but we don't want that
+				// as we have prealocated the array children
+				@:bypassAccessor cloneChild.parent = inst;
+				inst.children[idx] = cloneChild;
 			}
 		}