Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2cf00a0 authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

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
parent 452ae34a
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -81,13 +81,18 @@ public class TimeSparseArray<E> extends LongSparseArray<E> {
    @Override
    public void put(long key, E value) {
        final long origKey = key;
        while (indexOfKey(key) >= 0) {
        int keyIndex = indexOfKey(key);
        if (keyIndex >= 0) {
            final long sz = size();
            while (keyIndex < sz && keyAt(keyIndex) == key) {
                key++;
                keyIndex++;
            }
        if (origKey != key) {
            if (key >= origKey + 10) {
                Slog.w(TAG, "Value " + value + " supposed to be inserted at " + origKey
                        + " displaced to " + key);
            }
        }
        super.put(key, value);
    }