From df5fb1988c7eb6f6a328cf7da9134f005e88e03f Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Sat, 1 Jun 2024 11:10:54 +0200 Subject: [PATCH] Calculate BOM representation for each IU only once The BOM representation is currently calculated twice for each artifact. Within a reactor build, such IUs should always produce the same BOM representation and should therefore be cached. Resolves https://github.com/eclipse-tycho/tycho/issues/3911 --- .../TychoProjectDependenciesConverter.java | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/tycho-sbom/src/main/java/org/eclipse/tycho/sbom/TychoProjectDependenciesConverter.java b/tycho-sbom/src/main/java/org/eclipse/tycho/sbom/TychoProjectDependenciesConverter.java index 28cd2c73ca..5b30ae970d 100644 --- a/tycho-sbom/src/main/java/org/eclipse/tycho/sbom/TychoProjectDependenciesConverter.java +++ b/tycho-sbom/src/main/java/org/eclipse/tycho/sbom/TychoProjectDependenciesConverter.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -57,6 +58,8 @@ public class TychoProjectDependenciesConverter extends DefaultProjectDependencie @Inject private MavenProjectDependencyProcessor dependencyProcessor; + private final Map> bomRepresentations = new HashMap<>(); + @Override public void cleanupBomDependencies(Metadata metadata, Map components, Map dependencies) { @@ -117,35 +120,37 @@ private void convertToDependency(DependencyTreeNode node, Set depend private List getBomRepresentation(IInstallableUnit iu) { final MavenSession mavenSession = legacySupport.getSession(); final List reactorProjects = mavenSession.getAllProjects(); - // mutable! - List bomRefs = new ArrayList<>(); - // (I) IU describes local reactor project - try { - ProjectDependencyClosure dependencyClosure = dependencyProcessor - .computeProjectDependencyClosure(reactorProjects, mavenSession); - MavenProject iuProject = dependencyClosure.getProject(iu).orElse(null); - if (iuProject != null) { - String bomRef = modelConverter.generatePackageUrl(iuProject.getArtifact()); - if (bomRef == null) { - LOG.error("Unable to calculate BOM for: " + iuProject); + return bomRepresentations.computeIfAbsent(iu, ignore -> { + // mutable! + List bomRefs = new ArrayList<>(); + // (I) IU describes local reactor project + try { + ProjectDependencyClosure dependencyClosure = dependencyProcessor + .computeProjectDependencyClosure(reactorProjects, mavenSession); + MavenProject iuProject = dependencyClosure.getProject(iu).orElse(null); + if (iuProject != null) { + String bomRef = modelConverter.generatePackageUrl(iuProject.getArtifact()); + if (bomRef == null) { + LOG.error("Unable to calculate BOM for: " + iuProject); + return bomRefs; + } + bomRefs.add(bomRef); return bomRefs; } - bomRefs.add(bomRef); - return bomRefs; + } catch (CoreException e) { + LOG.error(e.getMessage(), e); + return Collections.emptyList(); } - } catch (CoreException e) { - LOG.error(e.getMessage(), e); - return Collections.emptyList(); - } - // (II) IU describes external artifact - for (IArtifactKey p2artifactKey : iu.getArtifacts()) { - String bomRef = modelConverter.generateP2PackageUrl(p2artifactKey, true, true, false); - if (bomRef == null) { - LOG.error("Unable to calculate BOM for: " + p2artifactKey); - continue; + // (II) IU describes external artifact + for (IArtifactKey p2artifactKey : iu.getArtifacts()) { + String bomRef = modelConverter.generateP2PackageUrl(p2artifactKey, true, true, false); + if (bomRef == null) { + LOG.error("Unable to calculate BOM for: " + p2artifactKey); + continue; + } + bomRefs.add(bomRef); } - bomRefs.add(bomRef); - } - return bomRefs; // mutable! + return bomRefs; // mutable! + }); } }