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

[core] Use ref hash to avoid key copy #49023

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

dentiny
Copy link
Contributor

@dentiny dentiny commented Dec 3, 2024

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.

@dentiny dentiny requested review from jjyao, rynewang and dayshah December 3, 2024 02:29
@dentiny dentiny force-pushed the hjiang/lru-map-utils branch from d32684e to 8894533 Compare December 3, 2024 02:30
@dentiny dentiny added the go add ONLY when ready to merge, run all tests label Dec 3, 2024
@dentiny dentiny force-pushed the hjiang/lru-map-utils branch from 8894533 to d2f64c2 Compare December 3, 2024 02:43
Copy link
Contributor

@rynewang rynewang left a 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>;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@dentiny
Copy link
Contributor Author

dentiny commented Dec 3, 2024

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.

I'm not sure what you're worrying about? The only overhead is the reference wrapper deference.

Is this saved memory space worth the extra compute time cost?

I'm not sure the indirection (aka. reference wrapper deference) could be saved easily from somewhere else.
For example, the shared pointer cleanup I'm working on these days. I'm not worried about the performance cost.

@dentiny dentiny requested a review from rynewang December 3, 2024 08:03
@dentiny
Copy link
Contributor Author

dentiny commented Dec 3, 2024

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?

Some numbers:

  • a cache miss and memory lookup is 10s of nanoseconds, which is far far far less than ray scheduling overhead
  • in constrast, considering the only existing use key is serialized runtime env, we don't have a hard cap on how large the serialized string could be, I think saving a key storage is more important

Some reference:

Signed-off-by: hjiang <[email protected]>
@dentiny dentiny requested a review from rynewang December 3, 2024 23:34
@rynewang rynewang enabled auto-merge (squash) December 3, 2024 23:46
@rynewang rynewang merged commit 80e698d into ray-project:master Dec 4, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants