From 54a4c9d8e4abbe9bea818c03ba50b36d4ee228e7 Mon Sep 17 00:00:00 2001 From: To6i Date: Wed, 20 Nov 2024 10:51:34 +0100 Subject: [PATCH] Improve performance for transitive dependency checks `TransitiveDependencyCondition` internally calls `contains()` recursively on the collection of all objects to be tested. If this collection is a large list and there are enough recursive calls to `getDirectDependencyTargetsOutsideOfAnalyzedClasses()` this results in a heavy performance impact. On a reasonable large project a single test using that condition may take minutes to complete. Converting the given list to a Set with much better `contains()` performance fixes this issue. on-behalf-of: @e-solutions-GmbH Signed-off-by: To6i --- .../lang/conditions/TransitiveDependencyCondition.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/conditions/TransitiveDependencyCondition.java b/archunit/src/main/java/com/tngtech/archunit/lang/conditions/TransitiveDependencyCondition.java index 724e0397c..ca7b02c8c 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/conditions/TransitiveDependencyCondition.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/conditions/TransitiveDependencyCondition.java @@ -21,6 +21,7 @@ import java.util.Set; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.tngtech.archunit.PublicAPI; import com.tngtech.archunit.base.DescribedPredicate; import com.tngtech.archunit.core.domain.JavaClass; @@ -41,7 +42,7 @@ public final class TransitiveDependencyCondition extends ArchCondition conditionPredicate; private final TransitiveDependencyPath transitiveDependencyPath = new TransitiveDependencyPath(); - private Collection allClasses; + private Set allClasses; public TransitiveDependencyCondition(DescribedPredicate conditionPredicate) { super("transitively depend on classes that " + conditionPredicate.getDescription()); @@ -51,7 +52,7 @@ public TransitiveDependencyCondition(DescribedPredicate condi @Override public void init(Collection allObjectsToTest) { - this.allClasses = allObjectsToTest; + this.allClasses = ImmutableSet.copyOf(allObjectsToTest); } @Override