Skip to content

Commit 0a34d92

Browse files
committed
src: Preallocate a number of stacks on first launch() to actually leverage pool allocator
1 parent ba304c1 commit 0a34d92

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mod unfurl;
1919
pub use linger::*;
2020

2121
const QUANTUM_MICROSECS: u64 = 100;
22+
23+
const STACK_N_PREALLOC: usize = 511;
2224
const STACK_SIZE_BYTES: usize = 2 * 1_024 * 1_024;
2325

2426
#[cfg(test)]

src/stacks.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::marker::PhantomData;
22
use std::ops::Deref;
33
use std::ops::DerefMut;
4+
use super::STACK_N_PREALLOC;
45
use super::STACK_SIZE_BYTES;
56
use timetravel::stable::StableAddr;
67
use timetravel::stable::StableMutAddr;
@@ -9,13 +10,25 @@ use reusable::ReusableSync;
910
pub fn alloc_stack() -> ReusableSync<'static, Box<[u8]>> {
1011
use compile_assert::assert_sync;
1112
use reusable::SyncPool;
13+
use std::collections::LinkedList;
1214
use std::convert::TryInto;
1315
use std::sync::Once;
1416

1517
static mut STACKS: Option<SyncPool<Box<[u8]>>> = None;
1618
static INIT: Once = Once::new();
17-
INIT.call_once(|| unsafe {
18-
STACKS.replace(SyncPool::new(|| Some(vec![0; STACK_SIZE_BYTES].into_boxed_slice())));
19+
INIT.call_once(|| {
20+
let stacks: fn() -> _ = || Some(vec![0; STACK_SIZE_BYTES].into_boxed_slice());
21+
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+
29+
unsafe {
30+
STACKS.replace(stacks);
31+
}
1932
});
2033

2134
let stacks = unsafe {

0 commit comments

Comments
 (0)