perf(metrics): optimize LakosMetrics transitive dependency graph#1629
Merged
Conversation
1417c14 to
9de0a9a
Compare
hankem
approved these changes
May 25, 2026
Member
hankem
left a comment
There was a problem hiding this comment.
Thank you for this improvement!
On my system, it reduces the time to compute
ArchitectureMetrics.lakosMetrics(MetricsComponents.fromClasses(classes))
for hibernate-core:7.3.6 (with 37,281 classes)
- from (101±2) s with ArchUnit 1.4.2
- by a factor of more than 16 to (6.2±0.3) s. 🎉
038ce4f to
133c9d5
Compare
Signed-off-by: Thanos Tsiamis <thatsiamis@gmail.com> perf(metrics): PR review comments which optimizes imports Signed-off-by: Thanos Tsiamis <thatsiamis@gmail.com>
133c9d5 to
5f22ee0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This Pull Request addresses the performance hotspot in
LakosMetricsby overhauling how transitive reachability is computed withinMetricsComponentDependencyGraph.The Problem
Previously,
getTransitiveDependenciesOf()executed a fresh Depth-First Search (DFS) from scratch for every single component. On complex architecture graphs with shared downstream subgraphs or deep/cyclic dependencies, this caused heavily redundant traversals, driving the runtime complexity up to O(V * (V + E)). It also introduced significant garbage collection pressure by instantiating newHashSetcollections on every query.The Solution
Instead of query-time DFS traversals, this PR introduces a robust, primitive-backed precomputation engine executed once during the graph's initialization:
determineStronglyConnectedComponents()runs a two-pass iterative DFS (forward to find finishing times, reverse on the transposed graph) to safely group cyclic dependencies into Strongly Connected Components (SCCs).BitSet.or()). This ensures that shared downstream paths are evaluated exactly once.getTransitiveDependenciesOf()from an expensive graph traversal into an O(1) map lookup.Additional Robustness & Safeguards
ArrayDeque<TraversalFrame>), protecting the JVM againstStackOverflowErroron deep, linear dependency chains.int[][],int[], andboolean[]arrays to optimize CPU cache locality and reduce memory footprint.ImmutableMap.Builder(which crashes with anIllegalArgumentExceptionif duplicate elements are fed from upstream), the precomputation safely compiles results into an intermediateLinkedHashMapbefore generating the final immutable copy.Related Issue
Closes #1628