Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up DefaultHasher, SipHasher, and SipHasher13. #69152

Commits on Feb 23, 2020

  1. Optimize sip::Hasher::short_write.

    This commit changes `sip::Hasher` to use the faster `short_write` approach
    that was used for `SipHasher128` in rust-lang#68914.
    
    This has no effect because `sip::Hasher::short_write` is currently
    unused. See the next commit for more details, and a fix.
    
    (One difference with rust-lang#68914 is that this commit doesn't apply the
    `u8to64_le` change from that PR, because I found it is slower, because
    it introduces `memcpy` calls with non-statically-known lengths.
    Therefore, this commit also undoes the `u8to64_le` change in
    `SipHasher128` for this reason. This doesn't affect `SipHasher128` much
    because it doesn't use `u8to64_le` much, but I am making the change to
    keep the two implementations consistent.)
    nnethercote committed Feb 23, 2020
    Configuration menu
    Copy the full SHA
    f272983 View commit details
    Browse the repository at this point in the history
  2. Add write_* methods for DefaultHasher, SipHasher, SipHasher13

    …, `sip::Hasher`.
    
    `sip::Hasher::short_write` is currently unused. It is called by
    `sip::Hasher::write_{u8,usize}`, but those methods are also unused,
    because `DefaultHasher`, `SipHasher` and `SipHasher13` don't implement
    any of the `write_xyz` methods, so all their write operations end up
    calling `sip::Hasher::write`.
    
    I confirmed this by inserting a `panic!` in `sip::Hasher::short_write`
    and running the tests -- they all passed.
    
    This commit adds all the `write_xyz` methods to `DefaultHasher`,
    `SipHasher`, `SipHasher13` and `sip::Hasher`, which means that
    `short_write` will be used for all of them.
    
    (I haven't properly implemented 128-bit writes in `sip::Hasher` and
    `SipHasher128`. That could be a follow-up if something thinks it is
    important.)
    
    Some microbenchmarks I wrote show that this commit speeds up default
    hashing of integers by about 2.5x, and can speed up hash tables that use
    `DefaultHasher` and have keys that contain integers by up to 30%, though
    the exact amount depends heavily on the details of the keys and how the
    table is used. This makes default `Hash{Map,Set}` much more likely to be
    competitive with `FxHash{Map,Set}`.
    nnethercote committed Feb 23, 2020
    Configuration menu
    Copy the full SHA
    96c4e42 View commit details
    Browse the repository at this point in the history