mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 05:59:52 +00:00
8379557: Further optimize URL.toExternalForm
Reviewed-by: vyazici
This commit is contained in:
parent
00a77704b0
commit
0379c0b005
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1995, 2026, 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
|
||||
@ -481,14 +481,33 @@ public abstract class URLStreamHandler {
|
||||
* @return a string representation of the {@code URL} argument.
|
||||
*/
|
||||
protected String toExternalForm(URL u) {
|
||||
String s;
|
||||
// The fast paths and branch-free concatenations in this method are here for
|
||||
// a reason and should not be updated without checking performance figures.
|
||||
|
||||
// Optionality, subtly different for authority
|
||||
boolean emptyAuth = u.getAuthority() == null || u.getAuthority().isEmpty();
|
||||
boolean emptyPath = u.getPath() == null;
|
||||
boolean emptyQuery = u.getQuery() == null;
|
||||
boolean emptyRef = u.getRef() == null;
|
||||
var path = emptyPath ? "" : u.getPath();
|
||||
// Fast paths for empty components
|
||||
if (emptyQuery && emptyRef) {
|
||||
return emptyAuth
|
||||
? (u.getProtocol() + ":" + path)
|
||||
: (u.getProtocol() + "://" + u.getAuthority() + path);
|
||||
}
|
||||
// Prefer locals for efficient concatenation
|
||||
var authSep = emptyAuth ? ":" : "://";
|
||||
var auth = emptyAuth ? "" : u.getAuthority();
|
||||
var querySep = emptyQuery ? "" : "?";
|
||||
var query = emptyQuery ? "" : u.getQuery();
|
||||
var refSep = emptyRef ? "" : "#";
|
||||
var ref = emptyRef ? "" : u.getRef();
|
||||
return u.getProtocol()
|
||||
+ ':'
|
||||
+ ((s = u.getAuthority()) != null && !s.isEmpty()
|
||||
? "//" + s : "")
|
||||
+ ((s = u.getPath()) != null ? s : "")
|
||||
+ ((s = u.getQuery()) != null ? '?' + s : "")
|
||||
+ ((s = u.getRef()) != null ? '#' + s : "");
|
||||
+ authSep + auth
|
||||
+ path
|
||||
+ querySep + query
|
||||
+ refSep + ref;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1998, 2026, 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
|
||||
@ -48,6 +48,7 @@ public class Constructor {
|
||||
entries.addAll(Arrays.asList(jarURLs));
|
||||
entries.addAll(Arrays.asList(normalHttpURLs));
|
||||
entries.addAll(Arrays.asList(abnormalHttpURLs));
|
||||
entries.addAll(Arrays.asList(blankComponents));
|
||||
if (hasFtp())
|
||||
entries.addAll(Arrays.asList(ftpURLs));
|
||||
URL url;
|
||||
@ -252,4 +253,20 @@ public class Constructor {
|
||||
"/dir1/entry.txt",
|
||||
"ftp://br:pwd@ftp.foo.com/dir1/entry.txt")
|
||||
};
|
||||
|
||||
static Entry[] blankComponents = new Entry[] {
|
||||
new Entry(null, "http://host/path#", "http://host/path#"),
|
||||
new Entry(null, "http://host/path?", "http://host/path?"),
|
||||
new Entry(null, "http://host/path?#", "http://host/path?#"),
|
||||
new Entry(null, "http://host/path#?", "http://host/path#?"),
|
||||
new Entry(null, "file:/path#", "file:/path#"),
|
||||
new Entry(null, "file:/path?", "file:/path?"),
|
||||
new Entry(null, "file:/path?#", "file:/path?#"),
|
||||
new Entry(null, "file:///path#", "file:/path#"),
|
||||
new Entry(null, "file:///path?", "file:/path?"),
|
||||
new Entry(null, "file:/path#?", "file:/path#?"),
|
||||
new Entry("file:/path", "path?#", "file:/path?#"),
|
||||
new Entry(null, "file:", "file:"),
|
||||
new Entry(null, "file:#", "file:#")
|
||||
};
|
||||
}
|
||||
|
||||
77
test/micro/org/openjdk/bench/java/net/URLToString.java
Normal file
77
test/micro/org/openjdk/bench/java/net/URLToString.java
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2026, 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.*;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Tests java.net.URL.toString performance
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@State(Scope.Thread)
|
||||
@Warmup(iterations = 5, time = 1)
|
||||
@Measurement(iterations = 5, time = 1)
|
||||
@Fork(value = 3)
|
||||
public class URLToString {
|
||||
|
||||
@Param({"false", "true"})
|
||||
boolean auth;
|
||||
|
||||
@Param({"false", "true"})
|
||||
boolean query;
|
||||
|
||||
@Param({"false", "true"})
|
||||
boolean ref;
|
||||
|
||||
private URL url;
|
||||
|
||||
@Setup()
|
||||
public void setup() throws MalformedURLException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (auth) {
|
||||
sb.append("http://hostname");
|
||||
} else {
|
||||
sb.append("file:");
|
||||
}
|
||||
sb.append("/some/long/path/to/jar/app-1.0.jar!/org/summerframework/samples/horseclinic/HorseClinicApplication.class");
|
||||
if (query) {
|
||||
sb.append("?param=value");
|
||||
}
|
||||
if (ref) {
|
||||
sb.append("#fragment");
|
||||
}
|
||||
|
||||
url = URI.create(sb.toString()).toURL();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String urlToString() {
|
||||
return url.toString();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user