File tree Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -14,19 +14,21 @@ thread_local! {
14
14
static CACHE : RefCell <Cache > = RefCell :: new( Cache :: default ( ) ) ;
15
15
}
16
16
17
- /// Configure the caching behaviour.
17
+ /// Configure the caching and eviction behaviour.
18
18
pub fn config ( config : Config ) {
19
19
CACHE . with ( |cache| cache. borrow_mut ( ) . config = config) ;
20
20
}
21
21
22
- /// Configuration for caching behaviour.
22
+ /// Configuration for caching and eviction behaviour.
23
23
pub struct Config {
24
24
max_age : u32 ,
25
25
}
26
26
27
27
impl Config {
28
28
/// The maximum number of evictions an entry can survive without having been
29
29
/// used in between.
30
+ ///
31
+ /// Default: 5
30
32
pub fn max_age ( mut self , age : u32 ) -> Self {
31
33
self . max_age = age;
32
34
self
@@ -40,6 +42,10 @@ impl Default for Config {
40
42
}
41
43
42
44
/// Evict cache entries that haven't been used in a while.
45
+ ///
46
+ /// The eviction behaviour can be customized with the [`config`] function.
47
+ /// Currently, comemo does not evict the cache automatically (this might
48
+ /// change in the future).
43
49
pub fn evict ( ) {
44
50
CACHE . with ( |cache| {
45
51
let mut cache = cache. borrow_mut ( ) ;
Original file line number Diff line number Diff line change 1
- //! Tracked memoization.
1
+ /*!
2
+ Memoization on steroids.
3
+
4
+ A _memoized_ function caches its return values so that it only needs to be
5
+ executed once per set of unique arguments.
6
+ While this only works for _pure_ functions (functions that don't have side
7
+ effects), it can improve performance in many computations.
8
+
9
+ However, memoization is not a silver bullet.
10
+ In many cases, the cache hit rate isn't all that great because arguments are
11
+ ever so slightly different.
12
+ This is where comemo comes into play:
13
+ It let's you reuse a function's output even when called with
14
+ _different_ arguments as long as the difference is not observed by the function.
15
+ It achieves this by carefully tracking which parts of an argument were actually
16
+ accessed during the invocation of a memoized function.
17
+ */
2
18
3
19
mod cache;
4
20
mod constraint;
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ use siphasher::sip128::{Hasher128, SipHasher};
19
19
/// a == b -> hash(a) == hash(b)
20
20
/// ```
21
21
/// The inverse implication does not follow from this immediately. However,
22
- /// _comemo_ uses high-quality 128 bit hashes in all places. This reduces the
22
+ /// comemo uses high-quality 128 bit hashes in all places. This reduces the
23
23
/// risk of a hash collision to an absolute minimum. Therefore, this trait
24
24
/// additionally provides `PartialEq` and `Eq` implementations that compare by
25
25
/// hash instead of by value.
You can’t perform that action at this time.
0 commit comments