From 431fcdf964776f911700279b51ddd894c52ba0e2 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 (cherry picked from commit 065819f9d3ccee67676cf3e83a2c654efaab87e1) --- .../TychoProjectDependenciesConverter.java | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 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 ae852451c9..873a5500f8 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 @@ -13,12 +13,14 @@ package org.eclipse.tycho.sbom; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; import javax.inject.Inject; @@ -54,6 +56,8 @@ public class TychoProjectDependenciesConverter extends DefaultProjectDependencie @Inject private P2DependencyTreeGenerator dependencyGenerator; + private final Map> bomRepresentations = new ConcurrentHashMap<>(); + @Override public void cleanupBomDependencies(Metadata metadata, Map components, Map dependencies) { @@ -108,38 +112,38 @@ private void convertToDependency(DependencyTreeNode node, Set depend * empty list is returned. * * @param iu The installable unit for which to generate - * @return A {@code mutable} list of all bom representation. matching the given - * IU. + * @return An {@code immutable} list of all bom representations matching the + * given IU. */ 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 - for (MavenProject project : reactorProjects) { - ReactorProject reactorProject = DefaultReactorProject.adapt(project); - Set initalUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.INITIAL); - Set seedUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED); - if (initalUnits.contains(iu) || seedUnits.contains(iu)) { - String bomRef = modelConverter.generatePackageUrl(project.getArtifact()); + return bomRepresentations.computeIfAbsent(iu, ignore -> { + final MavenSession mavenSession = legacySupport.getSession(); + final List reactorProjects = mavenSession.getAllProjects(); + // (I) IU describes local reactor project + for (MavenProject project : reactorProjects) { + ReactorProject reactorProject = DefaultReactorProject.adapt(project); + Set initalUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.INITIAL); + Set seedUnits = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED); + if (initalUnits.contains(iu) || seedUnits.contains(iu)) { + String bomRef = modelConverter.generatePackageUrl(project.getArtifact()); + if (bomRef == null) { + LOG.error("Unable to calculate BOM for: " + project); + return Collections.emptyList(); + } + return Collections.singletonList(bomRef); + } + } + // (II) IU describes external artifact + final List bomRefs = new ArrayList<>(); + for (IArtifactKey p2artifactKey : iu.getArtifacts()) { + String bomRef = modelConverter.generateP2PackageUrl(p2artifactKey, true, true, false); if (bomRef == null) { - LOG.error("Unable to calculate BOM for: " + project); - return bomRefs; + LOG.error("Unable to calculate BOM for: " + p2artifactKey); + continue; } bomRefs.add(bomRef); - return bomRefs; } - } - // (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); - } - return bomRefs; // mutable! + return Collections.unmodifiableList(bomRefs); + }); } }