Skip to content

Commit c5b676f

Browse files
Improve getResources performance
1 parent 8752232 commit c5b676f

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/JarClassPathElement.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ public Set<String> apply(JarFile jarFile) {
129129
Enumeration<JarEntry> entries = jarFile.entries();
130130
while (entries.hasMoreElements()) {
131131
JarEntry entry = entries.nextElement();
132-
paths.add(entry.getName());
132+
if (entry.getName().endsWith("/")) {
133+
paths.add(entry.getName().substring(0, entry.getName().length() - 1));
134+
} else {
135+
paths.add(entry.getName());
136+
}
133137
}
134138
return paths;
135139
}

independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/QuarkusClassLoader.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ public Enumeration<URL> getResources(String nm) throws IOException {
152152
//for single resources we still respect this
153153
boolean banned = state.bannedResources.contains(name);
154154
Set<URL> resources = new LinkedHashSet<>();
155-
//ClassPathElement[] providers = loadableResources.get(name);
156-
//if (providers != null) {
157-
// for (ClassPathElement element : providers) {
158-
// resources.add(element.getResource(nm).getUrl());
159-
// }
160-
//}
161155

162156
//this is a big of a hack, but is necessary to prevent service leakage
163157
//in some situations (looking at you gradle) the parent can contain the same
@@ -179,10 +173,12 @@ public Enumeration<URL> getResources(String nm) throws IOException {
179173
//ignore
180174
}
181175
}
182-
for (ClassPathElement i : elements) {
183-
ClassPathResource res = i.getResource(nm);
184-
if (res != null) {
185-
resources.add(res.getUrl());
176+
//TODO: in theory resources could have been added in dev mode
177+
//but I don't thing this really matters for this code path
178+
ClassPathElement[] providers = state.loadableResources.get(name);
179+
if (providers != null) {
180+
for (ClassPathElement element : providers) {
181+
resources.add(element.getResource(nm).getUrl());
186182
}
187183
}
188184
if (!banned) {
@@ -259,15 +255,20 @@ public URL getResource(String nm) {
259255
if (state.bannedResources.contains(name)) {
260256
return null;
261257
}
262-
// ClassPathElement[] providers = loadableResources.get(name);
263-
// if (providers != null) {
264-
// return providers[0].getResource(nm).getUrl();
265-
// }
266-
//TODO: because of dev mode we can't use the fast path her, we need to iterate
267-
for (ClassPathElement i : elements) {
268-
ClassPathResource res = i.getResource(name);
269-
if (res != null) {
270-
return res.getUrl();
258+
//TODO: because of dev mode we iterate, to see if any resources were added
259+
//not for .class files though, adding them causes a restart
260+
//this is very important for bytebuddy performance
261+
if (nm.endsWith(".class")) {
262+
ClassPathElement[] providers = state.loadableResources.get(name);
263+
if (providers != null) {
264+
return providers[0].getResource(nm).getUrl();
265+
}
266+
} else {
267+
for (ClassPathElement i : elements) {
268+
ClassPathResource res = i.getResource(name);
269+
if (res != null) {
270+
return res.getUrl();
271+
}
271272
}
272273
}
273274
return parent.getResource(nm);
@@ -280,15 +281,18 @@ public InputStream getResourceAsStream(String nm) {
280281
if (state.bannedResources.contains(name)) {
281282
return null;
282283
}
283-
// ClassPathElement[] providers = loadableResources.get(name);
284-
// if (providers != null) {
285-
// return new ByteArrayInputStream(providers[0].getResource(nm).getData());
286-
// }
287-
//TODO: because of dev mode we can't use the fast path her, we need to iterate
288-
for (ClassPathElement i : elements) {
289-
ClassPathResource res = i.getResource(name);
290-
if (res != null) {
291-
return new ByteArrayInputStream(res.getData());
284+
//dev mode may have added some files, so we iterate to check, but not for classes
285+
if (nm.endsWith(".class")) {
286+
ClassPathElement[] providers = state.loadableResources.get(name);
287+
if (providers != null) {
288+
return new ByteArrayInputStream(providers[0].getResource(nm).getData());
289+
}
290+
} else {
291+
for (ClassPathElement i : elements) {
292+
ClassPathResource res = i.getResource(name);
293+
if (res != null) {
294+
return new ByteArrayInputStream(res.getData());
295+
}
292296
}
293297
}
294298
return parent.getResourceAsStream(nm);

0 commit comments

Comments
 (0)