Expand description
A global allocator that turns an accidental heap allocation on a shard path into a crash.
Y7 says there is no global allocator call on a command path. That is easy to
write down and impossible to keep by review once more than one person is in
the codebase, because the allocating constructs in Rust are the comfortable
ones: format!, to_vec, collect, Box::new, a Vec that grows, a
String built to make an error message. Each costs tens of nanoseconds
against a 150 ns budget, and none of them looks wrong in a diff.
So the rule is enforced instead of reviewed. A shard thread marks itself
enter_no_alloc before the command loop, and from that point any
allocation aborts the process with a message naming the size. Setup, arena
growth and anything else that legitimately needs the heap wraps itself in
allow, which is a visible, greppable, deliberate act.
§Cost when it is off
The check is one thread local load and a branch, on a path that already
calls into the system allocator. It is not measurable next to malloc.
Non shard threads never set the flag and pay the same single branch.
§Using it
#[global_allocator]
static ALLOC: YoAlloc = YoAlloc::new();The engine installs this in its binaries and in its test and bench targets.
A library consumer of yodb does not get it, because choosing a global
allocator is the application’s call and never a library’s.
Structs§
- YoAlloc
- The allocator. Delegates to the system allocator and checks the flag first.
Functions§
- allow
- Run
fwith allocation permitted, then restore the previous state. - enter_
no_ alloc - Mark this thread as a shard thread: from here on, allocating aborts.
- exit_
no_ alloc - Undo one
enter_no_alloc. - is_
forbidden - Whether allocation is currently forbidden on this thread.