Indices, Not Pointers: A Zig Performance Trick
2025-09-03
A novel approach in Zig uses indices instead of pointers in data structures, resulting in significant performance gains. By storing nodes in a dynamic array and referencing them via indices, this technique reduces memory allocation overhead, lowers memory usage, speeds up access times, and makes freeing instantaneous. This is particularly beneficial for node-based structures like trees, and is used in Zig's compiler for efficient ASTs. While removing individual nodes requires additional handling (e.g., a freelist), the overall performance boost is substantial.
Development