16 ms·
I don't know Zig, but conceptually: a direct pointer is the fastest way to access an object. An arraylist is the fastest dynamic sequence of objects (fattest in
by dataflow 17d ago
I don't know Zig, but conceptually: a direct pointer is the fastest way to access an object. An arraylist is the fastest dynamic sequence of objects (fattest in access, not in growth). You use these when you need the performance. It's not often but it certainly happens. The most trivial example is a string that you append to but still need to pass to a C API in between that expects it to be contagious, but it's far more useful than just for storing characters.
- _bohm 17d agoSure, in cases where you need elements to be contiguous in memory then certainly an unrolled linked list is not appropriate. There's usually not a meaningful performance difference between a pointer deref and an indexed array access, however.
- kllrnohj 17d agoIndexing into an array is direct pointer access, there's just an addition in front of it but it's hard to imagine that showing up at all in even the tightest of benchmark loops
- dataflow 17d agoI can't speak for your imagination, but this absolutely does come up if you're writing high-performance code. Also note that being able to access arbitrary objects (as opposed to objects in the same array) requires storing two pieces of information: an index and a pointer to the beginning of the array. So it can use twice as much memory, which affects your cache etc., though you don't even need that to see the effect.
- pocksuppet 6d agoThe addition is much cheaper than the memory access. The register pressure is worse than the addition. The size reduction of the index (versus the pointer) and resulting cache pressure reduction can outweigh the register pressure and the addition if you are storing many indices.