Skip to content

perf(policy): avoid per-sample heap allocations in eviction sampling#490

Open
nishantmehta wants to merge 1 commit into
dgraph-io:mainfrom
nishantmehta:perf/fillsample-value-slice
Open

perf(policy): avoid per-sample heap allocations in eviction sampling#490
nishantmehta wants to merge 1 commit into
dgraph-io:mainfrom
nishantmehta:perf/fillsample-value-slice

Conversation

@nishantmehta

Copy link
Copy Markdown

What

sampledLFU.fillSample builds the eviction candidate pool as []*policyPair, allocating a fresh policyPair on the heap for every sampled key. This runs on the eviction path — every Set once the cache is full — so a write-heavy workload pays lfuSample (5) small allocations per eviction round.

policyPair is a 16-byte pure value type (two integers), and the sample is only read (to find the minimum-hit victim) and swap-removed — it is never retained or mutated through the pointer. Storing the pairs by value in the caller-sized slice keeps the single backing-array allocation while removing all the per-element ones.

Benchmark

BenchmarkSampledLFUFillSample   80 B/op -> 0 B/op,  5 allocs/op -> 0 allocs/op,  ~50% faster (79ns -> 39ns)

(benchstat, n=10, p=0.000.)

Testing

Full test suite passes, including -race. No API or behavioral change.

fillSample built the eviction candidate pool as []*policyPair, allocating
a new policyPair on the heap for every sampled key. This runs on the
eviction path (every Set once the cache is full), so a write-heavy workload
pays lfuSample (5) small allocations per eviction round.

policyPair is a 16-byte value type (two integers) and the sample is only
read (to find the minimum-hit victim) and swap-removed, never retained or
mutated through the pointer. Storing the pairs by value in a caller-sized
slice keeps the single backing-array allocation while removing all the
per-element ones.

BenchmarkSampledLFUFillSample: 80 B/op -> 0 B/op, 5 allocs/op -> 0
allocs/op, ~50% faster (79ns -> 39ns).

Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
@CLAassistant

CLAassistant commented Jun 28, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@nishantmehta nishantmehta marked this pull request as ready for review June 28, 2026 21:08
@nishantmehta nishantmehta requested a review from a team as a code owner June 28, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants