Skip to content

perf: cache dependency refs to skip redundant registration in validate() - #1033

Open
saquibsaifee wants to merge 4 commits into
CycloneDX:mainfrom
saquibsaifee:perf-optimize-bom-validate-17515006840806688594
Open

saquibsaifee wants to merge 4 commits into
CycloneDX:mainfrom
saquibsaifee:perf-optimize-bom-validate-17515006840806688594

Conversation

@saquibsaifee

@saquibsaifee saquibsaifee commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Optimizes Bom.validate() from $O(N^2)$ to $O(N \log N)$ by eliminating redundant linear scans over self.dependencies when ensuring dependable elements are registered, directly adding missing entries while tracking seen references in $O(1)$.

Changes:

  • Track existing dependency references dynamically to skip redundant registration for already-registered components and services.
  • Directly add missing dependable entries to _dependencies with active set tracking, eliminating repeated linear filter scans over the dependency collection.
  • Clean up collection comprehensions in dependency graph validation.

Affirmation

@saquibsaifee
saquibsaifee requested a review from a team as a code owner August 31, 2026 15:22
@saquibsaifee

saquibsaifee commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor Author

During the generation of large SBOMs, validation times were high due to generator evaluation.
With this change, the Bom.validate() running time on an SBOM with 2500 nested components and 2500 services executed 10 times drops from 49.4 seconds down to 5.1 seconds. This yields a 10x execution speedup in terms of validation execution alone.

@codacy-production

codacy-production Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 7 complexity · 0 duplication

Metric Results
Complexity 7
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@read-the-docs-community

read-the-docs-community Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

@saquibsaifee

Copy link
Copy Markdown
Contributor Author

@jkowalleck can you take a look at this PR to boost SBOM generation turn around

Signed-off-by: saquibsaifee <saquibsaifee2@gmail.com>
@saquibsaifee
saquibsaifee force-pushed the perf-optimize-bom-validate-17515006840806688594 branch from 6f5dc72 to e05bb2f Compare August 31, 2026 16:08
@saquibsaifee saquibsaifee changed the title perf: Optimize SBOM validation by removing itertools.chain perf: optimize validation by removing itertools.chain Sep 16, 2026
@saquibsaifee saquibsaifee changed the title perf: optimize validation by removing itertools.chain perf: cache dependency refs to skip redundant registration in validate() Sep 16, 2026
@saquibsaifee

Copy link
Copy Markdown
Contributor Author

Need to look into this again, might not be adding any performance improvement at all

…n validate()

Signed-off-by: Saquib Saifee <66195765+saquibsaifee@users.noreply.github.com>
 into perf-optimize-bom-validate-17515006840806688594
@saquibsaifee

Copy link
Copy Markdown
Contributor Author

@jkowalleck, this PR is ready for review.

@kmlyz kmlyz left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up that this overlaps with #1006 and open PR #1007, which addresses the same Bom.validate() -> register_dependency() linear scan.

I tested both branches locally against current main (Python 3.13, test_model_bom and JSON/XML v1.6 serialization). Both produce identical serialized output and reduce BomRef.__eq__ calls during validate() at N = 4000 from 8,051,885 to 49,884.

One code note if this branch moves forward: reading self._dependencies directly in Step 0 avoids going through the property getter, and keeping Step 1's set(chain(...)) avoids the extra per-iteration .add() / .update() overhead of the Python for loop (162 ms vs 176 ms in validate() at N = 4000). test_validate_registers_dependencies_for_components_and_services here is a nice addition though, since it explicitly covers shared bom_ref deduplication across Component and Service.

Disclosure: Local container test runs and benchmark comparisons were assisted by Google Antigravity (Gemini 4 Carbon); commands and outputs were verified locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants