marmeladema / clru-rs Goto Github PK
View Code? Open in Web Editor NEWAn LRU cache implementation with constant time operations and weighted semantic.
License: MIT License
An LRU cache implementation with constant time operations and weighted semantic.
License: MIT License
If I understand correctly, FixedSizeList::free
contains a list of position in FixedSizeList::nodes
that are free to be used. If I also understand correctly, the order in FixedSizeList::nodes
is irrelevant for the logic, since elements are sorted via the FixedSizeListNode::prev
/::next
links.
I wonder because during review I noticed that free
is filled with elements in descending order in the constructor and in ascending order in clear
.
Then, isn't FixedSizeList::free
better modelled as a set, which has no order and elements must be unique?
It should be possible to implement a CLruCache::resize
method to shrink or grow the capacity of the cache.
There is actually two cases:
Growing: it should be easy, its just a matter of increasing the size of the boxed slice in the FixedSizeList
Shrinking: its more complicated and one idea might be to first provide a re-order / sort internal API such that the node in the FixedSizeList
would be sorted from most recent to least recent (same order as iteration) and then truncating the boxed slice of nodes.
It looks like get
requires the argument has the same lifetime as it's key.
use clru::CLruCache;
use std::collections::HashMap;
struct Store<'a> {
index: HashMap<&'a str, String>,
cache: CLruCache<&'a str, String>,
}
impl<'a> Store<'a> {
fn foo(&mut self, key: String) -> String {
self.index.get(&key.as_str()).unwrap().to_owned()
}
fn bar(&mut self, key: String) -> String {
self.cache.get(&key.as_str()).unwrap().to_owned()
}
}
How can I get the method bar
to compile?
I firstly opend a same issue at lru-rs. Then I found this crate and tried it. But it also doesn't compile. Maybe I did not use it in a correct way?
It would be nice to integrate some form of code coverage now that the compiler supports source-based code coverage: https://blog.rust-lang.org/inside-rust/2020/11/12/source-based-code-coverage.html
If the modification will not change the weight I see no reason to prevent mutable access just because there is a weight scale.
I understand the basic motivation but it makes it impossible to use the crate for my intended use. I don't want or need the overhead of interior mutability.
Same issue as described here: jeromefroe/lru-rs#32. Would you be interested in implementing the solution proposed here?
If such a change is desired, that means the total number of elements is not known anymore. So this would require some changes and probably tradeoffs. Is this something you would like to see happening in this crate?
It should offer greater flexibility and is a step toward #24
Hey, I was interested in using this crate in one of projects, however I need CLruCache
to be Sync
, as I want to share the cache between threads. Would you be willing to either changing to be using Arc
internally or providing a sync
version of CLruCache
?
Currently CLruCache
never implements Send
trait because Key<K>
uses Rc
internally. However in this crate, Key
/Rc
is always owned by CLruCache
and never leaves the structure which means that CLruCache
could in theory implement Send
(assuming K
, V
, S
and W
are all Send
).
Implementing Send
would allow for usage of &Mutex<CLruCache<...>>
from multiple threads.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.