←back to thread

Indices, not Pointers

(joegm.github.io)
102 points vitalnodo | 7 comments | | HN request time: 0.4s | source | bottom
1. 10000truths ◴[] No.45111473[source]
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.

replies(3): >>45112164 #>>45112905 #>>45114542 #
2. cma ◴[] No.45112164[source]
Data memory-dependent prefetchers like in Apple's newer chips I think only work with full pointers and not offsets, so it could be a decent perf hit.
replies(3): >>45112323 #>>45112400 #>>45113187 #
3. astrange ◴[] No.45112323[source]
Indices can be much smaller than pointers (which are 8 bytes), so they have plenty of cache advantages of their own which can make up for that.
4. ◴[] No.45112400[source]
5. hinkley ◴[] No.45112905[source]
Someone regaled me with a story of the distributed computing system on the NeXT machines that utilized 64 bit pointers, where the upper bytes were the machine address and the lower bytes the memory address on that machine.
6. whstl ◴[] No.45113187[source]
Still depends. If the indices are pointing to a dense, flat, contiguous array, it will still be faster than following pointers into scattered heap allocations, with or without prefetching, because of how CPU caching works.
7. vanderZwan ◴[] No.45114542[source]
The second point is implicitly present in the example given at the end of the "Less Allocation Overhead" section. Copying all nodes from one backing arraylist to a larger one like requires the possibility of relocation.