News for hackers. Stuff that matters.
Re:Announcing Rust 1.20 by a_t48 on Thursday August 31, 2017 @08:16PM (#15145158)
A hashmap being implemented on top of vectors is a pretty common implementation. Not sure what Rust uses though.
Re:Announcing Rust 1.20 by cesarb on Friday September 01, 2017 @09:31AM (#15148165)
IIRC, Rust's HashMap uses a single raw vector containing for each entry its hash code, its key, and its value; empty entries have a special hash code as a marker, with the key/value left uninitialized. Also, the hashes are kept together (separate from the rest) for better cache behavior during the linear probing.
Re:Announcing Rust 1.20 by a_t48 on Friday September 01, 2017 @11:05AM (#15148944)
Wouldn't that be two parallel vectors? (Side note: nobody ever thinks to bring up cache behavior in interviews where I ask about how a hash map could be implemented - it's nice to know that the library writers care :) )

How does that special hash marker work? What happens when something actually hashes to it? Just silently increment the hash?

Re:Announcing Rust 1.20 by cesarb on Saturday September 02, 2017 @09:19AM (#15155829)
> Wouldn't that be two parallel vectors?

Yes, but it's a single contiguous memory allocation. (It used to be three parallel vectors in a single allocation, with keys and values also kept separate to avoid padding between them, but experiments showed that had worse cache behavior.)

> How does that special hash marker work? What happens when something actually hashes to it? Just silently increment the hash?

Looking at the code, it always sets the most significant bit of every real hash value (since the least significant bits select the bucket, it makes no difference), and the marker has the most significant bit clear (in fact, all bits of the marker value are clear).

Re:Announcing Rust 1.20 by a_t48 on Sunday September 03, 2017 @10:19PM (#15165222)
Oh, clever. Of course you can do it that way.
Re:Announcing Rust 1.20 by __s on Thursday August 31, 2017 @08:26PM (#15145203)
Problem is avoiding having to do Vec<Option<T>>
Re:Announcing Rust 1.20 by Animats on Friday September 01, 2017 @01:29AM (#15146353)
If T is a reference type, doesn't Rust do an optimization where <Option<T>>> is implemented by using null pointers?
Re:Announcing Rust 1.20 by kevincox on Friday September 01, 2017 @03:41AM (#15146826)
Yes. This optimization is expected to be expanded in the future but there are currently "NonZero" types. Rust notices this and uses the zero value as the enum descriminant.

So in this case the code generated is idential to a nullable pointer.

Re:Announcing Rust 1.20 by sanxiyn on Friday September 01, 2017 @04:00AM (#15146893)
Yes, but that doesn't help when T is not a reference type.
Re:Announcing Rust 1.20 by Animats on Friday September 01, 2017 @06:13PM (#15152785)
If it's not a reference type, then the Option enum value is probably small relative to the rest of the data in the type.
Re:Announcing Rust 1.20 by __s on Saturday September 02, 2017 @07:29PM (#15158815)
Option<i32> is 8 bytes