From d5c4c550e414c0218f9f2c4a44b6fde3f8bb0c0e Mon Sep 17 00:00:00 2001 From: Songchun Fan Date: Thu, 5 May 2022 19:13:31 +0000 Subject: [PATCH] [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 --- .../core/java/com/android/server/utils/WatchedArrayList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/utils/WatchedArrayList.java b/services/core/java/com/android/server/utils/WatchedArrayList.java index 6059f9675e343..75e39ebb32d86 100644 --- a/services/core/java/com/android/server/utils/WatchedArrayList.java +++ b/services/core/java/com/android/server/utils/WatchedArrayList.java @@ -416,7 +416,7 @@ public class WatchedArrayList 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(); }