Skip to content

Commit 926cc34

Browse files
authored
Optimize ClassTree.instantiate (#14061)
- Combine cloning and setting the parent of a component into one operation to reduce memory allocations.
1 parent 757ef85 commit 926cc34

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

OMCompiler/Compiler/NFFrontEnd/NFClassTree.mo

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,11 @@ public
561561
case InstNode.COMPONENT_NODE()
562562
algorithm
563563
// Set the component's parent and create a unique instance for it.
564-
node := InstNode.setParent(instance, c);
565-
comp := InstNode.component(node);
566-
node := InstNode.replaceComponent(comp, node);
564+
node := InstNode.cloneComponent(c, instance);
567565

568566
// If the component is outer, link it with the corresponding
569567
// inner component.
570-
if Component.isOuter(comp) then
568+
if InstNode.isOuter(node) then
571569
try
572570
node := linkInnerOuter(node, inst_scope);
573571
else
@@ -623,15 +621,7 @@ public
623621
case Class.PARTIAL_BUILTIN(elements = tree as FLAT_TREE(components = old_comps))
624622
algorithm
625623
instance := if InstNode.isEmpty(instance) then clsNode else instance;
626-
old_comps := arrayCopy(old_comps);
627-
628-
for i in 1:arrayLength(old_comps) loop
629-
node := old_comps[i];
630-
node := InstNode.setParent(instance, node);
631-
old_comps[i] := InstNode.replaceComponent(InstNode.component(node), node);
632-
end for;
633-
634-
tree.components := old_comps;
624+
tree.components := Array.map(old_comps, function InstNode.cloneComponent(newParent = instance));
635625
cls.elements := tree;
636626
compCount := arrayLength(old_comps);
637627
then

OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,18 @@ uniontype InstNode
19521952
end match;
19531953
end clone;
19541954

1955+
function cloneComponent
1956+
input InstNode component;
1957+
input InstNode newParent;
1958+
output InstNode outComponent;
1959+
algorithm
1960+
outComponent := match component
1961+
case COMPONENT_NODE()
1962+
then COMPONENT_NODE(component.name, component.definition, component.visibility,
1963+
Pointer.create(Pointer.access(component.component)), newParent, component.nodeType);
1964+
end match;
1965+
end cloneComponent;
1966+
19551967
function getComments
19561968
input InstNode node;
19571969
input list<SCode.Comment> accumCmts = {};

0 commit comments

Comments
 (0)