Skip to content

Commit 8b38efa

Browse files
committed
Fix array builder append value to grow array only when needed.
1 parent b5e583a commit 8b38efa

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/main/java/org/truffleruby/core/array/ArrayBuilderNode.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ public Object appendNewStrategy(Object store, int index, Object value) {
198198
final ArrayStrategy valueStrategy = ArrayStrategy.forValue(value);
199199
final ArrayStrategy generalized = currentStrategy.generalize(valueStrategy);
200200

201-
final int neededCapacity = ArrayUtils.capacityForOneMore(context, currentStrategy.capacityNode().execute(store));
201+
int currentCapacity = currentStrategy.capacityNode().execute(store);
202+
final int neededCapacity;
203+
if (index >= currentCapacity) {
204+
neededCapacity = ArrayUtils.capacityForOneMore(context, currentCapacity);
205+
} else {
206+
neededCapacity = currentCapacity;
207+
}
202208

203209
replaceNodes(generalized, neededCapacity);
204210

0 commit comments

Comments
 (0)