Skip to content

Commit d4d12ad

Browse files
committed
8282047: Enhance StringDecode/Encode microbenchmarks
Reviewed-by: bchristi
1 parent 22b93a3 commit d4d12ad

File tree

2 files changed

+287
-140
lines changed

2 files changed

+287
-140
lines changed
Lines changed: 130 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,46 +22,38 @@
2222
*/
2323
package org.openjdk.bench.java.lang;
2424

25-
import org.openjdk.jmh.annotations.Benchmark;
26-
import org.openjdk.jmh.annotations.BenchmarkMode;
27-
import org.openjdk.jmh.annotations.Fork;
28-
import org.openjdk.jmh.annotations.Measurement;
29-
import org.openjdk.jmh.annotations.Mode;
30-
import org.openjdk.jmh.annotations.OutputTimeUnit;
31-
import org.openjdk.jmh.annotations.Param;
32-
import org.openjdk.jmh.annotations.Scope;
33-
import org.openjdk.jmh.annotations.Setup;
34-
import org.openjdk.jmh.annotations.State;
35-
import org.openjdk.jmh.annotations.Warmup;
25+
import org.openjdk.jmh.annotations.*;
3626
import org.openjdk.jmh.infra.Blackhole;
3727

3828
import java.nio.charset.Charset;
3929
import java.util.concurrent.TimeUnit;
4030

4131
@BenchmarkMode(Mode.AverageTime)
4232
@OutputTimeUnit(TimeUnit.NANOSECONDS)
43-
@Fork(value = 3)
44-
@Warmup(iterations = 5, time = 2)
33+
@Fork(value = 2)
34+
@Warmup(iterations = 5, time = 3)
4535
@Measurement(iterations = 5, time = 3)
4636
@State(Scope.Thread)
4737
public class StringDecode {
4838

49-
@Param({"US-ASCII", "ISO-8859-1", "UTF-8", "MS932", "ISO-8859-6", "ISO-2022-KR"})
39+
// Reduced by default to only UTF-8, previous coverage:
40+
// @Param({"US-ASCII", "ISO-8859-1", "UTF-8", "MS932", "ISO-8859-6", "ISO-2022-KR"})
41+
@Param({"UTF-8"})
5042
private String charsetName;
5143

5244
private Charset charset;
5345
private byte[] asciiString;
46+
private byte[] longAsciiString;
5447
private byte[] utf16String;
55-
private byte[] longUtf16String;
48+
private byte[] longUtf16EndString;
5649
private byte[] longUtf16StartString;
57-
private byte[] longLatin1String;
50+
private byte[] longUtf16OnlyString;
51+
private byte[] latin1String;
52+
private byte[] longLatin1EndString;
53+
private byte[] longLatin1StartString;
54+
private byte[] longLatin1OnlyString;
5855

59-
@Setup
60-
public void setup() {
61-
charset = Charset.forName(charsetName);
62-
asciiString = "ascii string".getBytes(charset);
63-
utf16String = "UTF-\uFF11\uFF16 string".getBytes(charset);
64-
longUtf16String = """
56+
private static final String LOREM = """
6557
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac sem eu
6658
urna egestas placerat. Etiam finibus ipsum nulla, non mattis dolor cursus a.
6759
Nulla nec nisl consectetur, lacinia neque id, accumsan ante. Curabitur et
@@ -70,90 +62,145 @@ public void setup() {
7062
per conubia nostra, per inceptos himenaeos. Suspendisse erat diam, fringilla
7163
sed massa sed, posuere viverra orci. Suspendisse tempor libero non gravida
7264
efficitur. Vivamus lacinia risus non orci viverra, at consectetur odio laoreet.
73-
Suspendisse potenti.
74-
75-
Phasellus vel nisi iaculis, accumsan quam sed, bibendum eros. Sed venenatis
76-
nulla tortor, et eleifend urna sodales id. Nullam tempus ac metus sit amet
77-
sollicitudin. Nam sed ex diam. Praesent vitae eros et neque condimentum
78-
consectetur eget non tortor. Praesent bibendum vel felis nec dignissim.
79-
Maecenas a enim diam. Suspendisse quis ligula at nisi accumsan lacinia id
80-
hendrerit sapien. \uFF11Donec aliquam mattis lectus eu ultrices. Duis eu nisl\uFF11
81-
euismod, blandit mauris vel, \uFF11placerat urna. Etiam malesuada enim purus,
82-
tristique mollis odio blandit quis.\uFF11 Vivamus posuere. \uFF11
83-
\uFF11
84-
""".getBytes(charset);
85-
longUtf16StartString = """
86-
\uFF11
87-
Lorem ipsum dolor sit amet, \uFF11consectetur adipiscing elit. Aliquam ac sem eu
88-
urna egestas \uFF11placerat. Etiam finibus ipsum nulla, non mattis dolor cursus a.
89-
Nulla \uFF11nec nisl consectetur, lacinia neque id, accumsan ante. Curabitur et
90-
sapien in \uFF11magna porta ultricies. \uFF11Sed vel pellentesque nibh. Pellentesque dictum
91-
dignissim diam eu ultricies. Class aptent taciti sociosqu ad litora torquent
92-
per conubia nostra, per inceptos himenaeos. Suspendisse erat diam, fringilla
93-
sed massa sed, posuere viverra orci. Suspendisse tempor libero non gravida
94-
efficitur. Vivamus lacinia risus non orci viverra, at consectetur odio laoreet.
95-
Suspendisse potenti.
96-
97-
Phasellus vel nisi iaculis, accumsan quam sed, bibendum eros. Sed venenatis
98-
nulla tortor, et eleifend urna sodales id. Nullam tempus ac metus sit amet
99-
sollicitudin. Nam sed ex diam. Praesent vitae eros et neque condimentum
100-
consectetur eget non tortor. Praesent bibendum vel felis nec dignissim.
101-
Maecenas a enim diam. Suspendisse quis ligula at nisi accumsan lacinia id
102-
hendrerit sapien. Donec aliquam mattis lectus eu ultrices. Duis eu nisl
103-
euismod, blandit mauris vel, placerat urna. Etiam malesuada enim purus,
104-
tristique mollis odio blandit quis. Vivamus posuere.
105-
""".getBytes(charset);
106-
107-
longLatin1String = """
108-
a\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
109-
b\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
110-
c\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
111-
d\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
112-
e\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
113-
f\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
114-
g\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
115-
h\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
116-
i\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6
117-
j\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6\u00F6
118-
k\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6\u00F6
119-
l\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6\u00F6
120-
m\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00F6\u00B6\u00F6\u00F6
121-
""".getBytes(charset);
65+
Suspendisse potenti.""";
66+
private static final String UTF16_STRING = "\uFF11".repeat(31);
67+
private static final String LATIN1_STRING = "\u00B6".repeat(31);
68+
69+
@Setup
70+
public void setup() {
71+
charset = Charset.forName(charsetName);
72+
asciiString = LOREM.substring(0, 32).getBytes(charset);
73+
longAsciiString = LOREM.repeat(200).getBytes(charset);
74+
utf16String = "UTF-\uFF11\uFF16 string".getBytes(charset);
75+
longUtf16EndString = LOREM.repeat(4).concat(UTF16_STRING).getBytes(charset);
76+
longUtf16StartString = UTF16_STRING.concat(LOREM.repeat(4)).getBytes(charset);
77+
longUtf16OnlyString = UTF16_STRING.repeat(10).getBytes(charset);
78+
latin1String = LATIN1_STRING.getBytes(charset);
79+
longLatin1EndString = LOREM.repeat(4).concat(LATIN1_STRING).getBytes(charset);
80+
longLatin1StartString = LATIN1_STRING.concat(LOREM.repeat(4)).getBytes(charset);
81+
longLatin1OnlyString = LATIN1_STRING.repeat(10).getBytes(charset);
12282
}
12383

12484
@Benchmark
125-
public String decodeAsciiCharsetName() throws Exception {
126-
return new String(asciiString, charsetName);
85+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
86+
public void decodeAsciiShort(Blackhole bh) throws Exception {
87+
bh.consume(new String(asciiString, charset));
88+
bh.consume(new String(longAsciiString, 0, 15, charset));
89+
bh.consume(new String(asciiString, 0, 3, charset));
90+
bh.consume(new String(longAsciiString, 512, 512 + 7, charset));
12791
}
12892

12993
@Benchmark
130-
public String decodeAscii() throws Exception {
131-
return new String(asciiString, charset);
94+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
95+
public void decodeAsciiLong(Blackhole bh) throws Exception {
96+
bh.consume(new String(longAsciiString, charset));
97+
bh.consume(new String(longAsciiString, 0, 1024 + 31, charset));
13298
}
13399

134100
@Benchmark
135-
public String decodeLatin1Long() throws Exception {
136-
return new String(longLatin1String, charset);
101+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
102+
public void decodeLatin1Short(Blackhole bh) throws Exception {
103+
bh.consume(new String(latin1String, charset));
104+
bh.consume(new String(latin1String, 0, 15, charset));
105+
bh.consume(new String(latin1String, 0, 3, charset));
106+
bh.consume(new String(longLatin1OnlyString, 512, 512 + 7, charset));
137107
}
138108

139109
@Benchmark
140-
public String decodeUTF16Short() throws Exception {
141-
return new String(utf16String, charset);
110+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
111+
public String decodeLatin1LongStart() throws Exception {
112+
return new String(longLatin1StartString, charset);
142113
}
143114

144115
@Benchmark
116+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
117+
public String decodeLatin1LongEnd() throws Exception {
118+
return new String(longLatin1EndString, charset);
119+
}
120+
121+
@Benchmark
122+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
123+
public String decodeLatin1LongOnly() throws Exception {
124+
return new String(longLatin1OnlyString, charset);
125+
}
126+
127+
@Benchmark
128+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
129+
public void decodeLatin1Mixed(Blackhole bh) throws Exception {
130+
bh.consume(new String(longLatin1EndString, charset));
131+
bh.consume(new String(longLatin1StartString, charset));
132+
bh.consume(new String(latin1String, charset));
133+
bh.consume(new String(longLatin1OnlyString, charset));
134+
}
135+
136+
@Benchmark
137+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
138+
public void decodeUTF16Short(Blackhole bh) throws Exception {
139+
bh.consume(new String(utf16String, charset));
140+
bh.consume(new String(utf16String, 0, 15, charset));
141+
bh.consume(new String(utf16String, 0, 3, charset));
142+
bh.consume(new String(utf16String, 0, 7, charset));
143+
}
144+
145+
@Benchmark
146+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
145147
public String decodeUTF16LongEnd() throws Exception {
146-
return new String(longUtf16String, charset);
148+
return new String(longUtf16EndString, charset);
147149
}
148150

149151
@Benchmark
152+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
150153
public String decodeUTF16LongStart() throws Exception {
151154
return new String(longUtf16StartString, charset);
152155
}
153156

154157
@Benchmark
155-
public void decodeUTF16LongMixed(Blackhole bh) throws Exception {
158+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
159+
public String decodeUTF16LongOnly() throws Exception {
160+
return new String(longUtf16OnlyString, charset);
161+
}
162+
163+
@Benchmark
164+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
165+
public void decodeUTF16Mixed(Blackhole bh) throws Exception {
166+
bh.consume(new String(longUtf16StartString, charset));
167+
bh.consume(new String(longUtf16EndString, charset));
168+
bh.consume(new String(utf16String, charset));
169+
bh.consume(new String(longUtf16OnlyString, charset));
170+
}
171+
172+
@Benchmark
173+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
174+
public void decodeAllMixed(Blackhole bh) throws Exception {
175+
bh.consume(new String(utf16String, charset));
176+
bh.consume(new String(longUtf16EndString, charset));
177+
bh.consume(new String(utf16String, 0, 15, charset));
156178
bh.consume(new String(longUtf16StartString, charset));
157-
bh.consume(new String(longUtf16String, charset));
179+
bh.consume(new String(asciiString, 0, 3, charset));
180+
bh.consume(new String(longUtf16OnlyString, charset));
181+
bh.consume(new String(latin1String, charset));
182+
bh.consume(new String(longLatin1EndString, charset));
183+
bh.consume(new String(longLatin1StartString, charset));
184+
bh.consume(new String(latin1String, 0, 7, charset));
185+
bh.consume(new String(longLatin1OnlyString, charset));
186+
bh.consume(new String(asciiString, charset));
187+
bh.consume(new String(longAsciiString, charset));
188+
}
189+
190+
@Benchmark
191+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
192+
public void decodeShortMixed(Blackhole bh) throws Exception {
193+
bh.consume(new String(utf16String, 0, 15, charset));
194+
bh.consume(new String(latin1String, 0, 15, charset));
195+
bh.consume(new String(asciiString, charset));
196+
bh.consume(new String(utf16String, charset));
197+
bh.consume(new String(latin1String, 0, 3, charset));
198+
bh.consume(new String(asciiString, 0, 3, charset));
199+
bh.consume(new String(utf16String, 0, 7, charset));
200+
bh.consume(new String(latin1String, charset));
201+
bh.consume(new String(asciiString, 0, 7, charset));
202+
bh.consume(new String(utf16String, 0, 3, charset));
203+
bh.consume(new String(latin1String, 0, 7, charset));
204+
bh.consume(new String(asciiString, 0, 15, charset));
158205
}
159206
}

0 commit comments

Comments
 (0)