Skip to content

Commit

Permalink
Performance improvement for TypeTable.Reader (#5093)
Browse files Browse the repository at this point in the history
* Performance improvement for `TypeTable.Reader`

Only match row GAV against artifact name patterns when different from previous row's GAV.

* Performance improvement for `TypeTable.Reader`

Only match row GAV against artifact name patterns when different from previous row's GAV.

Also, don't accumulate classes over artifact boundaries.
  • Loading branch information
knutwannheden authored Feb 25, 2025
1 parent d984f97 commit d5a44f7
Showing 1 changed file with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -135,8 +136,6 @@ private static Collection<String> artifactsNotYetWritten(Collection<String> arti
@RequiredArgsConstructor
static class Reader {
private final ExecutionContext ctx;
private @Nullable GroupArtifactVersion gav;
private final Map<ClassDefinition, List<Member>> membersByClassName = new HashMap<>();

public void read(InputStream is, Collection<String> artifactNames) throws IOException {
if (artifactNames.isEmpty()) {
Expand All @@ -148,24 +147,32 @@ public void read(InputStream is, Collection<String> artifactNames) throws IOExce
.map(name -> Pattern.compile(name + ".*"))
.collect(Collectors.toSet());

AtomicReference<@Nullable GroupArtifactVersion> matchedGav = new AtomicReference<>();
Map<ClassDefinition, List<Member>> membersByClassName = new HashMap<>();

try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
AtomicReference<@Nullable GroupArtifactVersion> lastGav = new AtomicReference<>();
in.lines().skip(1).forEach(line -> {
String[] fields = line.split("\t", -1);
GroupArtifactVersion rowGav = new GroupArtifactVersion(fields[0], fields[1], fields[2]);
if (!rowGav.equals(gav)) {
writeClassesDir();
}

String artifactVersion = fields[1] + "-" + fields[2];
if (!Objects.equals(rowGav, lastGav.get())) {
writeClassesDir(matchedGav.get(), membersByClassName);
matchedGav.set(null);
membersByClassName.clear();

String artifactVersion = fields[1] + "-" + fields[2];

for (Pattern artifactNamePattern : artifactNamePatterns) {
if (artifactNamePattern.matcher(artifactVersion).matches()) {
gav = rowGav;
break;
for (Pattern artifactNamePattern : artifactNamePatterns) {
if (artifactNamePattern.matcher(artifactVersion).matches()) {
matchedGav.set(rowGav);
break;
}
}
}
lastGav.set(rowGav);

if (gav != null) {
if (matchedGav.get() != null) {
ClassDefinition classDefinition = new ClassDefinition(
Integer.parseInt(fields[3]),
fields[4],
Expand All @@ -188,10 +195,11 @@ public void read(InputStream is, Collection<String> artifactNames) throws IOExce
}
});
}
writeClassesDir();

writeClassesDir(matchedGav.get(), membersByClassName);
}

private void writeClassesDir() {
private void writeClassesDir(@Nullable GroupArtifactVersion gav, Map<ClassDefinition, List<Member>> membersByClassName) {
if (gav == null) {
return;
}
Expand Down Expand Up @@ -265,8 +273,6 @@ private void writeClassesDir() {
throw new UncheckedIOException(e);
}
});

gav = null;
}
}

Expand Down

0 comments on commit d5a44f7

Please sign in to comment.