servo / uluru

A simple, fast, LRU cache implementation.
Mozilla Public License 2.0
190 stars 20 forks source link

Add support for returning displaced entries for re-use. #22

Closed Byron closed 2 years ago

Byron commented 2 years ago

I added this to be able to avoid allocations if the entry is allocating, In case of gitoxide this saves a couple of millions of allocations in favour of a few thousand re-allocs.

Performance wasn't measurably better in my case, just as if the allocator can already repurpose previously freed slabs of memory pretty well or at only a very low cost.

Since it doesn't seem to hurt providing this capability, I am creating this PR anyway in the hopes it does indeed make sense to some in this form or another.

Please let me know :).

Byron commented 2 years ago

Here is how the altered method can be used.

mbrubeck commented 2 years ago

Thanks, this is great. I don't think it needs to be an optional feature, though. You can use core::mem::replace in no_std crates. Also, changing the signature of a function when a feature is enabled could technically break other crates in the same dependency graph that depend on the type of the function.

I'll merge this PR, and then push a follow-up to make it always use the new method.

mbrubeck commented 2 years ago

Released:

https://github.com/servo/uluru/releases/tag/v3.0.0 https://crates.io/crates/uluru/3.0.0

Byron commented 2 years ago

Absolutely awesome, thank you!

Also thanks for removing the feature toggle, there is so much I don't know about no-std, but I will get there (probably when gitoxide goes WASM :) ).