Skip to content

Commit 7bfb05b

Browse files
committed
fix: Fix some issues with StringUtilBenchmark
* Make `Random` static even though in practice it should only be created once already (fixes SpotBugs warning) * Initialize fields to empty to prevent warning "`@NullMarked` fields must be initialized" * Iterate over `decodedData` directly instead of using the `DATA_COUNT` constant even though in practice these should always have the same length * Fill data fields directly instead of using a local variables/parameters with the same names
1 parent 86bebd8 commit 7bfb05b

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/test/java/com/github/packageurl/internal/StringUtilBenchmark.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -54,43 +54,45 @@
5454
@OutputTimeUnit(TimeUnit.MICROSECONDS)
5555
@State(Scope.Benchmark)
5656
public class StringUtilBenchmark {
57+
private static final Random RANDOM = new Random();
5758

5859
private static final int DATA_COUNT = 1000;
60+
5961
private static final int DECODED_LENGTH = 256;
62+
6063
private static final byte[] UNRESERVED =
6164
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~".getBytes(StandardCharsets.US_ASCII);
6265

6366
@Param({"0", "0.1", "0.5"})
6467
private double nonAsciiProb;
6568

66-
private String[] decodedData;
67-
private String[] encodedData;
69+
private String[] decodedData = {};
70+
71+
private String[] encodedData = {};
6872

6973
@Setup
7074
public void setup() {
71-
decodedData = createDecodedData();
72-
encodedData = encodeData(decodedData);
75+
createDecodedData();
76+
createEncodedData();
7377
}
7478

75-
private String[] createDecodedData() {
76-
Random random = new Random();
77-
String[] decodedData = new String[DATA_COUNT];
79+
private void createDecodedData() {
80+
decodedData = new String[DATA_COUNT];
7881
for (int i = 0; i < DATA_COUNT; i++) {
7982
char[] chars = new char[DECODED_LENGTH];
8083
for (int j = 0; j < DECODED_LENGTH; j++) {
81-
if (random.nextDouble() < nonAsciiProb) {
82-
chars[j] = (char) (Byte.MAX_VALUE + 1 + random.nextInt(Short.MAX_VALUE - Byte.MAX_VALUE - 1));
84+
if (RANDOM.nextDouble() < nonAsciiProb) {
85+
chars[j] = (char) (Byte.MAX_VALUE + 1 + RANDOM.nextInt(Short.MAX_VALUE - Byte.MAX_VALUE - 1));
8386
} else {
84-
chars[j] = (char) UNRESERVED[random.nextInt(UNRESERVED.length)];
87+
chars[j] = (char) UNRESERVED[RANDOM.nextInt(UNRESERVED.length)];
8588
}
8689
}
8790
decodedData[i] = new String(chars);
8891
}
89-
return decodedData;
9092
}
9193

92-
private static String[] encodeData(String[] decodedData) {
93-
String[] encodedData = new String[decodedData.length];
94+
private void createEncodedData() {
95+
encodedData = new String[decodedData.length];
9496
for (int i = 0; i < encodedData.length; i++) {
9597
encodedData[i] = StringUtil.percentEncode(decodedData[i]);
9698
if (!StringUtil.percentDecode(encodedData[i]).equals(decodedData[i])) {
@@ -100,13 +102,12 @@ private static String[] encodeData(String[] decodedData) {
100102
+ StringUtil.percentDecode(encodedData[i]));
101103
}
102104
}
103-
return encodedData;
104105
}
105106

106107
@Benchmark
107108
public void baseline(Blackhole blackhole) {
108-
for (int i = 0; i < DATA_COUNT; i++) {
109-
byte[] buffer = decodedData[i].getBytes(StandardCharsets.UTF_8);
109+
for (String decodedStr : decodedData) {
110+
byte[] buffer = decodedStr.getBytes(StandardCharsets.UTF_8);
110111
// Prevent JIT compiler from assuming the buffer was not modified
111112
for (int idx = 0; idx < buffer.length; idx++) {
112113
buffer[idx] ^= 0x20;

0 commit comments

Comments
 (0)