Skip to content

⚡ Bolt: Pre-allocate distro family map and single-pass signature line stripping in discovery - #139

Merged
mayankpande88 merged 3 commits into
mainfrom
bolt-optimize-discovery-facts-pack
Aug 13, 2026
Merged

⚡ Bolt: Pre-allocate distro family map and single-pass signature line stripping in discovery#139
mayankpande88 merged 3 commits into
mainfrom
bolt-optimize-discovery-facts-pack

Conversation

@blue4209211

Copy link
Copy Markdown
Contributor

💡 What

  • Pre-allocated Distro Family Map (pkg/proxy/discovery/facts.go): Moved static byID distro family map literal to package-level osFamilyByID variable.
  • Single-Pass Signature Line Stripping (pkg/proxy/discovery/pack.go): Updated ParseAndVerify to call splitSignatureLine once, extracting the signed payload body and counting top-level signature lines in a single pass.
  • Journal Entry (.jules/bolt.md): Documented critical performance learning regarding package-level pre-allocation of lookup maps.

🎯 Why

  • osFamily() is called on every host probe during discovery sweeps. Defining the map literal inside the function body forced Go to allocate an 18-element hash map on the heap (~1.2 KB and 3 allocations) every single time.
  • ParseAndVerify() previously called countSignatureLines(raw) and SignedBytes(raw) separately, causing splitSignatureLine to parse and normalize lines twice per pack verification.

📊 Impact

  • BenchmarkParseFacts execution time: 41% faster (925.5 ns/op -> 545.1 ns/op)
  • BenchmarkParseFacts memory allocated: 52% reduction (2,272 B/op -> 1,080 B/op)
  • BenchmarkParseFacts allocations: 30% reduction (10 allocs/op -> 7 allocs/op)
  • BenchmarkParseAndVerify memory: 587 B saved per pack verify
  • BenchmarkParseAndVerify allocations: 5 fewer allocs/op

🔬 Measurement

Run benchmarks via go test -bench='BenchmarkParseFacts|BenchmarkParseAndVerify' -benchmem ./pkg/proxy/discovery.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces performance optimizations by pre-allocating static lookup maps at the package scope in facts.go and optimizing signature verification in pack.go to use a single-pass extraction. Additionally, benchmarks have been added to track these performance improvements. Feedback suggests replacing the package-level map with a switch-based helper function to guarantee immutability and prevent potential race conditions while maintaining zero allocations.

Comment thread pkg/proxy/discovery/facts.go Outdated
@blue4209211

Copy link
Copy Markdown
Contributor Author

Addressed review feedback: Replaced package-level var osFamilyByID map with an immutable switch-based helper function osFamilyByID(id string) (string, bool). Benchmark confirms zero allocations and faster execution (518.9 ns/op vs 545.1 ns/op).

@blue4209211

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces performance optimizations to reduce heap allocations and redundant parsing. Specifically, it replaces a local map literal in osFamily with a zero-allocation switch statement, optimizes signature line parsing in ParseAndVerify to avoid duplicate passes, and adds benchmarks to verify these improvements. Feedback suggests updating the documentation in .jules/bolt.md to accurately reflect the use of switch statements as a highly optimized alternative to package-scope maps.

Comment thread .jules/bolt.md Outdated
@blue4209211

Copy link
Copy Markdown
Contributor Author

/gemini review

@blue4209211

Copy link
Copy Markdown
Contributor Author

Addressed journal review feedback: Updated .jules/bolt.md to explicitly recommend using switch statements for zero-allocation, immutable static lookups.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces performance optimizations in the discovery package, including replacing static map literals with switch statements in osFamily to achieve zero heap allocations, and refactoring pack signature verification to extract the signed body and count signature lines in a single pass. Additionally, benchmarks are added to measure these changes. The reviewer notes that while splitSignatureLine reduces duplicate parsing, it still presents a performance bottleneck due to string conversions and regex operations, and suggests using a proper parser or anchoring the regex match to avoid false positives on indented lines.

Comment thread pkg/proxy/discovery/pack.go
@mayankpande88
mayankpande88 merged commit 0b3638f into main Aug 13, 2026
5 of 6 checks passed
@mayankpande88
mayankpande88 deleted the bolt-optimize-discovery-facts-pack branch August 13, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants