-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
[core] Use ref hash to avoid key copy #49023
Conversation
d32684e
to
8894533
Compare
Signed-off-by: hjiang <[email protected]>
8894533
to
d2f64c2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General comment: I am worried about 1 extra indirection this PR introduces. Say the flat map wants to rehash - it needs to do at least O(n) Hashes and Eqs. Now all these operations need a dereference to read the Key in list. Is this saved memory space worth the extra compute time cost?
using KeyHash = absl::container_internal::hash_default_hash<Key>; | ||
using KeyEqual = absl::container_internal::hash_default_eq<Key>; | ||
|
||
using KeyConstRef = std::reference_wrapper<const Key>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of a custom Hash and Equal functor, what about creating a MyWrapper class that contains a std::reference_wrapper and implements Hash and Eq?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It complicated the implementation. For example,
struct KeyReferenceWrapper {
std::reference_wrapper<Key> key_ref;
// Implement hash function: absl::container_internal::hash_default_hash<Key>
// Implement eq function: absl::container_internal::hash_default_eq<Key>
}
don't see benefit.
I'm not sure what you're worrying about? The only overhead is the reference wrapper deference.
I'm not sure the indirection (aka. reference wrapper deference) could be saved easily from somewhere else. |
Some numbers:
Some reference: |
Signed-off-by: hjiang <[email protected]>
Signed-off-by: hjiang <[email protected]>
Signed-off-by: hjiang <[email protected]>
For the current implementation, each key is stored twice in LRU cache, one in list, another in list.
But considering the invariant that, as long as the entry exists in LRU cache, the address of the key never changes (since we're using
std::list
), we could use our self-defined hash wrapper to store key reference in hash map instead of value.