Optimize large SBOM parsing and encoding - #13
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR focuses on reducing CPU time and allocations when parsing and encoding large SPDX and CycloneDX SBOMs by pre-sizing core model slices/maps, avoiding repeated transformations, and streamlining SPDX envelope handling while preserving format detection and depth limits.
Changes:
- Introduces pre-sized SBOM construction (
newSizedSBOM) and uses it in CycloneDX and SPDX parsing to cut allocations. - Splits SPDX envelope handling into a dedicated
spdxEnvelope+unwrapSPDXEnvelope, with detection carrying anspdxEnvelopeflag. - Optimizes encoding paths by pre-sizing output slices and avoiding repeated string/format work; adds targeted tests and expanded benchmarks for large/complex inputs.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| spdx.go | Refactors SPDX parsing to unwrap envelopes up-front and pre-size SBOM structures and per-package slices. |
| sbom.go | Adds newSizedSBOM; refactors Detect into detect returning envelope metadata used by Parse. |
| encode.go | Pre-sizes encoded CycloneDX/SPDX structures and replaces repeated string conversions with targeted helpers. |
| encode_test.go | Adds a regression test for generated SPDX package IDs and relationship ordering. |
| cyclonedx.go | Pre-computes component/relationship counts for nested components, pre-sizes SBOM, and avoids append-driven growth. |
| cyclonedx_test.go | Adds coverage for parsed CycloneDX package metadata (type normalization, hashes, external refs, properties). |
| bench_test.go | Adds/expands benchmarks for large docs, nested components, dense graphs, envelopes, duplicates, and parse→encode loops. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Reduce CPU time and allocations when parsing and encoding large CycloneDX and SPDX documents. Pre-size model slices and indexes, avoid repeated string transformations, copy package metadata into exact-sized slices, and reduce envelope work while preserving depth limits and format detection.\n\nAdd benchmarks for large documents, flat and nested CycloneDX components, dense graphs, SPDX envelopes, parse-then-encode loops, and duplicate package identities.