Skip to content

Commit 655d9f1

Browse files
committed
src: Refactor stack preallocation to be reusable for other resource types
This also eases debugging by making preallocation a self-contained operation, such that one can easily pinpoint a safe location to interrogate the pool's length and capacity. This change also appears to reduce the number of libsets necessary to run the launch benchmark from about 700 back down to 511, suggesting that the old preallocation approach was being optimized out!
1 parent 0a34d92 commit 655d9f1

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/reusable.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ where &'a C: SharedMut<Vec<T>> {
8787
builder: builder,
8888
}
8989
}
90+
91+
pub fn prealloc(&'a self, count: usize)
92+
-> StdResult<(), Option<<&'a C as SharedMut<Vec<T>>>::Error>> {
93+
use std::collections::LinkedList;
94+
use std::convert::TryInto;
95+
96+
let swap: LinkedList<StdResult<Reusable<_, _>, _>> = (0..count).map(|_|
97+
self.try_into()
98+
).collect();
99+
for temp in swap {
100+
temp?;
101+
}
102+
Ok(())
103+
}
90104
}
91105

92106
impl<'a, T: Default, C: Default + 'a> Default for Pool<T, fn() -> Option<T>, C>

src/stacks.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use reusable::ReusableSync;
1010
pub fn alloc_stack() -> ReusableSync<'static, Box<[u8]>> {
1111
use compile_assert::assert_sync;
1212
use reusable::SyncPool;
13-
use std::collections::LinkedList;
1413
use std::convert::TryInto;
1514
use std::sync::Once;
1615

@@ -19,13 +18,8 @@ pub fn alloc_stack() -> ReusableSync<'static, Box<[u8]>> {
1918
INIT.call_once(|| {
2019
let stacks: fn() -> _ = || Some(vec![0; STACK_SIZE_BYTES].into_boxed_slice());
2120
let stacks = SyncPool::new(stacks);
22-
23-
let prealloc = &stacks;
24-
let _: LinkedList<Box<_>> = (0..STACK_N_PREALLOC).map(|_|
25-
prealloc.try_into()
26-
.expect("libinger: stack allocator lock was poisoned during init")
27-
).collect();
28-
21+
stacks.prealloc(STACK_N_PREALLOC)
22+
.expect("libinger: stack allocator lock was poisoned during init");
2923
unsafe {
3024
STACKS.replace(stacks);
3125
}

0 commit comments

Comments
 (0)