Skip to content

Commit 4049d0e

Browse files
committed
Optimize RemoveSimpleEquations
- Change `replaceCrefWithBindExp` to use `UnorderedSet` to reduce the amount of allocations. - Avoid unnecessary allocations in `traverseCrefUnreplaceable`.
1 parent 32e51b6 commit 4049d0e

File tree

2 files changed

+61
-54
lines changed

2 files changed

+61
-54
lines changed

OMCompiler/Compiler/BackEnd/RemoveSimpleEquations.mo

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,7 +3400,7 @@ algorithm
34003400
Boolean b;
34013401
case(SOME(e), _)
34023402
equation
3403-
(e, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3403+
(e, b) = replaceCrefWithBindExp(e, globalKnownVars);
34043404
(e, _) = ExpressionSimplify.condsimplify(b, e);
34053405
then
34063406
SOME(e);
@@ -3434,7 +3434,7 @@ algorithm
34343434
equalNonFreeStartValues(values, globalKnownVars, (NONE(), NONE(), cr));
34353435
case (((SOME(e), _))::values, _, (SOME(e2), _, _))
34363436
equation
3437-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3437+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
34383438
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
34393439
true = Expression.expEqual(e1, e2);
34403440
then
@@ -3461,54 +3461,64 @@ algorithm
34613461
equalFreeStartValues(values, globalKnownVars, iValue);
34623462
case (((SOME(e), cr))::values, _, (NONE(), _, _))
34633463
equation
3464-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3464+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
34653465
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
34663466
then
34673467
equalFreeStartValues(values, globalKnownVars, (SOME(e1), SOME(e), cr));
34683468
// compare
34693469
case (((SOME(e), _))::values, _, (SOME(e2), _, _))
34703470
equation
3471-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3471+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
34723472
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
34733473
true = Expression.expEqual(e1, e2);
34743474
then
34753475
equalFreeStartValues(values, globalKnownVars, iValue);
34763476
end match;
34773477
end equalFreeStartValues;
34783478

3479-
protected function replaceCrefWithBindExp "author: Frenkel TUD 2012-12"
3480-
input DAE.Exp inExp;
3481-
input tuple<BackendDAE.Variables, Boolean, HashSet.HashSet> inTuple;
3479+
protected function replaceCrefWithBindExp
3480+
input DAE.Exp exp;
3481+
input BackendDAE.Variables vars;
34823482
output DAE.Exp outExp;
3483-
output tuple<BackendDAE.Variables, Boolean, HashSet.HashSet> outTuple;
3483+
output Boolean replaced;
3484+
protected
3485+
UnorderedSet<DAE.ComponentRef> replaced_crefs =
3486+
UnorderedSet.new(ComponentReference.hashComponentRef, ComponentReference.crefEqual);
34843487
algorithm
3485-
(outExp,outTuple) := matchcontinue (inExp,inTuple)
3486-
local
3487-
DAE.Exp e;
3488-
BackendDAE.Variables vars;
3489-
DAE.ComponentRef cr;
3490-
HashSet.HashSet hs;
3488+
(outExp, replaced) := Expression.traverseExpBottomUp(exp,
3489+
function replaceCrefWithBindExp_traverser(vars = vars, replacedCrefs = replaced_crefs), false);
3490+
end replaceCrefWithBindExp;
3491+
3492+
protected function replaceCrefWithBindExp_traverser
3493+
input DAE.Exp exp;
3494+
input Boolean replaced;
3495+
input BackendDAE.Variables vars;
3496+
input UnorderedSet<DAE.ComponentRef> replacedCrefs;
3497+
output DAE.Exp outExp;
3498+
output Boolean outReplaced;
3499+
protected
3500+
DAE.ComponentRef cr;
3501+
DAE.Exp e;
3502+
algorithm
3503+
(outExp, outReplaced) := matchcontinue (exp, replaced)
34913504
// true if crefs replaced in expression
3492-
case (DAE.CREF(componentRef=cr), (vars, _, hs))
3493-
guard
3494-
// check for cyclic bindings in start value
3495-
not BaseHashSet.has(cr, hs)
3496-
equation
3497-
(BackendDAE.VAR(bindExp = SOME(e)), _) = BackendVariable.getVarSingle(cr, vars);
3498-
hs = BaseHashSet.add(cr, hs);
3499-
(e, (_, _, hs)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (vars, false, hs));
3500-
then
3501-
(e, (vars, true, hs));
3502-
// true if crefs in expression
3503-
case (e as DAE.CREF(), (_, true, _))
3504-
then
3505-
(e, inTuple);
3506-
case (e as DAE.CREF(), (vars, _, hs))
3505+
case (DAE.CREF(componentRef = cr), _)
3506+
// check for cyclic bindings in start value
3507+
guard not UnorderedSet.contains(cr, replacedCrefs)
3508+
algorithm
3509+
(BackendDAE.VAR(bindExp = SOME(e)), _) := BackendVariable.getVarSingle(cr, vars);
3510+
UnorderedSet.add(cr, replacedCrefs);
3511+
e := Expression.traverseExpBottomUp(e,
3512+
function replaceCrefWithBindExp_traverser(vars = vars, replacedCrefs = replacedCrefs), false);
35073513
then
3508-
(e, (vars, true, hs));
3509-
else (inExp,inTuple);
3514+
(e, true);
3515+
3516+
case (DAE.CREF(), _)
3517+
then (exp, true);
3518+
3519+
else (exp, replaced);
35103520
end matchcontinue;
3511-
end replaceCrefWithBindExp;
3521+
end replaceCrefWithBindExp_traverser;
35123522

35133523
protected function getZeroFreeValues "author: Frenkel TUD 2012-12"
35143524
input tuple<Option<DAE.Exp>, DAE.ComponentRef> inTpl;
@@ -3600,7 +3610,7 @@ algorithm
36003610
((e, cr, _)) = selectNonZeroExpression(rest);
36013611
crVar = BackendVariable.varCref(inVar);
36023612
if Flags.isSet(Flags.ALIAS_CONFLICTS) then
3603-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3613+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
36043614
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
36053615
s2 = if b then " = " + ExpressionDump.printExpStr(e1) else "";
36063616
s = iStr + "=> Select value from " + ComponentReference.printComponentRefStr(cr) + "(" + iAttributeName + " = " + ExpressionDump.printExpStr(e) + s2 + ") for variable: " + ComponentReference.printComponentRefStr(crVar) + "\n";
@@ -3616,7 +3626,7 @@ algorithm
36163626
i = i + 5;
36173627
end if;
36183628
if Flags.isSet(Flags.ALIAS_CONFLICTS) then
3619-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3629+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
36203630
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
36213631
s2 = if b then " = " + ExpressionDump.printExpStr(e1) else "";
36223632
s = iStr + " * Candidate: " + ComponentReference.printComponentRefStr(cr) + "(" + iAttributeName + " = " + ExpressionDump.printExpStr(e) + s2 + ", confidence number = " + intString(i) + ")\n";
@@ -3630,7 +3640,7 @@ algorithm
36303640
i = i + 5;
36313641
end if;
36323642
if Flags.isSet(Flags.ALIAS_CONFLICTS) then
3633-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3643+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
36343644
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
36353645
s2 = if b then " = " + ExpressionDump.printExpStr(e1) else "";
36363646
s = iStr + " * Candidate: " + ComponentReference.printComponentRefStr(cr) + "(" + iAttributeName + " = " + ExpressionDump.printExpStr(e) + s2 + ", confidence number = " + intString(i) + ")\n";
@@ -3648,7 +3658,7 @@ algorithm
36483658
i = i + 5;
36493659
end if;
36503660
if Flags.isSet(Flags.ALIAS_CONFLICTS) then
3651-
(e1, (_, b, _)) = Expression.traverseExpBottomUp(e, replaceCrefWithBindExp, (globalKnownVars, false, HashSet.emptyHashSet()));
3661+
(e1, b) = replaceCrefWithBindExp(e, globalKnownVars);
36523662
(e1, _) = ExpressionSimplify.condsimplify(b, e1);
36533663
s2 = if b then " = " + ExpressionDump.printExpStr(e1) else "";
36543664
s = iStr + " * Candidate: " + ComponentReference.printComponentRefStr(cr) + "(" + iAttributeName + " = " + ExpressionDump.printExpStr(e) + s2 + ", confidence number = " + intString(i) + ")\n";
@@ -4523,29 +4533,26 @@ algorithm
45234533
HashSet.HashSet unReplaceable;
45244534
Boolean b;
45254535

4526-
case (DAE.CREF_QUAL(ident=name, identType=ty, subscriptLst=subs, componentRef=cr), SOME(pcr)) equation
4527-
(_, b) = Expression.traverseExpTopDownCrefHelper(DAE.CREF_IDENT(name, ty, subs), Expression.traversingComponentRefPresent, false);
4528-
pcr = if b then ComponentReference.crefPrependIdent(pcr, name, {}, ty) else pcr;
4529-
unReplaceable = if b then BaseHashSet.add(pcr, iUnreplaceable) else iUnreplaceable;
4530-
pcr = ComponentReference.crefPrependIdent(pcr, name, subs, ty);
4536+
case (DAE.CREF_QUAL(ident=name, identType=ty, subscriptLst=subs, componentRef=cr), SOME(pcr)) algorithm
4537+
(_, b) := Expression.traverseExpTopDownSubs(subs, Expression.traversingComponentRefPresent, false);
4538+
pcr := if b then ComponentReference.crefPrependIdent(pcr, name, {}, ty) else pcr;
4539+
unReplaceable := if b then BaseHashSet.add(pcr, iUnreplaceable) else iUnreplaceable;
4540+
pcr := ComponentReference.crefPrependIdent(pcr, name, subs, ty);
45314541
then traverseCrefUnreplaceable(cr, SOME(pcr), unReplaceable);
45324542

4533-
case (DAE.CREF_QUAL(ident=name, identType=ty, subscriptLst=subs, componentRef=cr), NONE()) equation
4534-
(_, b) = Expression.traverseExpTopDownCrefHelper(DAE.CREF_IDENT(name, ty, subs), Expression.traversingComponentRefPresent, false);
4535-
pcr = DAE.CREF_IDENT(name, ty, {});
4536-
unReplaceable = if b then BaseHashSet.add(pcr, iUnreplaceable) else iUnreplaceable;
4543+
case (DAE.CREF_QUAL(ident=name, identType=ty, subscriptLst=subs, componentRef=cr), NONE()) algorithm
4544+
(_, b) := Expression.traverseExpTopDownSubs(subs, Expression.traversingComponentRefPresent, false);
4545+
unReplaceable := if b then BaseHashSet.add(DAE.CREF_IDENT(name, ty, {}), iUnreplaceable) else iUnreplaceable;
45374546
then traverseCrefUnreplaceable(cr, SOME(DAE.CREF_IDENT(name, ty, subs)), unReplaceable);
45384547

4539-
case (DAE.CREF_IDENT(ident=name, identType=ty, subscriptLst=subs), SOME(pcr)) equation
4540-
(_, b) = Expression.traverseExpTopDownCrefHelper(DAE.CREF_IDENT(name, ty, subs), Expression.traversingComponentRefPresent, false);
4541-
pcr = ComponentReference.crefPrependIdent(pcr, name, {}, ty);
4542-
unReplaceable = if b then BaseHashSet.add(pcr, iUnreplaceable) else iUnreplaceable;
4548+
case (DAE.CREF_IDENT(ident=name, identType=ty, subscriptLst=subs), SOME(pcr)) algorithm
4549+
(_, b) := Expression.traverseExpTopDownSubs(subs, Expression.traversingComponentRefPresent, false);
4550+
unReplaceable := if b then BaseHashSet.add(ComponentReference.crefPrependIdent(pcr, name, {}, ty), iUnreplaceable) else iUnreplaceable;
45434551
then unReplaceable;
45444552

4545-
case (DAE.CREF_IDENT(ident=name, identType=ty, subscriptLst=subs), NONE()) equation
4546-
(_, b) = Expression.traverseExpTopDownCrefHelper(DAE.CREF_IDENT(name, ty, subs), Expression.traversingComponentRefPresent, false);
4547-
pcr = DAE.CREF_IDENT(name, ty, {});
4548-
unReplaceable = if b then BaseHashSet.add(pcr, iUnreplaceable) else iUnreplaceable;
4553+
case (DAE.CREF_IDENT(ident=name, identType=ty, subscriptLst=subs), NONE()) algorithm
4554+
(_, b) := Expression.traverseExpTopDownCrefHelper(inCref, Expression.traversingComponentRefPresent, false);
4555+
unReplaceable := if b then BaseHashSet.add(DAE.CREF_IDENT(name, ty, {}), iUnreplaceable) else iUnreplaceable;
45494556
then unReplaceable;
45504557

45514558
case (DAE.OPTIMICA_ATTR_INST_CREF(), _) then iUnreplaceable;

OMCompiler/Compiler/FrontEnd/Expression.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7502,7 +7502,7 @@ algorithm
75027502
end match;
75037503
end traverseExpBidirSubs;
75047504

7505-
protected function traverseExpTopDownSubs
7505+
public function traverseExpTopDownSubs
75067506
input list<DAE.Subscript> inSubscript;
75077507
input FuncType rel;
75087508
input Argument iarg;

0 commit comments

Comments
 (0)