Skip to content

Const-ify hashing of Key. #241

Open
Open
@tobz

Description

@tobz

A while back, we added support to pre-generate the hash of a Key in order to speed up its hashing performance when used with metrics_util::Registry. One part that I was never truly satisfied with was that we had to accomplish it by using atomics so that we could store the hash after construction. This was required because:

  • we had no way to do hashing in a const fn (had to be hashed on first access)
  • Key is passed around as &Key, so it has to be doable with only an immutable reference available

Since then, many features related to const fns have become stabilized. While not all of the necessary features are stabilized yet, I was able to craft a solution (a lot of unsafe code, unfortunately) that can do entirely const hashing of a key: https://gist.github.com/tobz/fec84b1ac850aeaef216608a8585c982

We should pay attention to the next few Rust releases to see what other const features get stabilized. The primary grossness of the approach taken in the gist relates to std::slice::from_raw_parts not having a stabilized const impl, so we reimplemented a lot of the union trickery that gets used to safely transmute between *const u8 and *const [u8], which provides the basis of jumping to *const str/&str. A recent PR was merged for that which should hopefully land in 1.58.0, but there might be other useful stabilizations that let us clean up more of the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-coreComponent: core functionality such as traits, etc.E-complexEffort: complex.S-waiting-on-upstreamStatus: issue/PR requires an upstream change to either a dependency or Rust itselfT-enhancementType: enhancement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions