Commit 11b1318c authored by Phillip Webb's avatar Phillip Webb

Reduce GC pressure in JAR handler

Update the JAR `Hander` so that URL `startsWith` checks produce less
garbage. Comparisons are now performed first on the `path` rather than
the full `toString`. URL `toString` operations produce quite a lot of
garbage since a `StringBuilder` is always used.

In addition, we now also cache the JarFile URL toString to save repeated
calculation.

Closes gh-14561
parent d0de4657
...@@ -92,8 +92,7 @@ public class Handler extends URLStreamHandler { ...@@ -92,8 +92,7 @@ public class Handler extends URLStreamHandler {
@Override @Override
protected URLConnection openConnection(URL url) throws IOException { protected URLConnection openConnection(URL url) throws IOException {
if (this.jarFile != null if (this.jarFile != null && isUrlInJarFile(url, this.jarFile)) {
&& url.toString().startsWith(this.jarFile.getUrl().toString())) {
return JarURLConnection.get(url, this.jarFile); return JarURLConnection.get(url, this.jarFile);
} }
try { try {
...@@ -104,6 +103,13 @@ public class Handler extends URLStreamHandler { ...@@ -104,6 +103,13 @@ public class Handler extends URLStreamHandler {
} }
} }
private boolean isUrlInJarFile(URL url, JarFile jarFile)
throws MalformedURLException {
// Try the path first to save building a new url string each time
return url.getPath().startsWith(jarFile.getUrl().getPath())
&& url.toString().startsWith(jarFile.getUrlString());
}
private URLConnection openFallbackConnection(URL url, Exception reason) private URLConnection openFallbackConnection(URL url, Exception reason)
throws IOException { throws IOException {
try { try {
......
...@@ -69,6 +69,8 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -69,6 +69,8 @@ public class JarFile extends java.util.jar.JarFile {
private URL url; private URL url;
private String urlString;
private JarFileEntries entries; private JarFileEntries entries;
private Supplier<Manifest> manifestSupplier; private Supplier<Manifest> manifestSupplier;
...@@ -301,6 +303,13 @@ public class JarFile extends java.util.jar.JarFile { ...@@ -301,6 +303,13 @@ public class JarFile extends java.util.jar.JarFile {
} }
} }
String getUrlString() throws MalformedURLException {
if (this.urlString == null) {
this.urlString = getUrl().toString();
}
return this.urlString;
}
/** /**
* Return a URL that can be used to access this JAR file. NOTE: the specified URL * Return a URL that can be used to access this JAR file. NOTE: the specified URL
* cannot be serialized and or cloned. * cannot be serialized and or cloned.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment