News for hackers. Stuff that matters.
Re:Handles Are the Better Pointers by Animats on Wednesday June 21, 2023 @02:36PM (#36422482)
WGPU, the cross-platform library for Rust, is in the process of going in the other direction. They had index-based handles, and are moving to reference counts.[1] The index tables required global locks, and this was killing multi-thread performance.

[1] https://github.com/gfx-rs/wgpu/pull/3626

Re:Handles Are the Better Pointers by slashdev on Wednesday June 21, 2023 @04:36PM (#36424052)
Maybe that’s a case of the wrong design? The index tables shouldn’t need global locks. It gets a little hairy if you need to be able to reallocate or move them (I.e grow them) but that happens at most a small number of times and there are ways of only taking the lock if that’s happening.

I’ve implemented this pattern without locks or CAS in C++, and it works just fine.

I’m currently using this pattern in rust (although with fixed size) and it works really well. The best part is it bypasses the borrow checker since an index isn’t a reference. So no compile time lifetimes to worry about. It’s awesome for linked lists, which are otherwise painful in rust. Also it can sometimes allow a linked list with array like cache performance, since the underlying layout is an array.

Re:Handles Are the Better Pointers by Animats on Wednesday June 21, 2023 @04:43PM (#36424142)
"it bypasses the borrow checker since an index isn’t a reference"

That's a bug, not a feature. The two times I've had go to looking for a bug in the lower levels of Rend3/WGPU (which are 3D graphics libraries), they've involved some index table being corrupted. That's the only time I've needed a debugger.

Re:Handles Are the Better Pointers by slashdev on Thursday June 22, 2023 @08:18AM (#36430988)
Lifetimes are managed at runtime, somewhat like Arc, just without the ref counts. If implemented well you get an assertion if you misuse a reference (eg use after free.)

I’m fine with the trade off, but yes, it can be a source of difficult bugs.

Re:Handles Are the Better Pointers by loeg on Wednesday June 21, 2023 @07:03PM (#36425931)
We use a similar mechanism, but it is thread-local. Handles get passed to other threads, but the other threads cannot directly access them; instead, they send requests associated with the handle to the owning thread over an MPSC queue. Our construct is not a general purposes replacement for pointers, obviously.
Re:Handles Are the Better Pointers by stkdump on Thursday June 22, 2023 @12:26AM (#36428001)
Sounds a bit like what seastar.io is supposed to enable. On their website they say they focus on IO bound applications. I wonder how well that model fits to parallelize computationally intensive work.
Re:Handles Are the Better Pointers by loeg on Thursday June 22, 2023 @10:52AM (#36433052)
I have no familiarity with Seastar, but a quick look at the marketing: shared nothing, message passing -- that sounds about right.
Re:Handles Are the Better Pointers by nitwit005 on Wednesday June 21, 2023 @11:41PM (#36427770)
I assume there was an underlying assumption of single-threadedness in this post.

You could presumably use the same tricks the allocator is using to handle more concurrent threads, although I suspect that's not worth the time required.