Indices, not Pointers

22 vitalnodo 9 9/2/2025, 11:21:30 PM joegm.github.io ↗

Comments (9)

10000truths · 20m ago
There are a couple other advantages that are unstated in the article, yet very important from a software design perspective:

1) Indices are a lot more portable across different environments than pointers. They can be serialized to disk and/or sent over the network, along with the data structure they refer to. Pointers can't even be shared between different processes, since they're local to an address space by design.

2) Indices enable relocation, but pointers restrict it. A struct that stores a pointer to itself cannot be trivially moved/copied, but a struct containing an integer offset into itself can. A live pointer to an object pool element prevents the object pool from being safely moved around in memory, but an index into the object pool does not impose such restriction.

skulk · 59m ago
This is a very tempting and commonly used strategy in Rust to bypass the borrow checker. I've used it to implement tries/DFAs with great success (though I can't find the code anymore)
zahlman · 2h ago
> There is a pattern I’ve learned while using Zig which I’ve never seen used in any other language.

I've done this in small applications in C (where nodes were already being statically allocated) and/or assembly (hacking on an existing binary).

No idea about the effect on speed in general; I was trying to save a few bytes of storage in a place where that mattered.

anonymousiam · 25m ago
But in C, there's not really any difference between pointers and indices. You can access array elements using either method, and they're both equally efficient.
LegionMammal978 · 21m ago
Depending on how many elements you have, you can save some space using 32-bit or even 16-bit indices in place of 64-bit pointers. (Just make sure there isn't any route to overflow the index type.)
throwawaymaths · 38m ago
> There is a pattern I’ve learned while using Zig which I’ve never seen used in any other language.

yeah, i feel like it's low key ECS (minus object/slot polymorphism)

adamnemecek · 11m ago
Generational arenas is the name of the pattern.
quantified · 2h ago
Reinventing malloc to some degree.
femto · 1h ago
malloc with NEAR ad FAR pointers (as was used in MSDOS on processors with segmented memory).