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

  1. Allocate everything at startup (StubTable::with_capacity() sized like --orders-max)
  2. Call GATE.freeze() to deny any further heap growth
  3. 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 early break, always probes 32 slots.
  • ActiveFileArena is const fn inline [AstNode; 32], not Box, so active file parsing is zero-alloc even in INIT.
  • SymbolName with len==0 reserved exclusively for Reserved sentinel, from_bytes validates 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

  1. Is allowing dealloc after freeze idiomatic? I allow it so Drop impls don't panic, but strict TigerStyle says no free at all. My Box lives until exit so it never actually frees, but is this a sound compromise?

  2. Probe window trade-off: Plain linear probing without break gives deterministic work and prefetch, but can return "probe window full" even if used < capacity. I chose MAX_PROBES=32 for 256 slots at 0.5 load. Would you prefer Robin Hood or just document the trade-off?

  3. Box<[T]> vs [T; N] for StubTable: I kept Box to allow with_capacity(cli_args) sizing, while ActiveFileArena is fully inline. Is this hybrid considered "static allocation" in Rust community, or should I go fully static with const generics?

  4. #![deny(unsafe_op_in_unsafe_fn)] compliance: I wrapped all System::alloc calls in unsafe {} blocks with SAFETY comments. Anything I missed?

Any feedback on making this more idiomatic TigerStyle would be appreciated!

Thanks!

2 posts - 1 participant

Read full topic