This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push: new 62f85a4233 [MNG-8396] Add a cache layer to the filtered dependency graph (#1944) 62f85a4233 is described below commit 62f85a4233366064750079d50fd86ef25a97c6f3 Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Mon Dec 2 05:19:26 2024 +0100 [MNG-8396] Add a cache layer to the filtered dependency graph (#1944) --- .../graph/FilteredProjectDependencyGraph.java | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/impl/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java b/impl/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java index feb06ec7c2..6496dd686d 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java +++ b/impl/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java @@ -24,6 +24,7 @@ import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; import org.apache.maven.execution.ProjectDependencyGraph; import org.apache.maven.project.MavenProject; @@ -40,6 +41,10 @@ class FilteredProjectDependencyGraph implements ProjectDependencyGraph { private final List<MavenProject> sortedProjects; + private final Map<Key, List<MavenProject>> cache = new ConcurrentHashMap<>(); + + private record Key(MavenProject project, boolean transitive, boolean upstream) {} + /** * Creates a new project dependency graph from the specified graph. * @@ -74,12 +79,28 @@ class FilteredProjectDependencyGraph implements ProjectDependencyGraph { @Override public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) { - return applyFilter(projectDependencyGraph.getDownstreamProjects(project, transitive), transitive, false); + Key key = new Key(project, transitive, false); + // Do not use computeIfAbsent here, as the computation is recursive + // and this is not supported by computeIfAbsent. + List<MavenProject> list = cache.get(key); + if (list == null) { + list = applyFilter(projectDependencyGraph.getDownstreamProjects(project, transitive), transitive, false); + cache.put(key, list); + } + return list; } @Override public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) { - return applyFilter(projectDependencyGraph.getUpstreamProjects(project, transitive), transitive, true); + Key key = new Key(project, transitive, true); + // Do not use computeIfAbsent here, as the computation is recursive + // and this is not supported by computeIfAbsent. + List<MavenProject> list = cache.get(key); + if (list == null) { + list = applyFilter(projectDependencyGraph.getUpstreamProjects(project, transitive), transitive, true); + cache.put(key, list); + } + return list; } /**