GithubHelp home page GithubHelp logo

Comments (2)

tkaitchuck avatar tkaitchuck commented on May 20, 2024

@ahornby This is due to a combination of two factors:

  1. Your implementation is not overriding all of the methods of Hasher and delegating them. So some are being implemented via their default implementation.
  2. The very unfortunate implementation of string's hashcode in the standard library.

String's hash function is does a double deref to get a [u8] and delegates to that.
The hash for [u8] is here: https://doc.rust-lang.org/beta/src/core/hash/mod.rs.html#667-672
as you can see it is explicitly passing in the length as a separate parameter. This is extremely unfortunate because it causes a loss in performance, because the Hasher implementations have to assume that such a call is not made and incorporate the length, because this is done in other types including arrays. (The specialization feature of aHash works around this, but requires nightly)
So this method is going to call write_usize(). aHash overrides this method, but your wrapping hasher does not, so it will instead call the default implementation here: https://doc.rust-lang.org/src/core/hash/mod.rs.html#322 which is different from how it is implemented in aHash.

So you can make your test pass if you have NewTypeHasher override every method on Hasher and delegate all of them.
That would work and I would expect it to continue to work. However it is worth noting that it is fragile. If a method is added to the Hash interface with a default provided, (as has happened in the past) then your code would not be broken. But then aHash could override that new method and suddenly you would be in the situation you are in now where the hashes don't match.

from ahash.

ahornby avatar ahornby commented on May 20, 2024

@tkaitchuck thanks for the great explanation! Yep, difference goes away once all methods wrapped.

from ahash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.