Skip to content

Commit 8f17eee

Browse files
committed
fix: merge conflicts
2 parents c49dfb5 + 062af5f commit 8f17eee

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.github.packageurl.internal;
2323

2424
import java.nio.charset.StandardCharsets;
25+
import java.util.Locale;
2526
import java.util.Random;
2627
import java.util.concurrent.TimeUnit;
2728
import org.openjdk.jmh.annotations.Benchmark;
@@ -30,7 +31,6 @@
3031
import org.openjdk.jmh.annotations.OutputTimeUnit;
3132
import org.openjdk.jmh.annotations.Param;
3233
import org.openjdk.jmh.annotations.Scope;
33-
import org.openjdk.jmh.annotations.Setup;
3434
import org.openjdk.jmh.annotations.State;
3535
import org.openjdk.jmh.infra.Blackhole;
3636

@@ -62,14 +62,8 @@ public class StringUtilBenchmark {
6262
@Param({"0", "0.1", "0.5"})
6363
private double nonAsciiProb;
6464

65-
private String[] decodedData = createDecodedData();
66-
private String[] encodedData = encodeData(decodedData);
67-
68-
@Setup
69-
public void setup() {
70-
decodedData = createDecodedData();
71-
encodedData = encodeData(encodedData);
72-
}
65+
private final String[] decodedData = createDecodedData();
66+
private final String[] encodedData = encodeData(decodedData);
7367

7468
private String[] createDecodedData() {
7569
Random random = new Random();
@@ -90,8 +84,11 @@ private String[] createDecodedData() {
9084

9185
private static String[] encodeData(String[] decodedData) {
9286
String[] encodedData = new String[decodedData.length];
93-
for (int i = 0; i < decodedData.length; i++) {
87+
for (int i = 0; i < encodedData.length; i++) {
9488
encodedData[i] = StringUtil.percentEncode(decodedData[i]);
89+
if (!StringUtil.percentDecode(encodedData[i]).equals(decodedData[i])) {
90+
throw new RuntimeException("Invalid implementation of `percentEncode` and `percentDecode`.");
91+
}
9592
}
9693
return encodedData;
9794
}
@@ -100,17 +97,28 @@ private static String[] encodeData(String[] decodedData) {
10097
public void baseline(Blackhole blackhole) {
10198
for (int i = 0; i < DATA_COUNT; i++) {
10299
byte[] buffer = decodedData[i].getBytes(StandardCharsets.UTF_8);
103-
// Change the String a little bit
100+
// Prevent JIT compiler from assuming the buffer was not modified
104101
for (int idx = 0; idx < buffer.length; idx++) {
105-
byte b = buffer[idx];
106-
if ('a' <= b && b <= 'z') {
107-
buffer[idx] = (byte) (b & 0x20);
108-
}
102+
buffer[idx] ^= 0x20;
109103
}
110104
blackhole.consume(new String(buffer, StandardCharsets.UTF_8));
111105
}
112106
}
113107

108+
@Benchmark
109+
public void toLowerCaseJre(Blackhole blackhole) {
110+
for (int i = 0; i < DATA_COUNT; i++) {
111+
blackhole.consume(decodedData[i].toLowerCase(Locale.ROOT));
112+
}
113+
}
114+
115+
@Benchmark
116+
public void toLowerCase(Blackhole blackhole) {
117+
for (int i = 0; i < DATA_COUNT; i++) {
118+
blackhole.consume(StringUtil.toLowerCase(decodedData[i]));
119+
}
120+
}
121+
114122
@Benchmark
115123
public void percentDecode(final Blackhole blackhole) {
116124
for (int i = 0; i < DATA_COUNT; i++) {

0 commit comments

Comments
 (0)