-
Notifications
You must be signed in to change notification settings - Fork 297
Augment raw_entry
API for indexing use case
#44
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
Comments
That sounds good, I would be happy to accept a PR which adds a new method to You should be able to make it so that the dummy |
54: Raw entry hasher r=Amanieu a=sujayakar This PR allows the user to create a `HashMap` where `K` doesn't implement `Hash`, so long as they are okay with only using the raw entry API. The first step is to move around some methods that didn't actually depend on `K: Hash`. Then, the only non-plumbing change was to defer the `.reserve(1)` call on `raw_entry_mut` to right before insertion. Then, we expose a new method `RawVacantEntryMut::insert_with_hasher` that takes in the custom hashing function and passes it in to `reserve` before inserting. @Amanieu do these API changes merit a version bump? I think we're only allowing these methods to be used on _more_ types, but let me know. Closes #44 Co-authored-by: Sujay Jayakar <[email protected]>
I've run into a hitch with Specifically, I'd like an adjustment from pub fn RawVacantEntryMut::insert_with_hasher<H>(
self,
hash: u64,
key: K,
value: V,
hasher: H,
) -> (&'a mut K, &'a mut V)
where
H: Fn(&K) -> u64, to pub fn RawVacantEntryMut::insert_with_hasher<H>(
self,
hash: u64,
key: K,
value: V,
hasher: H,
) -> (&'a mut K, &'a mut V)
where
H: Fn(&K, &S) -> u64, The impl I'm doing is an interner that requires information outside of the hashmap to properly hash the keys. [gist] (While we're (potentially) touching the function API, is there a reason for it to take |
You can keep the hasher outside the map and use |
I'm writing a string-interning data structure in a memory-constrained environment. The strings effectively have
'static
lifetime, so I don't need to support deallocation. Therefore, I just append null-terminated strings to aVec<u8>
buffer and identify a string by its (u32
) offset into this buffer.Then, we can query a string by its identifier by starting at the given offset and finding the null byte. But, we'd also like to see if a string is already in the buffer and get its offset if so. I'd like to use
hashbrown
for maintaining this relation fromString
tou32
without duplicating each string in memory twice.The raw entry API is almost there for supporting this. When looking up a string, we can compute its hash and then use
RawEntryBuilder::from_hash
.However, doing the same for insertions doesn't quite work. The essential issue is that the raw entry API only lets us control the hash of a single key, and inserting may require recomputing hashes of other keys when resizing.
Today, I get around this by sneaking in a pointer to
buf
via a thread-local to theHash
implementation. I could also stash aRc<RefCell<...>>
to the buffer on the hash builder for the same effect. However, both of these solutions are ugly, and given how close it is, it feels to me like theRawEntry
API should support this use case.Let me know if accommodating this is acceptable and I can submit a PR. It looks like it should be pretty easy since
RawTable
already takes in ahasher: impl Fn(&T) -> u64
for many of its methods.The text was updated successfully, but these errors were encountered: