Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4457de2

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/corelib] Shrink code size of inlined _GrowableList.add
Previously, _GrowableList.add used 2 calls to grow the list which results in extra code generated for each inlined List.add call. This change replaces _grow(_nextCapacity(len)); with _growToNextCapacity(); which reduces code size. Flutter gallery in release mode instructions size -0.32% (arm), -0.1% (arm64) total snapshot size -0.19% (arm) -0.06% (arm64) Change-Id: I8422757d147ecec5bdec60910c65c558c39fde1d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175006 Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
1 parent f30675a commit 4457de2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

sdk/lib/_internal/vm/lib/growable_array.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class _GrowableList<T> extends ListBase<T> {
261261
void add(T value) {
262262
var len = length;
263263
if (len == _capacity) {
264-
_grow(_nextCapacity(len));
264+
_growToNextCapacity();
265265
}
266266
_setLength(len + 1);
267267
this[len] = value;
@@ -310,7 +310,7 @@ class _GrowableList<T> extends ListBase<T> {
310310
if (this.length != newLen) throw new ConcurrentModificationError(this);
311311
len = newLen;
312312
}
313-
_grow(_nextCapacity(_capacity));
313+
_growToNextCapacity();
314314
} while (true);
315315
}
316316

@@ -370,6 +370,14 @@ class _GrowableList<T> extends ListBase<T> {
370370
_setData(newData);
371371
}
372372

373+
// This method is marked as never-inline to conserve code size.
374+
// It is called in rare cases, but used in the add() which is
375+
// used very often and always inlined.
376+
@pragma("vm:never-inline")
377+
void _growToNextCapacity() {
378+
_grow(_nextCapacity(_capacity));
379+
}
380+
373381
void _shrink(int new_capacity, int new_length) {
374382
var newData = _allocateData(new_capacity);
375383
// This is a work-around for dartbug.com/30090. See the comment in _grow.

0 commit comments

Comments
 (0)