Improve performance of classloaders + small refactoring
During the initialization of the classloader, we preload some jar files, that are necessary for the compiler to work correctly. In this process of preloading classes, we unpack each jar file and look at the value of the Class-Path in its META_INF. So we will determine, which files are depends on by original jars. We will load the resulting jars in the next iteration of class preloading. Thus, we get nested classloaders. However, in practice, it turned out that the jar files needed for the second iteration were already loaded in the first iteration, so there is no point in loading them again. Moreover, if we do not find the class in the first loader, then we will not find it in the second either. However, the case, when there are some jars from Class-Path of original jars and they were not loaded by first iteration, does not change. Aldo needed for KT-49786
This commit is contained in:
committed by
TeamCityServer
parent
51473651cf
commit
8c3b1dc55a
@@ -49,6 +49,7 @@ public class ClassPreloadingUtils {
|
|||||||
Map<String, Object> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
|
Map<String, Object> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
|
||||||
|
|
||||||
Collection<File> classpath = mergeClasspathFromManifests(entries);
|
Collection<File> classpath = mergeClasspathFromManifests(entries);
|
||||||
|
classpath.removeAll(jarFiles);
|
||||||
if (!classpath.isEmpty()) {
|
if (!classpath.isEmpty()) {
|
||||||
parentClassLoader = preloadClasses(classpath, classCountEstimation, parentClassLoader, null, handler);
|
parentClassLoader = preloadClasses(classpath, classCountEstimation, parentClassLoader, null, handler);
|
||||||
}
|
}
|
||||||
@@ -57,7 +58,7 @@ public class ClassPreloadingUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static URLClassLoader createFallbackClassLoader(Collection<File> files) throws IOException {
|
private static URLClassLoader createFallbackClassLoader(Collection<File> files) throws IOException {
|
||||||
List<URL> urls = new ArrayList<URL>(files.size());
|
List<URL> urls = new ArrayList<>(files.size());
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
urls.add(file.toURI().toURL());
|
urls.add(file.toURI().toURL());
|
||||||
}
|
}
|
||||||
@@ -76,7 +77,7 @@ public class ClassPreloadingUtils {
|
|||||||
return extractManifestClasspath((ResourceData) manifest);
|
return extractManifestClasspath((ResourceData) manifest);
|
||||||
}
|
}
|
||||||
else if (manifest instanceof ArrayList) {
|
else if (manifest instanceof ArrayList) {
|
||||||
List<File> result = new ArrayList<File>();
|
List<File> result = new ArrayList<>();
|
||||||
for (ResourceData data : (ArrayList<ResourceData>) manifest) {
|
for (ResourceData data : (ArrayList<ResourceData>) manifest) {
|
||||||
result.addAll(extractManifestClasspath(data));
|
result.addAll(extractManifestClasspath(data));
|
||||||
}
|
}
|
||||||
@@ -93,7 +94,7 @@ public class ClassPreloadingUtils {
|
|||||||
String classpathSpaceSeparated = (String) manifest.getMainAttributes().get(Attributes.Name.CLASS_PATH);
|
String classpathSpaceSeparated = (String) manifest.getMainAttributes().get(Attributes.Name.CLASS_PATH);
|
||||||
if (classpathSpaceSeparated == null) return Collections.emptyList();
|
if (classpathSpaceSeparated == null) return Collections.emptyList();
|
||||||
|
|
||||||
Collection<File> classpath = new ArrayList<File>(1);
|
Collection<File> classpath = new ArrayList<>(1);
|
||||||
for (String jar : classpathSpaceSeparated.split(" ")) {
|
for (String jar : classpathSpaceSeparated.split(" ")) {
|
||||||
if (".".equals(jar)) continue;
|
if (".".equals(jar)) continue;
|
||||||
|
|
||||||
@@ -117,7 +118,7 @@ public class ClassPreloadingUtils {
|
|||||||
ClassHandler handler
|
ClassHandler handler
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
// 0.75 is HashMap.DEFAULT_LOAD_FACTOR
|
// 0.75 is HashMap.DEFAULT_LOAD_FACTOR
|
||||||
Map<String, Object> resources = new HashMap<String, Object>((int) (classNumberEstimate / 0.75));
|
Map<String, Object> resources = new HashMap<>((int) (classNumberEstimate / 0.75));
|
||||||
|
|
||||||
for (File jarFile : jarFiles) {
|
for (File jarFile : jarFiles) {
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
@@ -154,7 +155,7 @@ public class ClassPreloadingUtils {
|
|||||||
resources.put(name, resourceData);
|
resources.put(name, resourceData);
|
||||||
}
|
}
|
||||||
else if (previous instanceof ResourceData) {
|
else if (previous instanceof ResourceData) {
|
||||||
List<ResourceData> list = new ArrayList<ResourceData>();
|
List<ResourceData> list = new ArrayList<>();
|
||||||
list.add((ResourceData) previous);
|
list.add((ResourceData) previous);
|
||||||
list.add(resourceData);
|
list.add(resourceData);
|
||||||
resources.put(name, list);
|
resources.put(name, list);
|
||||||
|
|||||||
Reference in New Issue
Block a user