rust-lang / rust-lang/hashbrown

Returning / Accepting hash codes

Open
#455 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3k
Forks
358
Avg merge
11h 57m
Merged PRs (30d)
2

Description

Hello everyone, how much of cpu is eaten by make_hash with default hasher?
Would it make sense to let user cache hashcode and let later reuse it if hse is able to?
I could imagine alternative insert / get methods which accept/return hash code alongside with a key.

e.g.

pub fn insert(&mut self, k: K, v: V) -> Option<V> {
    let hash = make_hash::<K, S>(&self.hash_builder, &k); // <-- we could let user pass it as arg if it's override and return to let user cache it
    let hasher = make_hasher::<_, V, S>(&self.hash_builder);
    match self
        .table
        .find_or_find_insert_slot(hash, equivalent_key(&k), hasher)
    {
        Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().1 }, v)),
        Err(slot) => {
            unsafe {
                self.table.insert_in_slot(hash, slot, (k, v));
            }
            None
        }
    }
}
fn get_inner<Q: ?Sized>(&self, k: &Q) -> Option<&(K, V)>
where
    Q: Hash + Equivalent<K>,
{
    if self.table.is_empty() {
        None
    } else {
        let hash = make_hash::<Q, S>(&self.hash_builder, k);// <-- same here, optionally accept as arg instead of recalculating and return to let user cache it
        self.table.get(hash, equivalent_key(k))
    }
}

On reddit @thermiter36 suggested raw_entry API but I honestly didn't get it how can I use it for hash code caching, maybe it's easy if one knows how?
pls discuss.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing raw_entry API and the insert/get_inner paths shown in the examples. Determine whether they can support passing and returning cached hash codes, and clarify the proposed API and expected behavior. Because this is an unanswered design discussion, maintainer agreement is needed before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.