Most important best practice for me: never use the name allocator, always be precise about the contract:
gpa— the code must calldefer gpa.freeto free stuff uparena— the data will be bulk-deallocated at the appropriate system boundary, do not call free.scratch— private space for function. Unlikearena, it’s definitely incorrect to return scratch data.
You can imagine use all there at the same time:
fn handle(
request: *Request,
arena: Allocator,
gpa: Allocator,
scratch: Allocator,
) Response
arenais for request-scoped stuff, that’s whereResponseis allocated in.gpais for data that outlives a single request. For example, if requests share a cache, data in the cache is managed viagpascratchis for data that is required to computeResponse, but isn’t actual response. For example, if you need to sort something, you can use scratch to allocate that buffer.
As a fun example of that, TigerBeetle is built on top of gpa abstraction (we do free stuff, which is important for VOPR), but we use arena as our production implementation of gpa: