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
This commit is contained in:
Suprabh Shukla
2018-03-27 15:39:52 -07:00
parent 452ae34af5
commit a2cf00a011

View File

@@ -81,12 +81,17 @@ public class TimeSparseArray<E> extends LongSparseArray<E> {
@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);
}