8245308: Replace ThreadLocalCoders decoder/encoder cache in java.net.URI

This fix updates java.net.URI and replaces the ThreadLocalCoders optimization.

Reviewed-by: alanb, dfuchs
This commit is contained in:
Rahul Yadav 2020-08-28 18:05:20 +01:00
parent e1d29cd6fb
commit 0f64d4bead
2 changed files with 73 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -34,6 +34,7 @@ import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.CharacterCodingException;
@ -41,7 +42,6 @@ import java.nio.file.Path;
import java.text.Normalizer;
import jdk.internal.access.JavaNetUriAccess;
import jdk.internal.access.SharedSecrets;
import sun.nio.cs.ThreadLocalCoders;
import sun.nio.cs.UTF_8;
import java.lang.Character; // for javadoc
@ -2761,11 +2761,10 @@ public final class URI
sb.append(hexDigits[(b >> 0) & 0x0f]);
}
private static void appendEncoded(StringBuilder sb, char c) {
private static void appendEncoded(CharsetEncoder encoder, StringBuilder sb, char c) {
ByteBuffer bb = null;
try {
bb = ThreadLocalCoders.encoderFor(UTF_8.INSTANCE)
.encode(CharBuffer.wrap("" + c));
bb = encoder.encode(CharBuffer.wrap("" + c));
} catch (CharacterCodingException x) {
assert false;
}
@ -2783,6 +2782,7 @@ public final class URI
//
private static String quote(String s, long lowMask, long highMask) {
StringBuilder sb = null;
CharsetEncoder encoder = null;
boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
@ -2800,11 +2800,13 @@ public final class URI
} else if (allowNonASCII
&& (Character.isSpaceChar(c)
|| Character.isISOControl(c))) {
if (encoder == null)
encoder = UTF_8.INSTANCE.newEncoder();
if (sb == null) {
sb = new StringBuilder();
sb.append(s, 0, i);
}
appendEncoded(sb, c);
appendEncoded(encoder, sb, c);
} else {
if (sb != null)
sb.append(c);
@ -2832,8 +2834,9 @@ public final class URI
String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
ByteBuffer bb = null;
try {
bb = ThreadLocalCoders.encoderFor(UTF_8.INSTANCE)
bb = UTF_8.INSTANCE.newEncoder()
.encode(CharBuffer.wrap(ns));
} catch (CharacterCodingException x) {
assert false;
}
@ -2890,9 +2893,9 @@ public final class URI
StringBuilder sb = new StringBuilder(n);
ByteBuffer bb = ByteBuffer.allocate(n);
CharBuffer cb = CharBuffer.allocate(n);
CharsetDecoder dec = ThreadLocalCoders.decoderFor(UTF_8.INSTANCE)
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
CharsetDecoder dec = UTF_8.INSTANCE.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
// This is not horribly efficient, but it will do for now
char c = s.charAt(0);

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.net;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class ThreadLocalURI {
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public URI uriEncoderTest() throws URISyntaxException {
return new URI("http", "\u00A0", "\u00A0");
}
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public URI uriDecoderBaseline() throws URISyntaxException {
return new URI("https", "example.com", "/xyz/abc/def?query=", "#30");
}
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public String uriDecoderTest() throws URISyntaxException {
return new URI("https", "example.com", "/xyz/abc/def?query=", "#30")
.getPath();
}
}