Allocators: Best Practices & Anti-Patterns

Most important best practice for me: never use the name allocator, always be precise about the contract:

  • gpa — the code must call defer gpa.free to free stuff up
  • arena — the data will be bulk-deallocated at the appropriate system boundary, do not call free.
  • scratch — private space for function. Unlike arena, 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
  • arena is for request-scoped stuff, that’s where Response is allocated in.
  • gpa is for data that outlives a single request. For example, if requests share a cache, data in the cache is managed via gpa
  • scratch is for data that is required to compute Response, 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:

21 Likes