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] Enable heterogeneous access for LRU cache #48954

Merged
merged 14 commits into from
Dec 3, 2024

Conversation

dentiny
Copy link
Contributor

@dentiny dentiny commented Nov 27, 2024

As titled, allow heterogeneous lookup and insertion.

Signed-off-by: hjiang <[email protected]>
@dentiny dentiny added the go add ONLY when ready to merge, run all tests label Nov 28, 2024
src/ray/util/tests/shared_lru_test.cc Outdated Show resolved Hide resolved
@@ -90,7 +96,8 @@ class SharedLruCache final {
// Delete the entry with key `key`. Return true if the entry was found for
// `key`, false if the entry was not found. In both cases, there is no entry
// with key `key` existed after the call.
bool Delete(const Key &key) {
template <typename KeyLike>
bool Delete(KeyLike &&key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a move in Delete? It's possible user wants to delete then still use its key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also even if you want move semantics you want to pass in by value?

Copy link
Contributor Author

@dentiny dentiny Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's perfect forward with universal reference not move.

Copy link
Contributor

@dayshah dayshah Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can forward instead of pass by value in headers, slightly more efficient, but only makes sense for Put where you can possibly forward rvalue in, underlying cache_ functions used for Get and Delete take const lvalue anyways so it doesn't matter...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put takes ownership of the key, so I take it by value;
Deletion and lookup only borrows reference, so perfect forwarding.

src/ray/util/shared_lru.h Show resolved Hide resolved
src/ray/util/shared_lru.h Outdated Show resolved Hide resolved
@@ -34,22 +47,21 @@ TEST(SharedLruCache, PutAndGet) {

// Check put and get.
cache.Put("1", std::make_shared<std::string>("1"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write a comment about the heterogeneous key is tested here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't use heterogeneous access for put operation, since the ownership is transferred anyway.

TestClass obj2{"hello"};
SharedLruCache<TestClass, std::string> cache{2};
cache.Put(obj1, std::make_shared<std::string>("val"));
auto val = cache.Get(obj2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we want to test cache.Get(absl::string_view{"hello"}) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, heterogeneous lookup is provided by flat hash map implementation.
To achieve that, we need to implement more features, like comparison between TestClass and std::string.
Reference: https://github.com/abseil/abseil-cpp/blob/c7cf999bda8390d2dd294ef903716a80135e6f4c/absl/container/flat_hash_map.h#L86-L94

Copy link
Contributor Author

@dentiny dentiny Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intended use case for heterogeneous lookup here is C++ native string types, including std::string_view, std::string and const char*.

For example, for a flat_hash_map<string, string>, you don't want to create a temporary string for lookup right? A string view, if possible, should suffice.

All of them have been implemented by hash map. Unfortunately not for STL unordered map implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, can we have a test case that heterogeneous lookup is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already did?
The above PutAndGet test case is using std::string for storage type, but std::string_view for delete and lookup type?

Copy link
Contributor

@dayshah dayshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but same q as ruiyang of why forwarding into get and and delete?

@dentiny
Copy link
Contributor Author

dentiny commented Dec 2, 2024

@rynewang / @dayshah
I'm using universal reference and perfect forward, not rvalue overload and move semantics.
You could check reference: https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers

@dentiny
Copy link
Contributor Author

dentiny commented Dec 2, 2024

lgtm but same q as ruiyang of why forwarding into get and and delete?

The thread-safe implementation should perfect forward what's provided into the non-thread-safe version, whatever value category it belongs to.

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

dentiny commented Dec 2, 2024

@rynewang I have updated the implementation based on our offline discussion.

@rynewang rynewang enabled auto-merge (squash) December 2, 2024 23:54
@dentiny dentiny changed the title [core] Customized hash and eq for LRU cache [core] Enable heterogeneous access for LRU cache Dec 3, 2024
@rynewang rynewang merged commit 24acab2 into ray-project:master Dec 3, 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.

4 participants