Avoid overallocating array size.

Since namespaces can just be a subset of jobs, don't allocate the
ArrayList to fit all jobs of the app.

Bug: 141645789
Test: atest CtsJobSchedulerTestCases
Change-Id: I801ee2b47b1735f7994c3c121da5aba34ede4b3d
This commit is contained in:
Kweku Adams
2023-04-11 16:46:58 +00:00
parent 9d1c928132
commit 3a5da035d0

View File

@@ -1588,12 +1588,12 @@ public class JobSchedulerService extends com.android.server.SystemService
final ArrayMap<String, List<JobInfo>> outMap = new ArrayMap<>();
synchronized (mLock) {
ArraySet<JobStatus> jobs = mJobs.getJobsByUid(uid);
// Write out for loop to avoid addAll() creating an Iterator.
// Write out for loop to avoid creating an Iterator.
for (int i = jobs.size() - 1; i >= 0; i--) {
final JobStatus job = jobs.valueAt(i);
List<JobInfo> outList = outMap.get(job.getNamespace());
if (outList == null) {
outList = new ArrayList<JobInfo>(jobs.size());
outList = new ArrayList<>();
outMap.put(job.getNamespace(), outList);
}
@@ -1606,7 +1606,7 @@ public class JobSchedulerService extends com.android.server.SystemService
private List<JobInfo> getPendingJobsInNamespace(int uid, @Nullable String namespace) {
synchronized (mLock) {
ArraySet<JobStatus> jobs = mJobs.getJobsByUid(uid);
ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size());
ArrayList<JobInfo> outList = new ArrayList<>();
// Write out for loop to avoid addAll() creating an Iterator.
for (int i = jobs.size() - 1; i >= 0; i--) {
final JobStatus job = jobs.valueAt(i);