Skip to content

Commit

Permalink
Reuse already created FileSystems during collection of ApplicationArc…
Browse files Browse the repository at this point in the history
…hive

the applicationarchive contains paths that are inside a zipfilesystem (of .jar files), needing the filesystem to be kept open during the whole build.
During collection of the applicationarchives by marker files (e.g. META-INF/jandex.idx), a fs is opened for checking for a marker file, and the opened again to be used for the paths of the apparchive.
Simply reusing the filesystems saves a few cycles. Since, at least on windows, opening new ZipFileSystems is relatively expensive.
  • Loading branch information
Postremus committed Nov 25, 2021
1 parent 3f0ffd4 commit d0594d4
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private List<ApplicationArchive> scanForOtherIndexes(QuarkusBuildCloseablesBuild
return appArchives;
}

public void addIndexDependencyPaths(List<IndexDependencyBuildItem> indexDependencyBuildItems,
private void addIndexDependencyPaths(List<IndexDependencyBuildItem> indexDependencyBuildItems,
ClassLoader classLoader, ArchiveRootBuildItem root, Set<Path> indexedDeps, List<ApplicationArchive> appArchives,
QuarkusBuildCloseablesBuildItem buildCloseables, IndexCache indexCache,
CurateOutcomeBuildItem curateOutcomeBuildItem,
Expand Down Expand Up @@ -189,17 +189,19 @@ private static ApplicationArchive createApplicationArchive(QuarkusBuildCloseable
IndexCache indexCache, Path dep, ArtifactKey artifactKey, Map<ArtifactKey, Set<String>> removedResources)
throws IOException {
Path rootDir = dep;
if (!Files.isDirectory(dep)) {
boolean isDirectory = Files.isDirectory(dep);
if (!isDirectory) {
final FileSystem fs = buildCloseables.add(FileSystems.newFileSystem(dep, classLoader));
rootDir = fs.getRootDirectories().iterator().next();
}
final IndexView index = indexPath(indexCache, dep, removedResources.get(artifactKey));
final IndexView index = indexPath(indexCache, dep, removedResources.get(artifactKey), isDirectory);
return new ApplicationArchiveImpl(index, rootDir, dep, artifactKey);
}

private static IndexView indexPath(IndexCache indexCache, Path dep, Set<String> removed) throws IOException {
private static IndexView indexPath(IndexCache indexCache, Path dep, Set<String> removed, boolean isWorkspaceModule)
throws IOException {
LOGGER.debugf("Indexing dependency: %s", dep);
return Files.isDirectory(dep) ? handleFilePath(dep, removed) : handleJarPath(dep, indexCache, removed);
return isWorkspaceModule ? handleFilePath(dep, removed) : handleJarPath(dep, indexCache, removed);
}

private static void addMarkerFilePaths(Set<String> applicationArchiveFiles,
Expand All @@ -213,6 +215,7 @@ private static void addMarkerFilePaths(Set<String> applicationArchiveFiles,
}
final PathCollection artifactPaths = dep.getResolvedPaths();
boolean containsMarker = false;
FileSystem fs = null;
for (Path p : artifactPaths) {
if (root.isExcludedFromIndexing(p)) {
continue;
Expand All @@ -222,15 +225,22 @@ private static void addMarkerFilePaths(Set<String> applicationArchiveFiles,
break;
}
} else {
try (FileSystem fs = FileSystems.newFileSystem(p, classLoader)) {
boolean close = true;
try {
fs = FileSystems.newFileSystem(p, classLoader);
if (containsMarker = containsMarker(fs.getPath("/"), applicationArchiveFiles)) {
close = false;
break;
}
} catch (ProviderNotFoundException | FileSystemNotFoundException e) {
// that is pretty much an exceptional case
// it's not a dir and not a jar, it could be anything (e.g. a pom file that
// ended up in some project deps)
// not necessarily a wrong state
} finally {
if (close && fs != null) {
fs.close();
}
}
}
}
Expand All @@ -239,13 +249,17 @@ private static void addMarkerFilePaths(Set<String> applicationArchiveFiles,
final PathList.Builder rootDirs = PathList.builder();
final List<IndexView> indexes = new ArrayList<>(artifactPaths.size());
for (Path p : artifactPaths) {
if (Files.isDirectory(p)) {
boolean isDirectory = Files.isDirectory(p);
if (isDirectory) {
rootDirs.add(p);
} else {
final FileSystem fs = buildCloseables.add(FileSystems.newFileSystem(p, classLoader));
if (fs == null) {
fs = FileSystems.newFileSystem(p, classLoader);
}
buildCloseables.add(fs);
fs.getRootDirectories().forEach(rootDirs::add);
}
indexes.add(indexPath(indexCache, p, removed.get(dep.getKey())));
indexes.add(indexPath(indexCache, p, removed.get(dep.getKey()), isDirectory));
indexedPaths.add(p);
}
appArchives
Expand All @@ -255,7 +269,7 @@ private static void addMarkerFilePaths(Set<String> applicationArchiveFiles,
}
}

private static boolean containsMarker(Path dir, Set<String> applicationArchiveFiles) throws IOException {
private static boolean containsMarker(Path dir, Set<String> applicationArchiveFiles) {
for (String file : applicationArchiveFiles) {
if (Files.exists(dir.resolve(file))) {
return true;
Expand All @@ -265,10 +279,6 @@ private static boolean containsMarker(Path dir, Set<String> applicationArchiveFi
}

private static Index handleFilePath(Path path, Set<String> removed) throws IOException {
return indexFilePath(path, removed);
}

private static Index indexFilePath(Path path, Set<String> removed) throws IOException {
Indexer indexer = new Indexer();
try (Stream<Path> stream = Files.walk(path)) {
stream.forEach(path1 -> {
Expand Down

0 comments on commit d0594d4

Please sign in to comment.