Improve performance of limit and throttle packages#7231
Improve performance of limit and throttle packages#7231michel-laterman wants to merge 2 commits into
Conversation
Adds benchmarks establishing allocation baselines for two hot paths: - Limiter.Acquire: current implementation allocates a new method value (heap escapes l.release) on every call when maxLimit is set; local limiterPreBound shows 0-alloc alternative via pre-bound releaseFunc - writeError: json.Marshal allocates on every 429 response; local writeErrorStatic shows 0-alloc alternative via pre-computed bodies - Throttle.Acquire: current *Token return allocates 32 B per acquire; local acquireValue shows (Token, bool) keeps Token on the stack Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_ac289332-cb6f-4471-827a-686196f1398c
|
This pull request does not have a backport label. Could you fix it @michel-laterman? 🙏
|
limit.Acquire: add releaseFunc field to Limiter, bound once at NewLimiter time. Acquire now returns the pre-bound field directly instead of capturing l.release as a new method value on each call. Eliminates 1 heap allocation (16 B) per Acquire when maxLimit is set. limit.writeError: replace json.Marshal of an anonymous struct with pre-computed []byte bodies. Eliminates 2 allocations (~128 B) on every 429 response. throttle.Acquire: change return from *Token to (Token, bool). Token no longer escapes to the heap on each successful acquire, eliminating 1 allocation (32 B) per call. Update caller in handleArtifacts.go and all tests accordingly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_ac289332-cb6f-4471-827a-686196f1398c
bc8b162 to
5c3b3ca
Compare
TL;DRThe Mac unit-test job did not run any tests. Buildkite build 15171 failed during checkout because it fetched PR #7231 at current head Remediation
Investigation detailsRoot CauseThis is an infrastructure/stale-status checkout failure. The job fetched Evidence
The same checkout failure repeated for all 3 retry attempts. GitHub currently reports PR #7231 head as Verification
Follow-up
What is this? | From workflow: PR Buildkite Detective Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not. |
|
buildkite run perf-tests |
What is the problem this PR solves?
Reduce allocations that limit and throttle packages use per request.
How does this PR solve the problem?
Limiter.releaseFuncattribute to avoid a heap allocation whenever a limiter with a defined max value is acquiredThrottle.Acquireto returnToken, boolinstead of*Tokenin order to avoid the token being allocated to heapHow to test this PR locally
mage test:benchmarkBenchmark findings
Memory profile findings
Design Checklist
I have ensured my design is stateless and will work when multiple fleet-server instances are behind a load balancer.I have included fail safe mechanisms to limit the load on fleet-server: rate limiting, circuit breakers, caching, load shedding, etc.Checklist
I have made corresponding changes to the documentationI have made corresponding change to the default configuration files./changelog/fragmentsusing the changelog tool