From 345f5c7bec040e39ed22ecd00a4707ab2ef65a64 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Mon, 5 Oct 2020 21:19:04 -0600 Subject: Replace List with faster IntArray. These were uncovered by the recently added EfficientCollections Error Prone detector, and several of them are in hot codepaths. Bug: 155703208 Test: make Change-Id: I4cff2d6b846edc9a7482d21a803117132cbcdadf --- .../android/internal/os/KernelCpuThreadReader.java | 10 ++++---- .../android/internal/os/ProcTimeInStateReader.java | 28 ++++++++++------------ .../java/com/android/internal/util/ArrayUtils.java | 4 ++++ .../server/pm/PackageManagerShellCommand.java | 5 ++-- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/core/java/com/android/internal/os/KernelCpuThreadReader.java b/core/java/com/android/internal/os/KernelCpuThreadReader.java index 2ba372a47cf3..0843741d237c 100644 --- a/core/java/com/android/internal/os/KernelCpuThreadReader.java +++ b/core/java/com/android/internal/os/KernelCpuThreadReader.java @@ -18,10 +18,10 @@ package com.android.internal.os; import android.annotation.Nullable; import android.os.Process; +import android.util.IntArray; import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; -import com.android.internal.util.ArrayUtils; import com.android.internal.util.Preconditions; import java.io.IOException; @@ -456,14 +456,14 @@ public class KernelCpuThreadReader { * cluster has started. */ private static int[] getClusterStartIndices(long[] frequencies) { - ArrayList indices = new ArrayList<>(); + IntArray indices = new IntArray(); indices.add(0); for (int i = 0; i < frequencies.length - 1; i++) { if (frequencies[i] >= frequencies[i + 1]) { indices.add(i + 1); } } - return ArrayUtils.convertToIntArray(indices); + return indices.toArray(); } /** Get the index in frequencies where each bucket starts */ @@ -477,7 +477,7 @@ public class KernelCpuThreadReader { return Arrays.copyOfRange(clusterStartIndices, 0, targetNumBuckets); } - ArrayList bucketStartIndices = new ArrayList<>(); + IntArray bucketStartIndices = new IntArray(); for (int clusterIdx = 0; clusterIdx < numClusters; clusterIdx++) { final int clusterStartIdx = getLowerBound(clusterIdx, clusterStartIndices); final int clusterEndIdx = @@ -509,7 +509,7 @@ public class KernelCpuThreadReader { bucketStartIndices.add(bucketStartIdx); } } - return ArrayUtils.convertToIntArray(bucketStartIndices); + return bucketStartIndices.toArray(); } private static int getLowerBound(int index, int[] startIndices) { diff --git a/core/java/com/android/internal/os/ProcTimeInStateReader.java b/core/java/com/android/internal/os/ProcTimeInStateReader.java index 2318473354e2..d54a9c7f3a46 100644 --- a/core/java/com/android/internal/os/ProcTimeInStateReader.java +++ b/core/java/com/android/internal/os/ProcTimeInStateReader.java @@ -18,16 +18,11 @@ package com.android.internal.os; import android.annotation.Nullable; import android.os.Process; - -import com.android.internal.util.ArrayUtils; +import android.util.IntArray; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; /** * Reads and parses {@code time_in_state} files in the {@code proc} filesystem. @@ -68,24 +63,25 @@ public class ProcTimeInStateReader { * The format of a single line of the {@code time_in_state} file that exports the frequency * values */ - private static final List TIME_IN_STATE_LINE_FREQUENCY_FORMAT = Arrays.asList( + private static final int[] TIME_IN_STATE_LINE_FREQUENCY_FORMAT = new int[] { Process.PROC_OUT_LONG | Process.PROC_SPACE_TERM, Process.PROC_NEWLINE_TERM - ); + }; /** * The format of a single line of the {@code time_in_state} file that exports the time values */ - private static final List TIME_IN_STATE_LINE_TIME_FORMAT = Arrays.asList( + private static final int[] TIME_IN_STATE_LINE_TIME_FORMAT = new int[] { Process.PROC_SPACE_TERM, Process.PROC_OUT_LONG | Process.PROC_NEWLINE_TERM - ); + }; /** * The format of a header line of the {@code time_in_state} file */ - private static final List TIME_IN_STATE_HEADER_LINE_FORMAT = - Collections.singletonList(Process.PROC_NEWLINE_TERM); + private static final int[] TIME_IN_STATE_HEADER_LINE_FORMAT = new int[] { + Process.PROC_NEWLINE_TERM + }; /** * The format of the {@code time_in_state} file to extract times, defined using {@link @@ -166,8 +162,8 @@ public class ProcTimeInStateReader { // formats. These formats are used to extract either the frequencies or the times from a // time_in_state file // Also check if each line is a header, and handle this in the created format arrays - ArrayList timeInStateFrequencyFormat = new ArrayList<>(); - ArrayList timeInStateTimeFormat = new ArrayList<>(); + IntArray timeInStateFrequencyFormat = new IntArray(); + IntArray timeInStateTimeFormat = new IntArray(); int numFrequencies = 0; for (int i = 0; i < timeInStateBytes.length; i++) { // If the first character of the line is not a digit, we treat it as a header @@ -194,12 +190,12 @@ public class ProcTimeInStateReader { final long[] readLongs = new long[numFrequencies]; final boolean readSuccess = Process.parseProcLine( timeInStateBytes, 0, timeInStateBytes.length, - ArrayUtils.convertToIntArray(timeInStateFrequencyFormat), null, readLongs, null); + timeInStateFrequencyFormat.toArray(), null, readLongs, null); if (!readSuccess) { throw new IOException("Failed to parse time_in_state file"); } - mTimeInStateTimeFormat = ArrayUtils.convertToIntArray(timeInStateTimeFormat); + mTimeInStateTimeFormat = timeInStateTimeFormat.toArray(); mFrequenciesKhz = readLongs; } } diff --git a/core/java/com/android/internal/util/ArrayUtils.java b/core/java/com/android/internal/util/ArrayUtils.java index cd69a02d4d2d..7cb19dceed95 100644 --- a/core/java/com/android/internal/util/ArrayUtils.java +++ b/core/java/com/android/internal/util/ArrayUtils.java @@ -310,6 +310,10 @@ public class ArrayUtils { return total; } + /** + * @deprecated use {@code IntArray} instead + */ + @Deprecated public static int[] convertToIntArray(List list) { int[] array = new int[list.size()]; for (int i = 0; i < list.size(); i++) { diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java index afbf7d3a35af..c356fc72379f 100644 --- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java +++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java @@ -96,6 +96,7 @@ import android.system.Os; import android.text.TextUtils; import android.text.format.DateUtils; import android.util.ArraySet; +import android.util.IntArray; import android.util.PrintWriterPrinter; import android.util.Slog; import android.util.SparseArray; @@ -1450,7 +1451,7 @@ class PackageManagerShellCommand extends ShellCommand { final PrintWriter pw = getOutPrintWriter(); final int parentSessionId = Integer.parseInt(getNextArg()); - List otherSessionIds = new ArrayList<>(); + IntArray otherSessionIds = new IntArray(); String opt; while ((opt = getNextArg()) != null) { otherSessionIds.add(Integer.parseInt(opt)); @@ -1459,7 +1460,7 @@ class PackageManagerShellCommand extends ShellCommand { pw.println("Error: At least two sessions are required."); return 1; } - return doInstallAddSession(parentSessionId, ArrayUtils.convertToIntArray(otherSessionIds), + return doInstallAddSession(parentSessionId, otherSessionIds.toArray(), true /*logSuccess*/); } -- cgit v1.2.3-59-g8ed1b