←back to thread

Indices, not Pointers

(joegm.github.io)
102 points vitalnodo | 3 comments | | HN request time: 0.226s | source
Show context
skulk ◴[] No.45111204[source]
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)
replies(3): >>45112462 #>>45112631 #>>45114374 #
Animats ◴[] No.45112631[source]
The trouble is, you've just replicated the problems of raw pointers. You can have dangling indices if the underlying object is reused or cleared. You can have aliasing, with two indices to the same object.

It's a problem in practice. Of the three times I've ever had to use a debugger on Rust code, two came from code someone had written to do their own index allocation. They'd created a race condition that Rust would ordinary prevent.

replies(3): >>45113452 #>>45121307 #>>45124000 #
IshKebab ◴[] No.45113452[source]
You don't replicate all the problems of raw pointers. You can't have type confusion or undefined behaviour. It's totally memory safe. That's a pretty huge difference.

But I agree, it does give up some of the benefits of using native references.

replies(1): >>45121140 #
1. Animats ◴[] No.45121140[source]
True. In some ways, that's worse. Instead of crashes, you get something working on the wrong data.

I had one bug in a renderer where I'd see object shadows moving across a 3D scene, but not the object casting the shadow. Didn't crash. That was hard to find. A dead index was still in use, and it pointed to something valid enough to be drawn.

replies(2): >>45123983 #>>45124005 #
2. ◴[] No.45123983[source]
3. 10000truths ◴[] No.45124005[source]
Use-after-frees and ABA problems can be addressed by using generation counts with your object pool. Rust's slotmap crate handles this out of the box, AFAIK.