[MNG-8396] Add a cache layer to the filtered dependency graph (#1944)

This commit is contained in:
Guillaume Nodet 2024-12-02 05:19:26 +01:00 committed by GitHub
parent a58551d9ab
commit 62f85a4233
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 23 additions and 2 deletions

View File

@ -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;
}
/**