From a2cf00a011e634f98b7e86ee025bfe489ec15f4f Mon Sep 17 00:00:00 2001 From: Suprabh Shukla Date: Tue, 27 Mar 2018 15:39:52 -0700 Subject: [PATCH] Optimizing TimeSparseArray#put in case of collisions Adding a linear check instead of binary searching everytime. Logging only when the displacement is greater than 10 millis. Test: python system/extras/boottime_tools/bootanalyze/bootanalyze.py\ -r -c system/extras/boottime_tools/bootanalyze/config.yaml -n 10 and atest android.app.usage.TimeSparseArrayTest Bug: 76435713 Change-Id: I8f4df59e84fc196d0f63f9433d01ebc759f104c4 --- .../java/android/app/usage/TimeSparseArray.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/java/android/app/usage/TimeSparseArray.java b/core/java/android/app/usage/TimeSparseArray.java index 5764fa85579c6..9ef88e418161e 100644 --- a/core/java/android/app/usage/TimeSparseArray.java +++ b/core/java/android/app/usage/TimeSparseArray.java @@ -81,12 +81,17 @@ public class TimeSparseArray extends LongSparseArray { @Override public void put(long key, E value) { final long origKey = key; - while (indexOfKey(key) >= 0) { - key++; - } - if (origKey != key) { - Slog.w(TAG, "Value " + value + " supposed to be inserted at " + origKey - + " displaced to " + key); + int keyIndex = indexOfKey(key); + if (keyIndex >= 0) { + final long sz = size(); + while (keyIndex < sz && keyAt(keyIndex) == key) { + key++; + keyIndex++; + } + if (key >= origKey + 10) { + Slog.w(TAG, "Value " + value + " supposed to be inserted at " + origKey + + " displaced to " + key); + } } super.put(key, value); }