Skip to content

perf: optimize patch helper hot paths#1638

Open
sozercan wants to merge 2 commits into
mainfrom
perf
Open

perf: optimize patch helper hot paths#1638
sozercan wants to merge 2 commits into
mainfrom
perf

Conversation

@sozercan

@sozercan sozercan commented Jul 2, 2026

Copy link
Copy Markdown
Member

Summary

  • Add focused microbenchmarks for report parsing, package-manager validation/parsing, platform/buildkit helpers, and bulk patch helper paths.
  • Optimize hot paths in Trivy fixed-version selection, package validation/manifest parsing, platform filtering, local manifest descriptor parsing, and bulk tag/report handling.
  • Preserve security-sensitive behavior around package-name validation, patch tag parsing, and report JSON compatibility; add regression tests for signed patch-tag suffixes and escaped slash ArtifactName values.

Impact

Representative benchmark improvements from the final runs include:

  • Trivy fixed-version selection: ~83% faster, allocations eliminated.
  • Large Trivy parse: ~44% faster, ~74% fewer allocations.
  • OS package-name validation: ~97% faster.
  • APK/Pacman/RPM validation paths: ~48-52% faster.
  • Bulk tag discovery paths: ~68-99% faster depending on strategy.
  • Default target-tag resolution: ~99% faster.
  • Bulk report index lookup: ~98% faster with zero allocations.

Full benchmark notes and raw progress tracking were kept in /tmp/perf-copa.md during development.

Validation

  • go test ./pkg/... -count=1
  • git diff --check
  • Targeted benchmark suites for:
    • pkg/report
    • pkg/pkgmgr
    • pkg/patch / pkg/buildkit
    • pkg/bulk
  • python3 /Users/sozercan/.codex/skills/autoreview/scripts/autoreview --mode local
    • Final result: autoreview clean: no accepted/actionable findings reported

Notes

  • make lint was run; it still reports pre-existing repository-wide lint issues unrelated to this change set (mostly goconst, plus existing gosec, govet, gocritic, and staticcheck findings). New visible lint issues introduced while developing this change were fixed before commit.

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.26829% with 189 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.85%. Comparing base (28e1fe0) to head (cee497f).

Files with missing lines Patch % Lines
pkg/bulk/skip.go 60.71% 56 Missing and 21 partials ⚠️
pkg/report/trivy.go 75.18% 21 Missing and 13 partials ⚠️
pkg/bulk/discover.go 70.96% 18 Missing and 9 partials ⚠️
pkg/bulk/engine.go 19.04% 17 Missing ⚠️
pkg/buildkit/buildkit.go 75.51% 8 Missing and 4 partials ⚠️
pkg/pkgmgr/rpm.go 81.25% 3 Missing and 3 partials ⚠️
pkg/pkgmgr/apk.go 60.00% 2 Missing and 2 partials ⚠️
pkg/pkgmgr/pacman.go 60.00% 2 Missing and 2 partials ⚠️
pkg/patch/platform.go 88.88% 1 Missing and 2 partials ⚠️
pkg/pkgmgr/pkgmgr.go 91.30% 1 Missing and 1 partial ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1638      +/-   ##
==========================================
+ Coverage   44.04%   44.85%   +0.80%     
==========================================
  Files          58       58              
  Lines       10282    10557     +275     
