From d0594d4d5db0c1fa5092ce8cd7024cc0fd18fcee Mon Sep 17 00:00:00 2001 From: Martin Panzer Date: Wed, 24 Nov 2021 22:38:37 +0100 Subject: [PATCH] Reuse already created FileSystems during collection of ApplicationArchive 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. --- .../index/ApplicationArchiveBuildStep.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/index/ApplicationArchiveBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/index/ApplicationArchiveBuildStep.java index e2703b60ef53d..ab8961e8cdabb 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/index/ApplicationArchiveBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/index/ApplicationArchiveBuildStep.java @@ -147,7 +147,7 @@ private List scanForOtherIndexes(QuarkusBuildCloseablesBuild return appArchives; } - public void addIndexDependencyPaths(List indexDependencyBuildItems, + private void addIndexDependencyPaths(List indexDependencyBuildItems, ClassLoader classLoader, ArchiveRootBuildItem root, Set indexedDeps, List appArchives, QuarkusBuildCloseablesBuildItem buildCloseables, IndexCache indexCache, CurateOutcomeBuildItem curateOutcomeBuildItem, @@ -189,17 +189,19 @@ private static ApplicationArchive createApplicationArchive(QuarkusBuildCloseable IndexCache indexCache, Path dep, ArtifactKey artifactKey, Map> 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 removed) throws IOException { + private static IndexView indexPath(IndexCache indexCache, Path dep, Set 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 applicationArchiveFiles, @@ -213,6 +215,7 @@ private static void addMarkerFilePaths(Set applicationArchiveFiles, } final PathCollection artifactPaths = dep.getResolvedPaths(); boolean containsMarker = false; + FileSystem fs = null; for (Path p : artifactPaths) { if (root.isExcludedFromIndexing(p)) { continue; @@ -222,8 +225,11 @@ private static void addMarkerFilePaths(Set 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) { @@ -231,6 +237,10 @@ private static void addMarkerFilePaths(Set applicationArchiveFiles, // 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(); + } } } } @@ -239,13 +249,17 @@ private static void addMarkerFilePaths(Set applicationArchiveFiles, final PathList.Builder rootDirs = PathList.builder(); final List 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 @@ -255,7 +269,7 @@ private static void addMarkerFilePaths(Set applicationArchiveFiles, } } - private static boolean containsMarker(Path dir, Set applicationArchiveFiles) throws IOException { + private static boolean containsMarker(Path dir, Set applicationArchiveFiles) { for (String file : applicationArchiveFiles) { if (Files.exists(dir.resolve(file))) { return true; @@ -265,10 +279,6 @@ private static boolean containsMarker(Path dir, Set applicationArchiveFi } private static Index handleFilePath(Path path, Set removed) throws IOException { - return indexFilePath(path, removed); - } - - private static Index indexFilePath(Path path, Set removed) throws IOException { Indexer indexer = new Indexer(); try (Stream stream = Files.walk(path)) { stream.forEach(path1 -> {