Repository navigation
Conversation
This commit allows creating LruCaches that are limited in size by something other than a count of entries. It does this by providing a new trait, `Meter`, where implementations of the trait provide a `measure` method to do the measurement. The default behavior of measuring by the count of entries is provided by a `Count` type. Due to difficulties with mutating entries making it possible to change an item's measure, the `get_mut` and `iter_mut` methods are provided only on LruCaches using `Count` for sizing. A bit of optimization is provided for the default case--to avoid having to redundantly store the item count the `Meter` trait allows implementations to provide a type for storing the measurement, and the `Count` implementation uses `()`. The `CountableMeter` trait allows `LruCache` to work with this or any `Meter` implementation that uses `usize` for measurement. Finally an optional feature is added, `heapsize`, which uses the heapsize crate to provide HeapSizeOf, a measurement based on the heap memory usage of values stored in the cache.
|
Thanks for the pull request, and welcome! The contain-rs team is excited to review your changes, and you should hear from @bluss (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. |
|
The Concerns:
Alternatively we could declare pub trait Meter<K: Hash + Eq, V> {
type Measure: Ord + Copy;
/// Accumulate a key-value pair into the current measure.
fn add(&mut self, key: &K, val: &V);
/// Remove a key-value pair from the current measure.
fn remove(&mut self, key: &K, val: &V);
/// Remove value measure (for when an old value exists)
fn remove_val(&mut self, val: &V);
/// Fetch the current measure.
fn current(&self) -> Self::Measure;
}And then also a // maybe doc(hidden)?
/// Meter where the current measure can be derived from the cache.
pub trait DerivableMeter<K: Hash + Eq, V> {
// ...all same as `Meter` except for this:
fn current<S: BuildHasher>(&self, cache: &LruCache<K, V, S, Self>) -> Self::Measure;
}
// trivial implementation
impl<K: Hash + Eq, V, M> DerivableMeter<K, V> for M where M: Meter<K, V> { ... }Then the cache is defined as pub struct LruCache<K: Hash + Eq, V, S: BuildHasher, M: DerivableMeter<K, V>> {
map: ...,
max_measure: M::Measure,
meter: M,
}and fn insert(&mut self, key: K, val: V) -> Option<V> {
self.meter.add(&key, &val);
let old_val = self.map.insert(key, val);
if let Some(ref val) = old_val {
self.meter.remove_val(val);
}
while self.meter.current(&*self) > self.max_measure {
self.remove_lru()
}
old_val
}
edit: miswrote; this whole idea was designed around using |
|
That Travis failure looks like a transient network issue: "Unable to update registry https://github.com/rust-lang/crates.io-index" |
The intention was for I don't really see how your |
FYI, I don't see how that could be made to work with |
|
@luser yeah, in the suggestion above |
|
Can I reassign this to you @rphmeier? |
|
Sure thing @bluss |
|
@rphmeier There was some back and forth there, but are there specific changes you'd like to see to this PR? I'm happy to fix things, I just don't think I understand what you're looking for. |
|
Ping @rphmeier |
|
|
||
| impl<K, V> Meter<K, V> for Count { | ||
| /// Don't store anything, the measurement can be derived from the map. | ||
| type Measure = (); |
| /// assert_eq!(cache.get_mut(&2), Some(&mut "c")); | ||
| /// ``` | ||
| pub fn contains_key<Q: ?Sized>(&mut self, key: &Q) -> bool | ||
| pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> |
There was a problem hiding this comment.
The main blocker here is that get_mut is only implemented when the meter is Count. I can't think of a single use case of a cache which doesn't need get_mut for regular operation.
What do you think of adding that via a CacheGuard<'a, V: 'a> which recalculates and updates the measure on drop?
There was a problem hiding this comment.
(the suggestion of non-mutable get is simply not sufficient IMO, we need get_mut to do this right)
|
@luser has forked the repo for their purposes, and we don't seem to have the bandwidth for this kind of work anymore. Closing. |
This commit allows creating LruCaches that are limited in size by
something other than a count of entries. It does this by providing a
new trait,
Meter, where implementations of the trait provide ameasuremethod to do the measurement. The default behavior ofmeasuring by the count of entries is provided by a
Counttype.Due to difficulties with mutating entries making it possible to change
an item's measure, the
get_mutanditer_mutmethods are providedonly on LruCaches using
Countfor sizing.A bit of optimization is provided for the default case--to avoid having
to redundantly store the item count the
Metertrait allows implementationsto provide a type for storing the measurement, and the
Countimplementationuses
(). TheCountableMetertrait allowsLruCacheto work with thisor any
Meterimplementation that usesusizefor measurement.Finally an optional feature is added,
heapsize, which uses the heapsizecrate to provide HeapSizeOf, a measurement based on the heap memory usage
of values stored in the cache.
Thanks to @rphmeier for the design suggestions!
Since this makes
get_mutnot available on caches that aren't using the defaultCountmetric, you will probably want to get non-mutable get merged as well to mitigate that.One issue that's not addressed by this change is providing the list of removed items on an
insertorset_capacitythat requires removing multiple items. This would make my use case simpler, so I may implement that as a follow-up once this PR is finished.