mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Feature Request]Improvements for Hashable trait

Open mzaks opened this issue 4 months ago • 1 comments

Review Mojo's priorities

What is your request?

Current implementation of Hashable trait has a rather traditional design where the developer needs to provide an implementation for __hash__(self: T) -> Int function. This has couple downsides and is avoided by more modern languages like Swift, Rust and Zig.

What are the downsides?

  1. Int as a return type is suboptimal as it has CPU arch specific byte width. Using a signed int with a modulo operation, can lead to unexpected side effects
  2. While implementing a custom struct developers are often tempted to implement a naive hash function, which can lead to bad performance and security issues
  3. The API is based on the call/return paradigm which is sub optimal for nested types. Say we have a Person struct which contains a name, birthdate and addresses fields, where addresses is a vector of Address struct. A hash function of a Person will imply that we first compute the hash values of the fields and then compute a hash of hashes. This produces transient allocations and is generally inefficient.

What is the alternative?

As mentioned above Swift, Rust and Zig define Hashable trait to receive a Hasher which aggregates provided values to a single hash value. This is based on the dataflow paradigm, it helps to keep the hash computation safe and efficient. Furthermore Rust and Zig hash based data structured allow developers to provide their own Hasher implementation which might be more appropriate for their use case. I believe that Mojo should also drive for such flexibility.

What is your motivation for this change?

I believe, with a different design for the Hashable trait, the Mojo language and standard library will become more robust and flexible.

Any other details?

Swift Hashable: https://developer.apple.com/documentation/swift/hashable/hash(into:)-v52 Swift Hasher: https://developer.apple.com/documentation/swift/hasher Rust Hash: https://doc.rust-lang.org/std/hash/trait.Hash.html Rust Hasher: https://doc.rust-lang.org/std/hash/trait.Hasher.html Zig HashMap and HashContext: https://github.com/ziglang/zig/blob/master/lib/std/hash_map.zig

mzaks avatar Feb 11 '24 13:02 mzaks