Skip to content

Commit

Permalink
Improve performance of Expression.extractUniqueCrefs* (#12638)
Browse files Browse the repository at this point in the history
- Use `UnorderedSet.unique_list` instead of `List.unique` for the
  `Expression.extractUniqueCrefs*` functions to improve scaling for
  large lists.
- Optimize `UnorderedSet.unique_list` to avoid unnecessary work if the
  list has no more than one element.

Fixes #10252
  • Loading branch information
perost committed Jun 24, 2024
1 parent c1e0c3a commit 907b8bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ComponentReference.mo
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected import Print;
protected import StringUtil;
protected import System;
protected import Types;
protected import UnorderedSet;
protected import Util;


Expand Down Expand Up @@ -4078,5 +4079,12 @@ algorithm
end match;
end isWild;

public function uniqueList
input list<DAE.ComponentRef> crefs;
output list<DAE.ComponentRef> uniqueCrefs;
algorithm
uniqueCrefs := UnorderedSet.unique_list(crefs, hashComponentRef, crefEqual);
end uniqueList;

annotation(__OpenModelica_Interface="frontend");
end ComponentReference;
9 changes: 5 additions & 4 deletions OMCompiler/Compiler/FrontEnd/Expression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6082,7 +6082,8 @@ public function extractUniqueCrefsFromExp
input Boolean expand = true;
output list<DAE.ComponentRef> ocrefs;
algorithm
ocrefs := List.unique(extractCrefsFromExp(inExp));
ocrefs := ComponentReference.uniqueList(extractCrefsFromExp(inExp));

if expand then
ocrefs := List.flatten(List.map1(ocrefs, ComponentReference.expandCref, true));
end if;
Expand Down Expand Up @@ -6125,7 +6126,7 @@ public function extractUniqueCrefsFromExpDerPreStart
input Boolean expand = true;
output list<DAE.ComponentRef> ocrefs;
algorithm
ocrefs := List.unique(extractCrefsFromExpDerPreStart(inExp, expand));
ocrefs := ComponentReference.uniqueList(extractCrefsFromExpDerPreStart(inExp, expand));
end extractUniqueCrefsFromExpDerPreStart;

public function extractCrefsFromExpDerPreStart
Expand Down Expand Up @@ -6196,8 +6197,8 @@ protected
list<DAE.ComponentRef> olhscrefs;
algorithm
(lhscreflstlst,rhscreflstlst) := List.map_2(inStmts,extractCrefsStatment);
orhscrefs := List.unique(List.flatten(rhscreflstlst));
olhscrefs := List.unique(List.flatten(lhscreflstlst));
orhscrefs := ComponentReference.uniqueList(List.flatten(rhscreflstlst));
olhscrefs := ComponentReference.uniqueList(List.flatten(lhscreflstlst));
ocrefs := (olhscrefs,orhscrefs);
end extractUniqueCrefsFromStatmentS;

Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Util/UnorderedSet.mo
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public
input list<T> inList;
input Hash hashFunc;
input KeyEq keyEqFunc;
output list<T> outList = toList(fromList(inList, hashFunc, keyEqFunc));
output list<T> outList = if List.hasSeveralElements(inList) then toList(fromList(inList, hashFunc, keyEqFunc)) else inList;
end unique_list;

function union
Expand Down

0 comments on commit 907b8bd

Please sign in to comment.