News for hackers. Stuff that matters.
Re:Rust Sucks If I Fail to Write X by comex on Friday February 17, 2017 @03:06PM (#13671267)
You can implement a hash map safely on top of an array; indeed you could do it with `Box<[T]>` today. You would just miss the optimization where missing buckets can be represented by zero in the "hash value" field rather than an explicit Option.
Re:Rust Sucks If I Fail to Write X by Animats on Friday February 17, 2017 @03:51PM (#13671608)
Doesn't Option() on a Rust pointer optimize down to a zero pointer value anyway?

Much of the unsafe Rust code I come across looks like either premature optimization or trying to write C in Rust.

Re:Rust Sucks If I Fail to Write X by Manishearth on Friday February 17, 2017 @04:16PM (#13671777)
> Doesn't Option() on a Rust pointer optimize down to a zero pointer value anyway?

There aren't pointers here though.

But yes, you could use NonZero<T> to mark the hash value as never-zero so that the option gets optimized. You'd still need unsafe code to construct the nonzero, but that's okay.

At least for the Hashmap I don't think the code is premature optimization; it seems to be pretty "idiomatic" unsafe Rust, with unsafe used exactly where needed. I haven't really looked at the btreemap in a while.

Re:Rust Sucks If I Fail to Write X by Animats on Friday February 17, 2017 @04:52PM (#13672017)
t seems to be pretty "idiomatic" unsafe Rust, with unsafe used exactly where needed. I haven't really looked at the btreemap in a while.

The number of excuses associated with unsafe code in Rust is excessive.

Re:Rust Sucks If I Fail to Write X by Manishearth on Friday February 17, 2017 @05:35PM (#13672325)
It's not an excuse, I just don't think that your statement that the hashmap code uses unsafe superfluously is correct.
Re:Rust Sucks If I Fail to Write X by comex on Friday February 17, 2017 @07:03PM (#13672804)
It's a bit more complicated than marking the hash value as NonZero because there are currently two parallel arrays: one for hash values, one for (key, value) pairs. So there's nothing in the second array to indicate that an element is missing. (It used to be three arrays, with the keys and values separate, but that "optimization" was actually a terrible layout for the cache. I'd like to see a further improvement where key types that can be hashed trivially, e.g. integers, ditch the hash array altogether.)

I'm fine with the use of `unsafe` here. A hash table can be implemented using safe Rust, but you can squeeze a few percent more performance out of it by using a small amount of unsafe code, in a way that would be very difficult to make safe, without a super fancy dependent type system like ATS plus a ton of annotation overhead. The unsafe code can be vetted relatively thoroughly and is safe from the outside. That kind of practical compromise seems very much in the spirit of Rust...