Newtype Index Pattern In Zig

45 points by Johz


weberc2

Note well that memory savings are evenly spread out. Using indexes makes every data structure slightly more compact, which improves performance across the board, regardless of hotspot distribution. It’s hard to notice a potential for such saving in a profiler, and even harder to test out.

This should be pretty easy to test out—if you have any end-to-end benchmarks or performance measurement support for your application, you should be able to just change your index types from 32 to 64 bit and see how much performance degrades.

peter-leonov

First, they save memory. Typically a 32-bit index is enough, a saving of four bytes per pointer on 64-bit architectures. I haven’t seen this measured, but my gut feeling is that this is much more impactful than it might initially seem.

v8 team at google posted on pointer compression (with some stats), which is specifically impactful for "everything is a reference" languages like JS.

aapoalas

I've recently found one reason to prefer index + count in storing a collection of fields that can either have constants or an AST: for the AST case I'd store a slice of the Nodes array as index and count, and I'd detect the constant case by a zero count: the index would then be an index to a separate constants array instead.