Repository navigation
Reduce rule-result allocation churn with a preallocated caller-owned slice - #1021
Conversation
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthrough
ChangesCreateAllRules result behavior
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Refactor Merge Risk: ⚪ Minimal · up to The change is ready to merge after normal checks. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
🛠️ Fix failing CI checks 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Performance Benchmark ResultsNode-Agent Resource Usage
Dedup EffectivenessNo data available. |
There was a problem hiding this comment.
🟢 Approval recommended
The focused optimization preserves existing semantics and is covered by relevant regression and concurrency tests.
0 open findings
What changed in this PR
Optimizes rule creation by replacing repeated slice growth with one exact-size allocation while preserving synchronization and behavior.
Changes:
- Preallocates
CreateAllRulesresults. - Adds regression, concurrency, prefilter, and benchmark coverage.
| File | Description |
|---|---|
pkg/rulemanager/rulecreator/factory.go |
Uses an exact-size caller-owned result slice. |
pkg/rulemanager/rulecreator/factory_allocation_test.go |
Verifies behavior and allocation performance. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
CreateAllRulesgrows a nil slice on every call, repeatedly allocating and copying rule structs. A five-minute node-agent pprof capture attributed 47.29 MB on one node directly to that append loop.Allocate the caller-owned result slice once at its final length. Preserve nil results for empty creators, rule order, shallow copies, lazy prefilter initialization, locking, and visibility of synchronized rule additions, updates, and deletions. Callers can continue filtering results in place without changing the creator. Cache TTL and invalidation behavior are unchanged.
Validation:
A separate matched benchmark on this PR’s main base also shows one allocation per nil/cached-filter call. The allocation counts and byte reductions match the production integration results; timing improves for all cases except the 128-rule non-filter case (+0.5%).
Direct three-node DigitalOcean validation completed with the same stabilized microservices workload, batch size 512, three-minute warmup, and five-minute capture. The runtime dependency versions match except for the rule-preallocation commit. Total allocation bytes/sec fell 14.1%; direct
CreateAllRulesflat allocation bytes/sec fell 81.1%. RSS fell 2.4% and working set fell 4.5%. Frontend requests were 640 versus 686 with zero failures; no collector or detected export/drop errors occurred. Agent and workload restarts stayed zero.Final sampled forced-GC live heap was 8.2% higher. Almost all of that difference already existed at capture start (11.51 MB initially versus 12.06 MB finally, across three agents); live-heap differences are distributed across unchanged initialization/cache/BTF allocation sites. No final live sample was attributed directly to
CreateAllRules. This short sequential run supports an allocation-churn improvement, not a retained-heap improvement or long-term CPU guarantee.