==========================================
+ Hits         4529     4735     +206     
- Misses       5436     5466      +30     
- Partials      317      356      +39     
Files with missing lines Coverage Δ
pkg/pkgmgr/dpkg.go 34.36% <90.90%> (ø)
pkg/pkgmgr/pkgmgr.go 69.62% <91.30%> (+3.66%) ⬆️
pkg/report/report.go 83.87% <66.66%> (-2.10%) ⬇️
pkg/patch/platform.go 80.00% <88.88%> (+2.68%) ⬆️
pkg/pkgmgr/apk.go 79.86% <60.00%> (-2.38%) ⬇️
pkg/pkgmgr/pacman.go 75.52% <60.00%> (-2.56%) ⬇️
pkg/pkgmgr/rpm.go 66.06% <81.25%> (-0.35%) ⬇️
pkg/buildkit/buildkit.go 21.15% <75.51%> (+5.24%) ⬆️
pkg/bulk/engine.go 8.37% <19.04%> (+0.04%) ⬆️
pkg/bulk/discover.go 60.00% <70.96%> (-4.11%) ⬇️
... and 2 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes CPU/allocation hot paths across Copa's report parsing, package-manager validation/manifest parsing, platform/BuildKit helpers, and bulk patch tag/report handling. It replaces regex- and strings.Split/bufio.Scanner-based code with lower-allocation equivalents (strings.Cut, hand-rolled scanners, precomputed lookup maps/sets, cached templates), while aiming to keep observable behavior identical. It also adds an optional ParseWithPackageTypes fast path so the Trivy parser can skip work for filtered pkg-types, and adds focused microbenchmarks and a couple of regression tests (signed patch-tag suffixes, escaped-slash ArtifactName).

Changes:

  • Rewrote version selection/comparison, package-name validation, and OS manifest parsing with allocation-conscious implementations while preserving prior semantics.
  • Added ParseWithPackageTypes (via a new optional interface) to avoid computing OS/library updates that pkg-types filtering would discard.
  • Added benchmark suites for pkg/report, pkg/pkgmgr, pkg/patch, pkg/buildkit, and pkg/bulk, plus regression tests in pkg/bulk/skip_test.go.
Show a summary per file
File Description
pkg/report/trivy.go Replaces parseVersion/compareVersions/getHighestVersion/parseVersionParts with allocation-free candidate structs; adds ParseWithPackageTypes with includeOS/includeLibrary gating; hoists regexes/special-package map to package vars.
pkg/report/report.go Adds optional packageTypeScanReportParser interface and dispatches to ParseWithPackageTypes when supported.
pkg/report/trivy_benchmark_test.go New benchmarks for optimal fixed-version selection and large Trivy report parsing.
pkg/pkgmgr/pkgmgr.go Replaces regex package-name validation with isValidOSPackageName; adds shared splitManifestLines; presizes maps/slices.
pkg/pkgmgr/rpm.go Cut-based parseRPMTools/parseManifestFile, index-based version extraction in validateRPMPackageVersions, sort.Slice/sort.Strings.
pkg/pkgmgr/pacman.go Uses splitManifestLines and index-based prefix/version extraction; non-stable sorts.
pkg/pkgmgr/dpkg.go strings.Cut-based manifest field parsing with presized map.
pkg/pkgmgr/apk.go Uses splitManifestLines and index-based prefix/version extraction; non-stable sorts.
pkg/pkgmgr/pkgmgr_bench_test.go New benchmarks for validation/parse helpers.
pkg/patch/platform.go Precomputes arch-tag suffixes and normalized platform specs; map-keyed filterPlatforms; string-concat archTag.
pkg/patch/platform_benchmark_test.go New benchmarks for platform helpers.
pkg/buildkit/buildkit.go Extracts descriptorFromRawManifest/platformImageReferenceFromManifest; typed manifest-list decoding with startsWithJSONArray guard and hex digest.
pkg/buildkit/buildkit_benchmark_test.go New unit tests + benchmarks for manifest descriptor/reference helpers.
pkg/bulk/skip.go Caches arch-tag suffix set; replaces regex tag matching with numeric parsing; adds a hand-rolled top-level JSON string extractor guarded by json.Valid; adds fast-path lookup.
pkg/bulk/discover.go Replaces excludeTags/semver.Collection with a set + custom tagVersion parsing/compare and semver.NewVersion fallback.
pkg/bulk/engine.go Caches tag templates via sync.Map; default-tag fast path; string-concat repository/summary building.
pkg/bulk/skip_test.go Regression tests for signed patch-tag suffixes and escaped-slash ArtifactName.
pkg/bulk/bulk_benchmark_test.go New benchmarks for discovery, report index build/lookup, and summary printing.

Review details

  • Files reviewed: 18/18 changed files
  • Comments generated: 0
  • Review effort level: Medium

sozercan added 2 commits July 1, 2026 21:07
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

2 participants