Skip to content

Commit

Permalink
Merge pull request #20823 from geoand/cl-microopt
Browse files Browse the repository at this point in the history
Apply small optimizations to CL setup
geoand authored Oct 18, 2021
2 parents 5f51c93 + 1252f17 commit 6f53f24
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -32,7 +32,8 @@ public final class RunnerClassLoader extends ClassLoader {
private final Set<String> nonExistentResources;
// the following two fields go hand in hand - they need to both be populated from the same data
// in order for the resource loading to work properly
private final Set<String> fullyIndexedDirectories;
// normally this field would be a set, but it only contains 2 elements, so making it a list is actually better
private final List<String> fullyIndexedDirectories;
private final Map<String, ClassLoadingResource[]> directlyIndexedResourcesIndexMap;

//Mutations protected by synchronization on the field value itself:
@@ -42,7 +43,7 @@ public final class RunnerClassLoader extends ClassLoader {

RunnerClassLoader(ClassLoader parent, Map<String, ClassLoadingResource[]> resourceDirectoryMap,
Set<String> parentFirstPackages, Set<String> nonExistentResources,
Set<String> fullyIndexedDirectories, Map<String, ClassLoadingResource[]> directlyIndexedResourcesIndexMap) {
List<String> fullyIndexedDirectories, Map<String, ClassLoadingResource[]> directlyIndexedResourcesIndexMap) {
super(parent);
this.resourceDirectoryMap = resourceDirectoryMap;
this.parentFirstPackages = parentFirstPackages;
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
@@ -20,7 +19,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
@@ -36,7 +34,7 @@ public class SerializedApplication {

public static final String META_INF_VERSIONS = "META-INF/versions/";
// the files immediately (i.e. not recursively) under these paths should all be indexed
private static final Set<String> FULLY_INDEXED_PATHS = new LinkedHashSet<>(Arrays.asList("", "META-INF/services"));
private static final List<String> FULLY_INDEXED_PATHS = List.of("", "META-INF/services");

private static final int MAGIC = 0XF0315432;
private static final int VERSION = 2;
@@ -210,7 +208,8 @@ private static List<String> writeJar(DataOutputStream out, Path jar) throws IOEx
if (!entry.getName().contains("/")) {
hasDefaultPackage = true;
if (!entry.getName().isEmpty() && FULLY_INDEXED_PATHS.contains("")) {
fullyIndexedPaths.computeIfAbsent("", s -> new ArrayList<>(10)).add(entry.getName());
fullyIndexedPaths.computeIfAbsent("", SerializedApplication::newFullyIndexedPathsValue)
.add(entry.getName());
}
} else if (!entry.isDirectory()) {
//some jars don't have correct directory entries
@@ -222,7 +221,7 @@ private static List<String> writeJar(DataOutputStream out, Path jar) throws IOEx
if (entry.getName().startsWith(META_INF_VERSIONS)) {
//multi release jar
//we add all packages here
//they may no be relevant for some versions, but that is fine
//they may not be relevant for some versions, but that is fine
String part = entry.getName().substring(META_INF_VERSIONS.length());
int slash = part.indexOf("/");
if (slash != -1) {
@@ -233,12 +232,14 @@ private static List<String> writeJar(DataOutputStream out, Path jar) throws IOEx
}
}

for (String path : FULLY_INDEXED_PATHS) {
for (int i = 0; i < FULLY_INDEXED_PATHS.size(); i++) {
String path = FULLY_INDEXED_PATHS.get(i);
if (path.isEmpty()) {
continue;
}
if (entry.getName().startsWith(path)) {
fullyIndexedPaths.computeIfAbsent(path, s -> new ArrayList<>(10)).add(entry.getName());
fullyIndexedPaths.computeIfAbsent(path, SerializedApplication::newFullyIndexedPathsValue)
.add(entry.getName());
}
}
}
@@ -258,6 +259,10 @@ private static List<String> writeJar(DataOutputStream out, Path jar) throws IOEx
}
}

private static List<String> newFullyIndexedPathsValue(String ignored) {
return new ArrayList<>(10);
}

private static void collectPackages(Path jar, Set<String> dirs) throws IOException {
if (Files.isDirectory(jar)) {
//this can only really happen when testing quarkus itself
@@ -356,14 +361,13 @@ void addResourceDir(String dir, JarResource resource) {
}

Map<String, ClassLoadingResource[]> getResult() {
overrides.forEach(new BiConsumer<String, Set<? extends ClassLoadingResource>>() {
@Override
public void accept(String dir, Set<? extends ClassLoadingResource> jarResources) {
result.put(dir, jarResources.toArray(EMPTY_ARRAY));
}
});
overrides.forEach(this::addToResult);
return result;
}

private void addToResult(String dir, Set<? extends ClassLoadingResource> jarResources) {
result.put(dir, jarResources.toArray(EMPTY_ARRAY));
}
}

}

0 comments on commit 6f53f24

Please sign in to comment.