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

Commit 72754e58 authored by Kweku Adams's avatar Kweku Adams
Browse files

Address small inefficiencies.

1. Don't hold the lock for validation checks that don't need it.
2. Put early returns ahead of other work when possible.

Bug: 203811598
Test: atest frameworks/base/services/tests/servicestests/src/com/android/server/usage
Test: atest frameworks/base/services/tests/mockingservicestests/src/com/android/server/usage
Change-Id: Ic923db1534e27e0932e594b8259fe482772b8a6a
parent 0c6c7812
Loading
Loading
Loading
Loading
+21 −20
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.usage;

import android.annotation.Nullable;
import android.app.usage.TimeSparseArray;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
@@ -788,7 +789,7 @@ public class UsageStatsDatabase {
    public interface StatCombiner<T> {

        /**
         * Implementations should extract interesting from <code>stats</code> and add it
         * Implementations should extract interesting information from <code>stats</code> and add it
         * to the <code>accumulatedResult</code> list.
         *
         * If the <code>stats</code> object is mutable, <code>mutable</code> will be true,
@@ -805,15 +806,14 @@ public class UsageStatsDatabase {
    /**
     * Find all {@link IntervalStats} for the given range and interval type.
     */
    @Nullable
    public <T> List<T> queryUsageStats(int intervalType, long beginTime, long endTime,
            StatCombiner<T> combiner) {
        synchronized (mLock) {
        // mIntervalDirs is final. Accessing its size without holding the lock should be fine.
        if (intervalType < 0 || intervalType >= mIntervalDirs.length) {
            throw new IllegalArgumentException("Bad interval type " + intervalType);
        }

            final TimeSparseArray<AtomicFile> intervalStats = mSortedStatFiles[intervalType];

        if (endTime <= beginTime) {
            if (DEBUG) {
                Slog.d(TAG, "endTime(" + endTime + ") <= beginTime(" + beginTime + ")");
@@ -821,12 +821,8 @@ public class UsageStatsDatabase {
            return null;
        }

            int startIndex = intervalStats.closestIndexOnOrBefore(beginTime);
            if (startIndex < 0) {
                // All the stats available have timestamps after beginTime, which means they all
                // match.
                startIndex = 0;
            }
        synchronized (mLock) {
            final TimeSparseArray<AtomicFile> intervalStats = mSortedStatFiles[intervalType];

            int endIndex = intervalStats.closestIndexOnOrBefore(endTime);
            if (endIndex < 0) {
@@ -849,6 +845,13 @@ public class UsageStatsDatabase {
                }
            }

            int startIndex = intervalStats.closestIndexOnOrBefore(beginTime);
            if (startIndex < 0) {
                // All the stats available have timestamps after beginTime, which means they all
                // match.
                startIndex = 0;
            }

            final ArrayList<T> results = new ArrayList<>();
            for (int i = startIndex; i <= endIndex; i++) {
                final AtomicFile f = intervalStats.valueAt(i);
@@ -985,7 +988,6 @@ public class UsageStatsDatabase {
        }
    }


    private static long parseBeginTime(AtomicFile file) throws IOException {
        return parseBeginTime(file.getBaseFile());
    }
@@ -1233,7 +1235,6 @@ public class UsageStatsDatabase {
        }
    }


    /* Backup/Restore Code */
    byte[] getBackupPayload(String key) {
        return getBackupPayload(key, BACKUP_VERSION);