[snapshot] optimizing WatchedArrayList snapshot

ArrayList.add(index, value) does an array copy everytime to extend the
underlying array. Here we already knew the size of that array and
ensured the capacity so we should only call ArrayList.add(value)
which does not involve array copying.

We need to examine other Watched* classes and try to spot performance
issues like this.

BUG: 231373522
Test: forrest run
Change-Id: Iab440fc809072e9b1ee1bef7d69a0dc04207bafb
This commit is contained in:
Songchun Fan
2022-05-05 19:13:31 +00:00
parent 455e490d94
commit d5c4c550e4

View File

@@ -416,7 +416,7 @@ public class WatchedArrayList<E> extends WatchableImpl
dst.mStorage.ensureCapacity(end);
for (int i = 0; i < end; i++) {
final E val = Snapshots.maybeSnapshot(src.get(i));
dst.add(i, val);
dst.add(val);
}
dst.seal();
}