perf(discovery): pre-parse and cache whenExpr in Collector during Pack validation - #141
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces performance optimizations to the discovery pack parsing and evaluation logic. Key changes include replacing static map lookups with a switch statement to eliminate heap allocations, pre-parsing and caching guard expressions (when expressions) during pack validation to avoid repeated parsing during host sweeps, and optimizing signature verification to avoid duplicate line parsing. Additionally, several benchmarks have been added to track these performance improvements. There are no review comments, so no feedback is provided.
blue4209211
force-pushed
the
bolt-preparse-when-guards
branch
from
August 14, 2026 15:27
26af385 to
fabfb34
Compare
RamanKharchee
approved these changes
Aug 14, 2026
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
⚡ Bolt Performance Optimization: Pre-parse and cache whenExpr in Collector during Pack validation
💡 Problem
During
discovery_inventoryruns across server fleets (up to 5,000 targets per request),runInventorycallspack.Select(res.Facts)for every single host.Inside
Pack.Select(), for every collector with awhenguard,parseWhen(c.When)was previously called repeatedly for every host.parseWhenperforms multiplestrings.Split, string allocations, map lookups, and slice allocations for AST structures ([][]comparison) — even thoughPack.validate()already checked the syntax when the pack was loaded.For a realistic content pack with ~12 guarded collectors across a 1,000-host fleet, this caused 12,000 redundant AST parsing cycles and ~75,000 heap allocations on the inventory sweep path.
⚡ Solution
expr *whenExprfield toCollector.Pack.validate(), stored the compiled*whenExprintoc.exprduring pack validation/verification.Pack.Select(), evaluatedc.exprdirectly (falling back toparseWhen(c.When)ifc.expris nil for defensive backward compatibility with ad-hoc unvalidated structs).runslice inSelect()withmake([]Collector, 0, len(p.Collectors)).BenchmarkPackSelectandBenchmarkExamplePackSelect).📊 Benchmark Results (Apple M2 Pro)
Type of change
How Has This Been Tested?
Ran
make lint(0 issues),go test -race ./...(all packages pass), andgo test -bench="BenchmarkPackSelect|BenchmarkExamplePackSelect" -benchmem ./pkg/proxy/discovery.Checklist
make validatepasses (fmt + lint + test)