Skip to content

Commit

Permalink
Fix GC memory issue when building FMU (#11860)
Browse files Browse the repository at this point in the history
- Double the capacity when adding an element to a full ExpandableArray
  instead of only increasing the capacity by one, to avoid massive
  allocations when adding a lot of elements one by one.
  • Loading branch information
perost committed Jan 23, 2024
1 parent b1b7812 commit d21f7d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion OMCompiler/Compiler/Util/ExpandableArray.mo
Expand Up @@ -149,7 +149,13 @@ protected
algorithm
if index > 0 and (index > capacity or isNone(Dangerous.arrayGetNoBoundsChecking(data, index))) then
if index > capacity then
expandToSize(index, exarray);
capacity := max(capacity, 1);

while index > capacity loop
capacity := capacity * 2;
end while;

expandToSize(capacity, exarray);
data := Dangerous.arrayGetNoBoundsChecking(exarray.data, 1);
end if;

Expand Down
14 changes: 7 additions & 7 deletions testsuite/openmodelica/bootstrapping/ExpandableArrayTest.mos
Expand Up @@ -85,7 +85,7 @@ ExpandableArrayTest.Test(); getErrorString();
// set(2, 2) (1/2)
// ========================================
// 2: 2
// add(1) (2/3)
// add(1) (2/4)
// ========================================
// 2: 2
// 3: 1
Expand All @@ -94,13 +94,13 @@ ExpandableArrayTest.Test(); getErrorString();
// 2: 2
// 3: 1
// 4: 3
// set(5, 6) (4/5)
// set(5, 6) (4/8)
// ========================================
// 2: 2
// 3: 1
// 4: 3
// 5: 6
// update(5, 5) (4/5)
// update(5, 5) (4/8)
// ========================================
// 2: 2
// 3: 1
Expand All @@ -112,14 +112,14 @@ ExpandableArrayTest.Test(); getErrorString();
// 2: 2
// 3: 1
// 4: 3
// set(6, 6) (5/6)
// set(6, 6) (5/8)
// ========================================
// 1: 5
// 2: 2
// 3: 1
// 4: 3
// 6: 6
// clear() (0/6)
// clear() (0/8)
// ========================================
// <empty>
// shrink() (0/0)
Expand All @@ -128,10 +128,10 @@ ExpandableArrayTest.Test(); getErrorString();
// shrink() (0/0)
// ========================================
// <empty>
// set(7, 1) (1/7)
// set(7, 1) (1/8)
// ========================================
// 7: 1
// delete(7) (0/7)
// delete(7) (0/8)
// ========================================
// <empty>
// shrink() (0/0)
Expand Down

0 comments on commit d21f7d9

Please sign in to comment.