Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cache to LaunchedURLClassloader to improve startup performance #30016

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a cache to LaunchedURLClassloader to improve startup performance
This commit adds a ClassLoaderCache to LaunchedURLClassLoader  to improve startup performance. URLClassLoader shows awful performance when find Classes/Resources which do not exist.
  • Loading branch information
尚之 committed Mar 1, 2022
commit 49146f93ff5a9e306df0d4c5b236b1a33c0caec6
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.loader;

import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

/**
* A class/resource cache used by {@link LaunchedURLClassLoader} .
*
* @author Bingjie Lv
* @since 2.7.0
*
*/
public class ClassLoaderCache {

private boolean enableCache = Boolean.getBoolean("loader.cache.enable");

private final int cacheSize = Integer.getInteger("loader.cache.size", 3000);

private Map<String, ClassNotFoundException> classNotFoundExceptionCache;

private Map<String, Optional<URL>> resourceUrlCache;

private Map<String, Optional<Enumeration<URL>>> resourcesUrlCache;

public ClassLoaderCache() {
this.classNotFoundExceptionCache = createCache(this.cacheSize);
this.resourceUrlCache = createCache(this.cacheSize);
this.resourcesUrlCache = createCache(this.cacheSize);
}

public void fastClassNotFoundException(String name) throws ClassNotFoundException {
if (!this.enableCache) {
return;
}
ClassNotFoundException classNotFoundException = this.classNotFoundExceptionCache.get(name);
if (classNotFoundException != null) {
throw classNotFoundException;
}
}

public void cacheClassNotFoundException(String name, ClassNotFoundException exception) {
if (!this.enableCache) {
return;
}
this.classNotFoundExceptionCache.put(name, exception);
}

public Optional<URL> getResourceCache(String name) {
if (!this.enableCache) {
return null;
}
return this.resourceUrlCache.get(name);
}

public URL cacheResourceUrl(String name, URL url) {
if (!this.enableCache) {
return url;
}
this.resourceUrlCache.put(name, (url != null) ? Optional.of(url) : Optional.empty());
return url;
}

public Optional<Enumeration<URL>> getResourcesCache(String name) {
if (!this.enableCache) {
return null;
}
return this.resourcesUrlCache.get(name);
}

public Enumeration<URL> cacheResourceUrls(String name, Enumeration<URL> urlEnumeration) {
if (!this.enableCache) {
return urlEnumeration;
}
if (!urlEnumeration.hasMoreElements()) {
this.resourcesUrlCache.put(name, Optional.of(urlEnumeration));
}
return urlEnumeration;
}

public void clearCache() {
if (this.enableCache) {
this.classNotFoundExceptionCache.clear();
this.resourceUrlCache.clear();
this.resourcesUrlCache.clear();
}
}

public void setEnableCache(boolean enableCache) {
this.enableCache = enableCache;
}

protected <K, V> Map<K, V> createCache(int maxSize) {
return Collections.synchronizedMap(new LinkedHashMap<K, V>(maxSize, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() >= maxSize;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
@@ -55,6 +56,8 @@ public class LaunchedURLClassLoader extends URLClassLoader {

private final Object packageLock = new Object();

private final ClassLoaderCache loaderCache = new ClassLoaderCache();

private volatile DefinePackageCallType definePackageCallType;

/**
@@ -92,12 +95,16 @@ public LaunchedURLClassLoader(boolean exploded, Archive rootArchive, URL[] urls,

@Override
public URL findResource(String name) {
Optional<URL> optional = this.loaderCache.getResourceCache(name);
if (optional != null) {
return optional.orElse(null);
}
if (this.exploded) {
return super.findResource(name);
return this.loaderCache.cacheResourceUrl(name, super.findResource(name));
}
Handler.setUseFastConnectionExceptions(true);
try {
return super.findResource(name);
return this.loaderCache.cacheResourceUrl(name, super.findResource(name));
}
finally {
Handler.setUseFastConnectionExceptions(false);
@@ -106,12 +113,17 @@ public URL findResource(String name) {

@Override
public Enumeration<URL> findResources(String name) throws IOException {
Optional<Enumeration<URL>> optional = this.loaderCache.getResourcesCache(name);
if (optional != null) {
return optional.orElse(null);
}
if (this.exploded) {
return super.findResources(name);
return this.loaderCache.cacheResourceUrls(name, super.findResources(name));
}
Handler.setUseFastConnectionExceptions(true);
try {
return new UseFastConnectionExceptionsEnumeration(super.findResources(name));
return this.loaderCache.cacheResourceUrls(name,
new UseFastConnectionExceptionsEnumeration(super.findResources(name)));
}
finally {
Handler.setUseFastConnectionExceptions(false);
@@ -120,6 +132,21 @@ public Enumeration<URL> findResources(String name) throws IOException {

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
this.loaderCache.fastClassNotFoundException(name);
try {
return loadClassInternal(name, resolve);
}
catch (ClassNotFoundException ex) {
this.loaderCache.cacheClassNotFoundException(name, ex);
throw ex;
}
}

public void setEnableCache(boolean enableCache){
loaderCache.setEnableCache(enableCache);
}

private Class<?> loadClassInternal(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("org.springframework.boot.loader.jarmode.")) {
try {
Class<?> result = loadClassInLaunchedClassLoader(name);
@@ -297,6 +324,7 @@ private <T> T doDefinePackage(DefinePackageCallType type, Supplier<T> call) {
* Clear URL caches.
*/
public void clearCache() {
this.loaderCache.clearCache();
if (this.exploded) {
return;
}
Original file line number Diff line number Diff line change
@@ -108,4 +108,54 @@ void resolveFromNestedWhileThreadIsInterrupted() throws Exception {
}
}

@Test
void enableLoaderCache() throws Exception {
resolveResourceFromArchive();
resolveResourcesFromArchive();
resolveRootPathFromArchive();
resolveRootResourcesFromArchive();
resolveFromNested();
resolveFromNestedWhileThreadIsInterrupted();

LaunchedURLClassLoader loader = new LaunchedURLClassLoader(
new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") }, getClass().getClassLoader());
loader.setEnableCache(true);
assertThat(loader.getResource("demo/Application.java")).isEqualTo(loader.getResource("demo/Application.java"));
assertThat(loader.loadClass("demo.Application")).isEqualTo(loader.loadClass("demo.Application"));
assertThat(loader.getResource("demo/ApplicationNotExist.java")).isNull();
assertThat(loader.getResources("demo/ApplicationNotExist.java").hasMoreElements()).isNotEqualTo(true);
assertThat(loader.getResources("demo/ApplicationNotExist.java").hasMoreElements()).isNotEqualTo(true);
ClassNotFoundException ex = null;
ClassNotFoundException ex1 = null;
ClassNotFoundException ex2 = null;
ClassNotFoundException ex3 = null;
try {
loader.loadClass("demo.ApplicationNotExist");
} catch (ClassNotFoundException exception){
ex = exception;
}
try {
loader.loadClass("demo.ApplicationNotExist");
} catch (ClassNotFoundException exception){
ex1 = exception;
}
try {
loader.setEnableCache(false);
loader.loadClass("demo.ApplicationNotExist");
} catch (ClassNotFoundException exception){
ex2 = exception;
loader.setEnableCache(true);
}
try {
loader.clearCache();
loader.loadClass("demo.ApplicationNotExist");
} catch (ClassNotFoundException exception){
ex3 = exception;
}
assertThat(ex).isNotNull();
assertThat(ex1).isNotNull().isEqualTo(ex);
assertThat(ex2).isNotNull().isNotEqualTo(ex).isNotEqualTo(ex1);
assertThat(ex3).isNotNull().isNotEqualTo(ex2).isNotEqualTo(ex1).isNotEqualTo(ex);
}

}