Replace List<Integer> 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
This commit is contained in:
@@ -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<Integer> 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<Integer> 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) {
|
||||
|
||||
@@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> timeInStateFrequencyFormat = new ArrayList<>();
|
||||
ArrayList<Integer> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,6 +310,10 @@ public class ArrayUtils {
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@code IntArray} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public static int[] convertToIntArray(List<Integer> list) {
|
||||
int[] array = new int[list.size()];
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
|
||||
@@ -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<Integer> 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*/);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user