You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement LRU sampling + SampleStats; clean benchmarks and docs
This change introduces LRU sampling to reduce contention on hot Get paths. Instead of moving an entry to the front of the LRU list on every hit, we do so with probability p. Non‑sampled hits take a read‑only path
(no write lock, no list mutation), which lowers contention and improves throughput under concurrency, at the cost of slightly less precise recency ordering.
Backwards compatibility is preserved. The zero‑value configuration behaves exactly as before: 100% LRU updates on every Get and exact stats. Existing users see no behavior change unless they opt in to the new
settings.
Two new configuration options are added:
- LRUSamplingRate (float64, 0.0–1.0): probability of updating LRU on Get. A zero value is treated as 1.0 (traditional behavior). Choose based on workload; a moderate rate typically yields strong gains with
minimal impact on eviction quality.
- SampleStats (bool): when true, stats are updated only on sampled events and scaled by approximately 1/p so they estimate the totals you would see without sampling. The same sampling decision is reused for both
LRU and stats, preserving the relationship Gets ≈ Hits + Misses. When false (default), stats remain exact.
Documentation is updated to describe the trade‑offs and suggest a practical starting range for LRUSamplingRate.
`agecache` supports reducing lock contention on hot `Get` paths by sampling
36
+
how often LRU positions are updated.
37
+
38
+
- Configure with `Config.LRUSamplingRate` in the range `[0.0, 1.0]`.
39
+
-`0.0` or the zero value behaves like `1.0` (traditional LRU update on every `Get`).
40
+
- Lower values (e.g., `0.2`–`0.25`) can significantly improve throughput under high concurrency, at the cost of approximate LRU ordering.
41
+
- To keep stats inexpensive but useful, you can enable `Config.SampleStats`.
42
+
- When `SampleStats` is `true`, stats counters (Gets/Hits/Misses) are updated only when an LRU update would happen, and are scaled by approximately `1/LRUSamplingRate` so they estimate the unsampled totals.
43
+
- When `SampleStats` is `false` (default), stats are exact but may add contention in very hot paths.
44
+
45
+
Notes:
46
+
- Sampling changes eviction accuracy. For many workloads a rate around `0.2–0.25` is a good starting point; benchmark for your use case.
47
+
- With `SampleStats: true`, values are estimates and may vary slightly; with `false`, they are exact.
0 commit comments