Skip to content

Commit e0cf023

Browse files
author
Julia Boes
committed
8245309: Re-examine use of ThreadLocalCoders in sun.net.www.ParseUtil
Reviewed-by: shade, dfuchs, alanb, chegar
1 parent 60c4902 commit e0cf023

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

src/java.base/share/classes/sun/net/www/ParseUtil.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2020, 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
@@ -34,10 +34,10 @@
3434
import java.nio.CharBuffer;
3535
import java.nio.charset.CharacterCodingException;
3636
import java.nio.charset.CharsetDecoder;
37+
import java.nio.charset.CharsetEncoder;
3738
import java.nio.charset.CoderResult;
3839
import java.nio.charset.CodingErrorAction;
3940

40-
import sun.nio.cs.ThreadLocalCoders;
4141
import sun.nio.cs.UTF_8;
4242

4343
/**
@@ -177,9 +177,9 @@ public static String decode(String s) {
177177
StringBuilder sb = new StringBuilder(n);
178178
ByteBuffer bb = ByteBuffer.allocate(n);
179179
CharBuffer cb = CharBuffer.allocate(n);
180-
CharsetDecoder dec = ThreadLocalCoders.decoderFor(UTF_8.INSTANCE)
181-
.onMalformedInput(CodingErrorAction.REPORT)
182-
.onUnmappableCharacter(CodingErrorAction.REPORT);
180+
CharsetDecoder dec = UTF_8.INSTANCE.newDecoder()
181+
.onMalformedInput(CodingErrorAction.REPORT)
182+
.onUnmappableCharacter(CodingErrorAction.REPORT);
183183

184184
char c = s.charAt(0);
185185
for (int i = 0; i < n;) {
@@ -451,6 +451,7 @@ private static void appendFragment(StringBuilder sb, String fragment) {
451451
private static String quote(String s, long lowMask, long highMask) {
452452
int n = s.length();
453453
StringBuilder sb = null;
454+
CharsetEncoder encoder = null;
454455
boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
455456
for (int i = 0; i < s.length(); i++) {
456457
char c = s.charAt(i);
@@ -468,11 +469,14 @@ private static String quote(String s, long lowMask, long highMask) {
468469
} else if (allowNonASCII
469470
&& (Character.isSpaceChar(c)
470471
|| Character.isISOControl(c))) {
472+
if (encoder == null) {
473+
encoder = UTF_8.INSTANCE.newEncoder();
474+
}
471475
if (sb == null) {
472476
sb = new StringBuilder();
473477
sb.append(s, 0, i);
474478
}
475-
appendEncoded(sb, c);
479+
appendEncoded(encoder, sb, c);
476480
} else {
477481
if (sb != null)
478482
sb.append(c);
@@ -494,11 +498,11 @@ && match(s.charAt(pos + 1), L_HEX, H_HEX)
494498
&& match(s.charAt(pos + 2), L_HEX, H_HEX);
495499
}
496500

497-
private static void appendEncoded(StringBuilder sb, char c) {
501+
private static void appendEncoded(CharsetEncoder encoder,
502+
StringBuilder sb, char c) {
498503
ByteBuffer bb = null;
499504
try {
500-
bb = ThreadLocalCoders.encoderFor(UTF_8.INSTANCE)
501-
.encode(CharBuffer.wrap("" + c));
505+
bb = encoder.encode(CharBuffer.wrap("" + c));
502506
} catch (CharacterCodingException x) {
503507
assert false;
504508
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package org.openjdk.bench.java.net;
25+
26+
import org.openjdk.jmh.annotations.State;
27+
import org.openjdk.jmh.annotations.Scope;
28+
import org.openjdk.jmh.annotations.Benchmark;
29+
import org.openjdk.jmh.annotations.Fork;
30+
import org.openjdk.jmh.annotations.CompilerControl;
31+
import org.openjdk.jmh.annotations.BenchmarkMode;
32+
import org.openjdk.jmh.annotations.Mode;
33+
import org.openjdk.jmh.annotations.OutputTimeUnit;
34+
import org.openjdk.jmh.annotations.Setup;
35+
36+
import java.lang.invoke.MethodHandle;
37+
import java.lang.invoke.MethodHandles;
38+
import java.net.URI;
39+
import java.net.URL;
40+
import java.util.concurrent.TimeUnit;
41+
42+
import static java.lang.invoke.MethodType.methodType;
43+
44+
@BenchmarkMode(Mode.AverageTime)
45+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
46+
@State(Scope.Thread)
47+
@Fork(value = 1, jvmArgsAppend = "--add-exports=java.base/sun.net.www=ALL-UNNAMED")
48+
public class ThreadLocalParseUtil {
49+
50+
private static final MethodHandle MH_DECODE;
51+
private static final MethodHandle MH_TO_URI;
52+
53+
static {
54+
final MethodHandles.Lookup lookup = MethodHandles.lookup();
55+
try {
56+
Class<?> c = Class.forName("sun.net.www.ParseUtil");
57+
MH_DECODE = lookup.findStatic(c, "decode", methodType(String.class, String.class));
58+
MH_TO_URI = lookup.findStatic(c, "toURI", methodType(URI.class, URL.class));
59+
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
60+
throw new ExceptionInInitializerError(e);
61+
}
62+
}
63+
64+
@Benchmark
65+
public String decodeTest() throws Throwable {
66+
return (String) MH_DECODE.invokeExact("/xyz/\u00A0\u00A0");
67+
}
68+
69+
@Benchmark
70+
public URI appendEncodedTest() throws Throwable {
71+
URL url = new URL("https://example.com/xyz/abc/def?query=#30");
72+
return (URI) MH_TO_URI.invokeExact(url);
73+
}
74+
}

0 commit comments

Comments
 (0)