jonhoo / left-right

A lock-free, read-optimized, concurrency primitive.
Apache License 2.0
1.95k stars 94 forks source link

Efficiently Keeping value sets under a certain size? #32

Closed Sushisource closed 4 years ago

Sushisource commented 5 years ago

This definitely relates to https://github.com/jonhoo/rust-evmap/issues/29

So, all that understood, I'm wondering it there's a particular trick for this usecase that might stay efficient (namely, I want my value sets/vecs to always be N or fewer elements, a sort of LRU cache)? Intuitively it seems this could be implemented in terms of retain and keeping a tiny bit of extra metadata on each item.

Alternatively, I had started implementing this with a get_and, copy, and then update with the new values. Only to realize update doesn't swap out the entire value set, but rather replaces the new set with a one-value set. Adding a replace method that allows you to provide an entire set/vec as the second param would be desirable. I think the update method is a bit unintutive in that respect.

Really like the library! Thanks for your work!

jonhoo commented 5 years ago

Thanks!

I think retain should let you do what you want, no? I just pushed (and will soon release) a change to retain which lets you give an FnMut closure, which should in turn make this a decent amount easier. Note that retain is (now, and should have been in the past) unsafe; you need to make sure that it retains the same elements from the value-set both times it is called!

jonhoo commented 4 years ago

I'm going to close this as I believe retain does what you want.