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

Commit 9b6454d3 authored by Yisroel Forta's avatar Yisroel Forta
Browse files

Keeping start info records sorted

The documentation states that records should be sorted, but we do
some additional work as if it was not sorted. Update logic to both
ensure that the list stays sorted, and reduce the unnecessary
sorting.

Test: run appstartinfo unit tests which cover order with flag enabled and disabled
Bug: 384539178
Flag: android.app.app_start_info_keep_records_sorted
Change-Id: I34637a6f4fddf7e86f4b87dfde062478a9036057
parent 5ed56aac
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -180,3 +180,13 @@ flag {
         purpose: PURPOSE_BUGFIX
     }
}

flag {
     namespace: "system_performance"
     name: "app_start_info_keep_records_sorted"
     description: "Ensure records are kept sorted to avoid extra work"
     bug: "384539178"
     metadata {
         purpose: PURPOSE_BUGFIX
     }
}
+37 −21
Original line number Diff line number Diff line
@@ -1240,7 +1240,7 @@ public final class AppStartInfoTracker {

    /** A container class of (@link android.app.ApplicationStartInfo) */
    final class AppStartInfoContainer {
        private ArrayList<ApplicationStartInfo> mInfos; // Always kept sorted by first timestamp.
        private ArrayList<ApplicationStartInfo> mInfos; // Always kept sorted by monotonic time.
        private int mMaxCapacity;
        private int mUid;
        private boolean mMonitoringModeEnabled = false;
@@ -1271,9 +1271,11 @@ public final class AppStartInfoTracker {
                return;
            }

            if (!android.app.Flags.appStartInfoKeepRecordsSorted()) {
                // Sort records so we can remove the least recent ones.
                Collections.sort(mInfos, (a, b) ->
                        Long.compare(b.getMonoticCreationTimeMs(), a.getMonoticCreationTimeMs()));
            }

            // Remove records and trim list object back to size.
            mInfos.subList(0, mInfos.size() - getMaxCapacity()).clear();
@@ -1288,6 +1290,13 @@ public final class AppStartInfoTracker {

        @GuardedBy("mLock")
        void addStartInfoLocked(ApplicationStartInfo info) {
            if (android.app.Flags.appStartInfoKeepRecordsSorted()) {
                while (mInfos.size() >= getMaxCapacity()) {
                    // Expected to execute at most once.
                    mInfos.removeLast();
                }
                mInfos.addFirst(info);
            } else {
                int size = mInfos.size();
                if (size >= getMaxCapacity()) {
                    // Remove oldest record if size is over max capacity.
@@ -1308,6 +1317,7 @@ public final class AppStartInfoTracker {
                Collections.sort(mInfos, (a, b) ->
                        Long.compare(b.getMonoticCreationTimeMs(), a.getMonoticCreationTimeMs()));
            }
        }

        /**
         * Add the provided key/timestamp to the most recent start record, if it is currently
@@ -1493,7 +1503,13 @@ public final class AppStartInfoTracker {
                        info.readFromProto(proto, AppsStartInfoProto.Package.User.APP_START_INFO,
                                byteArrayInputStream, objectInputStream, typedXmlPullParser);
                        info.setPackageName(packageName);
                        if (android.app.Flags.appStartInfoKeepRecordsSorted()) {
                            // Since the writes are done from oldest to newest, each additional
                            // record will be newer than the previous so use addFirst.
                            mInfos.addFirst(info);
                        } else {
                            mInfos.add(info);
                        }
                        break;
                    case (int) AppsStartInfoProto.Package.User.MONITORING_ENABLED:
                        mMonitoringModeEnabled = proto.readBoolean(