From 5f3a57f0a3876ed58ba139c19f71fa8757e92ce7 Mon Sep 17 00:00:00 2001 From: Clement Espeute Date: Wed, 27 Mar 2024 09:53:55 +0100 Subject: [PATCH] [prefab] Properly pre allocate children array --- hrt/prefab/Macros.hx | 7 ++++++- hrt/prefab/Prefab.hx | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/hrt/prefab/Macros.hx b/hrt/prefab/Macros.hx index b3112486b..472f0c824 100644 --- a/hrt/prefab/Macros.hx +++ b/hrt/prefab/Macros.hx @@ -762,7 +762,12 @@ class Macros { else { var _a : Array = 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": diff --git a/hrt/prefab/Prefab.hx b/hrt/prefab/Prefab.hx index e7bbee281..83eb4781a 100644 --- a/hrt/prefab/Prefab.hx +++ b/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; } }