1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use std::collections::{VecDeque, hash_map::DefaultHasher};
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use crate::sync::spin_lock::SpinLock;
/// A resource with a queryable byte size or capacity.
pub trait SizeBounded {
/// The size or capacity of the resource in bytes.
fn size(&self) -> u64;
}
/// Helper to map an arbitrary size to its power-of-two bin index.
#[inline]
fn bin_index(size: u64) -> usize {
if size <= 1 {
0
} else {
// Subtract 1 so that exact powers of two fall into the exact bin,
// e.g. 1024 -> 10, 1025 -> 11.
64 - (size - 1).leading_zeros() as usize
}
}
struct Shard<T> {
// 64 bins, representing power-of-two size classes (2^0 to 2^63).
// SpinLock is cache-line aligned, preventing false sharing.
bins: [SpinLock<VecDeque<T>>; 64],
retained_bytes: AtomicU64,
retained_count: AtomicUsize,
}
impl<T> Shard<T> {
fn new() -> Self {
let mut bins_vec = Vec::with_capacity(64);
for _ in 0..64 {
bins_vec.push(SpinLock::new(VecDeque::new()));
}
let bins: [SpinLock<VecDeque<T>>; 64] = bins_vec
.try_into()
.unwrap_or_else(|_| panic!("invariant: failed to convert vector of 64 bins"));
Self {
bins,
retained_bytes: AtomicU64::new(0),
retained_count: AtomicUsize::new(0),
}
}
}
/// A sharded, binned resource pool designed for high-concurrency reuse of transient allocations.
///
/// Resources are partitioned by thread affinity across 4 shards to minimize lock contention,
/// and internally binned into 64 power-of-two size classes. Pop operations use a non-blocking
/// stealing fallback across shards.
pub struct ShardedResourcePool<T> {
shards: [Shard<T>; 4],
shard_max_buffers: usize,
shard_max_bytes: u64,
#[cfg(test)]
test_hook: test_support::Hook,
}
impl<T> std::fmt::Debug for ShardedResourcePool<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ShardedResourcePool")
.field("shard_max_buffers", &self.shard_max_buffers)
.field("shard_max_bytes", &self.shard_max_bytes)
.finish_non_exhaustive()
}
}
impl<T: SizeBounded> ShardedResourcePool<T> {
/// Construct a new pool with the given capacity limits.
#[must_use]
pub fn new(max_buffers: usize, max_bytes: u64) -> Self {
Self {
shards: [Shard::new(), Shard::new(), Shard::new(), Shard::new()],
shard_max_buffers: (max_buffers / 4).max(1),
shard_max_bytes: max_bytes / 4,
#[cfg(test)]
test_hook: test_support::Hook::new(),
}
}
/// Retrieve the thread-local shard index.
#[inline]
fn get_shard_index() -> usize {
thread_local! {
// clippy 1.97.0 false positive: initialiser is already
// `const { Cell::new(None) }`. Retire when toolchain advances
// past the regression (ATLAS-MNEMOSYNE-CI-1).
#[allow(clippy::missing_const_for_thread_local)]
static THREAD_SHARD_INDEX: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) };
}
THREAD_SHARD_INDEX.with(|cell| {
if let Some(idx) = cell.get() {
idx
} else {
let thread_id = std::thread::current().id();
let mut hasher = DefaultHasher::new();
thread_id.hash(&mut hasher);
let idx = (hasher.finish() as usize) % 4;
cell.set(Some(idx));
idx
}
})
}
/// Retrieve a resource of size >= `size` from the pool, or return `None`.
pub fn take_at_least(&self, size: u64) -> Option<T> {
let local_idx = Self::get_shard_index();
let start_bin = bin_index(size);
// Try local shard first
let local_shard = &self.shards[local_idx];
if local_shard.retained_count.load(Ordering::Acquire) > 0
&& local_shard.retained_bytes.load(Ordering::Acquire) >= size
{
// 1. Search the start_bin for a buffer >= size (since start_bin contains elements of varying sizes)
{
let mut guard = local_shard.bins[start_bin].lock();
if let Some(pos) = guard.iter().rposition(|item| item.size() >= size) {
let item = guard.remove(pos).expect("element exists at pos");
let item_size = item.size();
local_shard
.retained_bytes
.fetch_sub(item_size, Ordering::Release);
local_shard.retained_count.fetch_sub(1, Ordering::Release);
return Some(item);
}
}
// 2. Search larger bins (all elements in larger bins are guaranteed to be >= size)
for b in (start_bin + 1)..64 {
let mut guard = local_shard.bins[b].lock();
if let Some(item) = guard.pop_back() {
let item_size = item.size();
local_shard
.retained_bytes
.fetch_sub(item_size, Ordering::Release);
local_shard.retained_count.fetch_sub(1, Ordering::Release);
return Some(item);
}
}
}
// Steal from other shards using non-blocking try_lock
for i in 1..4 {
let other_idx = (local_idx + i) % 4;
let other_shard = &self.shards[other_idx];
// Fast path check: if the other shard does not have any items or does not have enough bytes, skip it.
if other_shard.retained_count.load(Ordering::Acquire) == 0
|| other_shard.retained_bytes.load(Ordering::Acquire) < size
{
continue;
}
// 1. Search start_bin of other shard
if let Some(mut guard) = other_shard.bins[start_bin].try_lock()
&& let Some(pos) = guard.iter().rposition(|item| item.size() >= size)
{
let item = guard.remove(pos).expect("element exists at pos");
let item_size = item.size();
other_shard
.retained_bytes
.fetch_sub(item_size, Ordering::Release);
other_shard.retained_count.fetch_sub(1, Ordering::Release);
return Some(item);
}
// 2. Search larger bins of other shard
for b in (start_bin + 1)..64 {
if let Some(mut guard) = other_shard.bins[b].try_lock()
&& let Some(item) = guard.pop_back()
{
let item_size = item.size();
other_shard
.retained_bytes
.fetch_sub(item_size, Ordering::Release);
other_shard.retained_count.fetch_sub(1, Ordering::Release);
return Some(item);
}
}
}
None
}
/// Recycle a resource back into the pool.
pub fn recycle(&self, item: T) {
let size = item.size();
if size > self.shard_max_bytes || self.shard_max_buffers == 0 {
return;
}
let local_idx = Self::get_shard_index();
let local_shard = &self.shards[local_idx];
let bin_idx = bin_index(size);
// The target-bin guard covers reservation through publication. `clear`
// acquires every bin guard before draining or resetting counters, so it
// cannot publish a zero-counter state between these two mutations.
let mut target_guard = local_shard.bins[bin_idx].lock();
// Reserve this item's count and bytes up front, before inserting, so the
// eviction decision below sees a total that already includes this item
// *and* every other concurrent recycler's in-flight contribution. The
// prior load-decide-insert sequence read the counters, decided no
// eviction was needed, then inserted — allowing N concurrent recyclers to
// each skip eviction and overshoot the shard cap by up to N-1 buffers
// (and exceed the byte budget). `fetch_add` returns the pre-add value, so
// `+ 1` / `+ size` is this shard's total with our reservation applied.
let mut current_count = local_shard.retained_count.fetch_add(1, Ordering::AcqRel) + 1;
let mut current_bytes = local_shard.retained_bytes.fetch_add(size, Ordering::AcqRel) + size;
// Evict oldest items (FIFO) until the shard — counting our reserved item —
// is within both limits, or no further eviction is possible. The local
// `current_*` counters are decremented per eviction (rather than
// re-loaded) so the loop terminates under sustained concurrent recycling
// instead of chasing a moving atomic snapshot; a single item always fits
// because `size <= shard_max_bytes` and `shard_max_buffers >= 1`.
let mut evicted = Vec::new();
while current_count > self.shard_max_buffers || current_bytes > self.shard_max_bytes {
let mut progress = false;
for b in 0..64 {
if b == bin_idx {
if let Some(removed) = target_guard.pop_front() {
let removed_size = removed.size();
// Decrements remove already-inserted items, never our
// reservation, so the net total keeps counting our item.
local_shard.retained_count.fetch_sub(1, Ordering::Release);
local_shard
.retained_bytes
.fetch_sub(removed_size, Ordering::Release);
current_count -= 1;
current_bytes = current_bytes.saturating_sub(removed_size);
evicted.push(removed);
progress = true;
break;
}
} else if let Some(mut guard) = local_shard.bins[b].try_lock()
&& let Some(removed) = guard.pop_front()
{
let removed_size = removed.size();
// Decrements remove already-inserted items, never our
// reservation, so the net total keeps counting our item.
local_shard.retained_count.fetch_sub(1, Ordering::Release);
local_shard
.retained_bytes
.fetch_sub(removed_size, Ordering::Release);
current_count -= 1;
current_bytes = current_bytes.saturating_sub(removed_size);
evicted.push(removed);
progress = true;
break;
}
}
if !progress {
break;
}
}
#[cfg(test)]
self.test_hook.pause_after_reservation(local_idx, bin_idx);
// The counters already account for this item (reserved above); inserting
// it makes the bin contents consistent with the published totals.
target_guard.push_back(item);
drop(target_guard);
drop(evicted);
}
/// Clear all pooled resources.
///
/// All bin guards remain held until the bins are drained and the counters
/// are reset. This makes the reset a linearization point: a concurrent
/// `recycle` or `take_at_least` either completes before the reset or starts
/// after it, and cannot publish a resource behind zero counters.
pub fn clear(&self) {
for (shard_idx, shard) in self.shards.iter().enumerate() {
#[cfg(not(test))]
let _ = shard_idx;
let mut guards: [Option<_>; 64] = std::array::from_fn(|_| None);
for (bin_idx, bin) in shard.bins.iter().enumerate() {
#[cfg(test)]
self.test_hook.announce_clear(shard_idx, bin_idx);
guards[bin_idx] = Some(bin.lock());
}
let mut evicted = Vec::new();
for guard in guards.iter_mut().flatten() {
evicted.extend(guard.drain(..));
}
shard.retained_bytes.store(0, Ordering::Release);
shard.retained_count.store(0, Ordering::Release);
drop(guards);
drop(evicted);
}
}
#[cfg(test)]
pub(crate) fn install_test_hook(
&self,
recycle_entered: std::sync::mpsc::SyncSender<()>,
clear_started: std::sync::mpsc::SyncSender<()>,
release: std::sync::Arc<std::sync::Barrier>,
) -> test_support::HookGuard {
self.test_hook
.install(recycle_entered, clear_started, release)
}
}
#[cfg(test)]
pub(crate) mod test_support {
use std::sync::{Arc, Barrier, Mutex, mpsc::SyncSender};
struct InterleavingHook {
recycle_entered: SyncSender<()>,
clear_started: SyncSender<()>,
release: Arc<Barrier>,
target: Option<(usize, usize)>,
clear_announced: bool,
}
pub(crate) struct Hook {
state: Arc<Mutex<Option<InterleavingHook>>>,
}
impl Hook {
pub(crate) fn new() -> Self {
Self {
state: Arc::new(Mutex::new(None)),
}
}
pub(crate) fn install(
&self,
recycle_entered: SyncSender<()>,
clear_started: SyncSender<()>,
release: Arc<Barrier>,
) -> HookGuard {
let mut hook = self
.state
.lock()
.expect("invariant: test hook mutex poisoned");
assert!(
hook.is_none(),
"invariant: only one interleaving hook is active"
);
*hook = Some(InterleavingHook {
recycle_entered,
clear_started,
release,
target: None,
clear_announced: false,
});
HookGuard {
state: Arc::clone(&self.state),
}
}
pub(crate) fn pause_after_reservation(&self, shard_idx: usize, bin_idx: usize) {
let (entered, release) = {
let mut hook = self
.state
.lock()
.expect("invariant: test hook mutex poisoned");
let Some(hook) = hook.as_mut() else {
return;
};
assert!(
hook.target.replace((shard_idx, bin_idx)).is_none(),
"invariant: only one recycle interleaving is active"
);
(hook.recycle_entered.clone(), Arc::clone(&hook.release))
};
entered
.send(())
.expect("invariant: interleaving test receiver remains active");
release.wait();
}
pub(crate) fn announce_clear(&self, shard_idx: usize, bin_idx: usize) {
let started = {
let mut hook = self
.state
.lock()
.expect("invariant: test hook mutex poisoned");
let Some(hook) = hook.as_mut() else {
return;
};
if hook.target == Some((shard_idx, bin_idx)) && !hook.clear_announced {
hook.clear_announced = true;
Some(hook.clear_started.clone())
} else {
None
}
};
if let Some(started) = started {
started
.send(())
.expect("invariant: interleaving test receiver remains active");
}
}
}
pub(crate) struct HookGuard {
state: Arc<Mutex<Option<InterleavingHook>>>,
}
impl Drop for HookGuard {
fn drop(&mut self) {
self.state
.lock()
.expect("invariant: test hook mutex poisoned")
.take();
}
}
}