Should Dict avoid GC allocations for isbits key/value types?

greatpet
Mon, 09 Dec 2024 12:22:00 +0000
Internals & Design

Resizing arrays, when elements have primitive types or more generally isbits types, does not trigger any GC, as this is handled by the C runtime. Dictionaries are also allocation-free during most operations, except during rehashing, which happens if you insert more elements than the current capacity. rehash is called to allocate larger arrays inside the Dict object before data from the original arrays are copied over. The original arrays are then discarded and eventually garbage-collected.

It seems that with a little bit of extra work, Dict for primitive and isbits keys/values can completely avoid GC-tracked allocations. You just need to make a C call to malloc in the Dict constructor and call free in the rehashing function and the finalizer.

Would such changes be worthwhile, given that some Julia users want to write allocation-free code? Tooling for this purpose includes e.g. AllocCheck.jl.

4 posts - 3 participants