Skip to content

Commit

Permalink
Make impl of ClassPathResourceIndex#getResourceKey() faster
Browse files Browse the repository at this point in the history
Previous implementation:
Warmup Iteration   7: 63202.300 ops/ms
Warmup Iteration   8: 64074.043 ops/ms
Warmup Iteration   9: 63297.222 ops/ms
Warmup Iteration  10: 63905.908 ops/ms

New implementation:
Warmup Iteration   6: 234089.005 ops/ms
Warmup Iteration   7: 234372.742 ops/ms
Warmup Iteration   8: 235722.737 ops/ms
Warmup Iteration   9: 233265.148 ops/ms

Also added a test to make sure we don't break it.
  • Loading branch information
gsmet committed Aug 16, 2024
1 parent 1318543 commit 34631a0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class ClassPathResourceIndex {
// let's go with default max segments + 3 for the META-INF/versions/<version> part
private static final int MAX_SEGMENTS_META_INF_VERSIONS = MAX_SEGMENTS_DEFAULT + 3;

private static final char SLASH = '/';
private static final char DOT = '.';

/**
* This map is mapped by prefixes.
*/
Expand Down Expand Up @@ -132,7 +135,7 @@ public static Builder builder() {
* <p>
* Probably something we will have to tweak for corner cases but let's try to keep it fast.
*/
private static String getResourceKey(String resource) {
static String getResourceKey(String resource) {
// we don't really care about this part, it can be slower
if (resource.startsWith(META_INF_MAVEN)) {
return META_INF_MAVEN;
Expand All @@ -141,7 +144,6 @@ private static String getResourceKey(String resource) {
// for services, we want to reference the full path
return resource;
}
StringBuilder prefix = new StringBuilder();

int maxSegments;
if (resource.startsWith(IO_QUARKUS)) {
Expand All @@ -152,27 +154,21 @@ private static String getResourceKey(String resource) {
maxSegments = MAX_SEGMENTS_DEFAULT;
}

char[] charArray = resource.toCharArray();
int segmentCount = 0;
for (char c : charArray) {
if (c == '/') {
segmentCount++;
if (segmentCount == maxSegments) {
return prefix.toString();
int position = 0;
for (int i = 0; i < maxSegments; i++) {
int slashPosition = resource.indexOf(SLASH, position);
if (slashPosition > 0) {
position = slashPosition + 1;
} else {
if (i > 0 && resource.substring(position).indexOf(DOT) >= 0) {
break;
} else {
return resource;
}
}
prefix.append(c);
}

// let's drop the file name if we haven't truncated anything and we detect it's a file
if (segmentCount > 0 && segmentCount < maxSegments) {
int lastSlash = prefix.lastIndexOf("/");
if (prefix.substring(lastSlash).contains(".")) {
prefix.setLength(lastSlash);
}
}

return prefix.toString();
return resource.substring(0, position - 1);
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.bootstrap.classloading;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class ClassPathResourceIndexTestCase {

@Test
public void testGetResourceKey() {
assertEquals("io/quarkus/core/deployment",
ClassPathResourceIndex.getResourceKey("io/quarkus/core/deployment/MyClass.class"));
assertEquals("io/quarkus/core/deployment",
ClassPathResourceIndex.getResourceKey("io/quarkus/core/deployment/package/MyClass.class"));
assertEquals("org/apache/commons",
ClassPathResourceIndex.getResourceKey("org/apache/commons/codec/MyClass.class"));
assertEquals("test.properties", ClassPathResourceIndex.getResourceKey("test.properties"));
assertEquals("META-INF/maven/", ClassPathResourceIndex.getResourceKey("META-INF/maven/commons-codec/file.properties"));
assertEquals("io/quarkus", ClassPathResourceIndex.getResourceKey("io/quarkus/MyClass.class"));
assertEquals("META-INF/services/my-service", ClassPathResourceIndex.getResourceKey("META-INF/services/my-service"));
assertEquals("META-INF/versions/17/io/quarkus/core",
ClassPathResourceIndex.getResourceKey("META-INF/versions/17/io/quarkus/core/deployment"));
}
}

0 comments on commit 34631a0

Please sign in to comment.