Hi all,
I've been implementing matklad's Static Allocation, Constant Work as a small Rust glancer (like a mini rust-analyzer).
The goal is a classic TigerBeetle pattern: INIT -> FREEZE -> SERVE
- Allocate everything at startup (
StubTable::with_capacity()sized like--orders-max) - Call
GATE.freeze()to deny any further heap growth - SERVE with zero heap allocation and constant P100 latency
What I built
1. Allocation-free termination path (mod sys)
A 1KB stack buffer + StackWriter + direct write(2) / WriteFile. So deny() itself never allocates. Inspired by the need to avoid allocation in OOM handler.
2. Gatekeeper Global Allocator
#[global_allocator] static GATE: Gate = Gate::new(); impl Gate { pub fn freeze(&self) { /* store counters + FROZEN.store(true, Release) */ } } unsafe impl GlobalAlloc for Gate { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { if is_frozen() { deny("alloc", layout); } // ... } unsafe fn realloc(&self, ..) { if is_frozen() && new_size > old { deny("realloc(grow)", ..) } } unsafe fn dealloc(&self, ..) { /* allowed - so Drop doesn't fail */ } } After freeze, post_freeze_allocs is asserted to be 0.
3. Constant-work core
TABLE_SLOTS=256, MAX_PROBES=32(~17.9KB, fits in L1d). No earlybreak, always probes 32 slots.ActiveFileArenaisconst fninline[AstNode; 32], notBox, so active file parsing is zero-alloc even in INIT.SymbolNamewithlen==0reserved exclusively forReservedsentinel,from_bytesvalidates UTF-8 and 1..=32 length.- Sorbet-style shim:
apply_derive_shim(b"Order", b"Serialize")instead of running proc-macro.
Full source here:https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b0c20821e98449d1d3e27ec7c5d43605
It prints:
[glancer] frozen=true init_allocs=3 init_bytes=18468 post_freeze_allocs=0 post_freeze_bytes=0 [work ] parsed_nodes=7 nodes_scanned=32 total_probes=1024 resolved=4 errors=2 reserved=26 [ok ] line=4 ident=incoming aux=Order -> stub_slot=41 [ok ] line=5 ident=book_level aux=PriceLevel -> stub_slot=100 [ok ] line=6 ident=Order aux=Serialize -> stub_slot=169 [diag ] line=7 code=MissingTraitImpl ident=PriceLevel aux=Serialize [ok ] line=8 ident=match_order aux= -> stub_slot=81 [diag ] line=9 code=UnresolvedFn ident=unindexed_helper aux= My questions for the community
-
Is allowing
deallocafter freeze idiomatic? I allow it soDropimpls don't panic, but strict TigerStyle says no free at all. MyBoxlives until exit so it never actually frees, but is this a sound compromise? -
Probe window trade-off: Plain linear probing without
breakgives deterministic work and prefetch, but can return"probe window full"even ifused < capacity. I choseMAX_PROBES=32for 256 slots at 0.5 load. Would you prefer Robin Hood or just document the trade-off? -
Box<[T]>vs[T; N]forStubTable: I keptBoxto allowwith_capacity(cli_args)sizing, whileActiveFileArenais fully inline. Is this hybrid considered "static allocation" in Rust community, or should I go fully static with const generics? -
#![deny(unsafe_op_in_unsafe_fn)]compliance: I wrapped allSystem::alloccalls inunsafe {}blocks with SAFETY comments. Anything I missed?
Any feedback on making this more idiomatic TigerStyle would be appreciated!
Thanks!
2 posts - 1 participant