Crates.io | cachers |
lib.rs | cachers |
version | 0.1.0 |
source | src |
created_at | 2018-06-13 19:14:22.724243 |
updated_at | 2018-06-13 19:14:22.724243 |
description | A caching library |
homepage | https://gitlab.com/alexsnaps/cachers |
repository | https://gitlab.com/alexsnaps/cachers |
max_upload_size | |
id | 69995 |
size | 37,682 |
I've been toying around with the idea of implementing a low & predictable latency caching library in Rust for a while. It's interesting to me from a couple of perspectives: having low-level control over memory layout, while honouring Rust's type system and its guarantees; as well as trying to reconcile the efficiency guarantees, with that level of control, and possibly supporting pluggable eviction algorithms while not hurting performance. The latter I actually think isn't possible: i.e. some sacrifice on the design side on the altar of performance will forever be required, which will come at odds with the pluggable eviction. But I still would like to toy with the challenge.
WARNING: This is all just me fooling around as of now. I'm merely trying to get a thing that works & exposes a somewhat sensible API. Once I have that sorted out, I'll iterate over the implementation and start making it faster...
std::hash
CacheThrough
CacheThrough
, maybe a cache-aside?)I think the v0.x
releases should inform how to resolve the tension between API & performance.
The key part in this I think, but that's the point behind the whole project, is to be able to walk the clock hand in a CPU cache friendly way. Probably requiring the clock bit information to be held in a different data structure than the cache entries themselves. This will come at a slight overhead on cache hits to update the bit.
The other interesting part is making this work in an optimistic way with regards to coordination primitives. Hence the idea to use interior mutability for cache entries, but fall to different strategies for the eviction data; possibly lowering the guarantees for these.
Finally, trying to plug another eviction algorithm (probably LRU, which requires a different approach to storing the eviction data) and see how that affects the design.
<1> cache
is not mut
, and <K, V>
is inferred; the cache would contain at most size
entries;
<2> key
in this case would be a miss, invoking populating_fn
only once, whether cache
even if cache was shared
across threads all trying to access the same key
. populating_fn
returns the value Some<V>
to populate entry for
key: K
. The .get
then returns a Option<Arc<V>>
, with None
if the populating_fn
returned a None
<3> .udpate
forces an update to the key
, whether it was already present or not. updating_fn
receives the key but
also, unlike populating_fn
, a Option<Arc<V>>
that represents the previous value if present; .update
then returns
the updated V
<4> remove
invalidates the mapping to key
without proposing a new value. Which is effectively the equivalent of an
.update(key, |_, _| None)
. I wonder if this is even necesary... it's here tho!