1
- lru- cache-macros
1
+ cache-macro
2
2
================
3
3
[ ![ Build Status] ( https://travis-ci.org/tylerreisinger/lru-cache-macro.svg?branch=master )] ( https://travis-ci.org/tylerreisinger/lru-cache-macro )
4
4
[ ![ lru-cache-macros on docs.rs] [ docsrs-image ]] [ docsrs ]
@@ -9,14 +9,15 @@ lru-cache-macros
9
9
[ crates-image ] : https://img.shields.io/crates/v/lru-cache-macros.svg
10
10
[ crates ] : https://crates.io/crates/lru-cache-macros/
11
11
12
- An attribute procedural macro to automatically cache the result of a function given a set of inputs.
12
+ A procedural macro to automatically cache the result of a function given a set of inputs.
13
13
14
14
# Example:
15
15
16
16
``` rust
17
- use lru_cache_macros :: lru_cache;
17
+ use cache_macro :: cache;
18
+ use lru_cache :: LruCache ;
18
19
19
- #[lru_cache(20 )]
20
+ #[cache( LruCache : LruCache :: new(20) )]
20
21
fn fib (x : u32 ) -> u64 {
21
22
println! (" {:?}" , x );
22
23
if x <= 1 {
@@ -34,89 +35,83 @@ results because of the recursion hit the cache.
34
35
35
36
# Usage:
36
37
37
- Simply place ` #[lru_cache([size] )] ` above your function. The function must obey a few properties
38
+ Simply place ` #[cache(CacheType : constructor )] ` above your function. The function must obey a few properties
38
39
to use lru_cache:
39
40
40
41
* All arguments and return values must implement ` Clone ` .
41
42
* The function may not take ` self ` in any form.
42
43
43
- The macro will use the LruCache at ` ::lru_cache::LruCache ` by default. This can be changed by
44
- setting the ` cache_type ` config variable as shown in the configuration section.
45
-
46
44
The ` LruCache ` type used must accept two generic parameters ` <Args, Return> ` and must support methods
47
- ` get_mut(&K) ` and ` insert(K, V) ` . The ` lru-cache ` crate meets these requirements.
45
+ ` get_mut(&K) -> Option<&mut V> ` and ` insert(K, V) ` . The ` lru-cache ` (for LRU caching)
46
+ and ` expiring_map ` (for time-to-live caching) crates currently meet these requirements.
48
47
49
48
Currently, this crate only works on nightly rust. However, once the 2018 edition stabilizes as well as the
50
49
procedural macro diagnostic interface, it should be able to run on stable.
51
50
52
51
# Configuration:
53
52
54
- The lru_cache macro can be configured by adding additional attributes under ` #[lru_cache(size)] ` .
55
-
56
- All configuration attributes take the form ` #[lru_config(...)] ` . The available attributes are:
57
-
58
- * ` #[lru_config(cache_type = ...)] `
59
-
60
- This allows the cache type used internally to be changed. The default is equivalent to
61
-
62
- ``` #[lru_config(cache_type = ::lru_cache::LruCache)] ```
63
-
64
- * ` #[lru_config(ignore_args = ...)] `
65
-
66
- This allows certain arguments to be ignored for the purposes of caching. That means they are not part of the
67
- hash table key and thus should never influence the output of the function. It can be useful for diagnostic settings,
68
- returning the number of times executed, or other introspection purposes.
69
-
70
- ` ignore_args ` takes a comma-separated list of variable identifiers to ignore.
71
-
72
- ### Example:
73
- ``` rust
74
- use lru_cache_macros :: lru_cache;
75
- #[lru_cache(20)]
76
- #[lru_config(ignore_args = call_count)]
77
- fn fib (x : u64 , call_count : & mut u32 ) -> u64 {
78
- * call_count += 1 ;
79
- if x <= 1 {
80
- 1
81
- } else {
82
- fib (x - 1 , call_count ) + fib (x - 2 , call_count )
83
- }
53
+ The lru_cache macro can be configured by adding additional attributes under ` #[cache(...)] ` .
54
+
55
+ All configuration attributes take the form ` #[cache_cfg(...)] ` . The available attributes are:
56
+
57
+ * ` #[cache_cfg(ignore_args = ...)] `
58
+
59
+ This allows certain arguments to be ignored for the purposes of caching. That means they are not part of the
60
+ hash table key and thus should never influence the output of the function. It can be useful for diagnostic settings,
61
+ returning the number of times executed, or other introspection purposes.
62
+
63
+ ` ignore_args ` takes a comma-separated list of variable identifiers to ignore.
64
+
65
+ ### Example:
66
+ ``` rust
67
+ use cache_macro :: cache;
68
+ use lru_cache :: LruCache ;
69
+ #[cache(LruCache : LruCache :: new(20))]
70
+ #[cache_cfg(ignore_args = call_count)]
71
+ fn fib (x : u64 , call_count : & mut u32 ) -> u64 {
72
+ * call_count += 1 ;
73
+ if x <= 1 {
74
+ 1
75
+ } else {
76
+ fib (x - 1 , call_count ) + fib (x - 2 , call_count )
84
77
}
78
+ }
79
+
80
+ let mut call_count = 0 ;
81
+ assert_eq! (fib (39 , & mut call_count ), 102_334_155 );
82
+ assert_eq! (call_count , 40 );
83
+ ```
84
+
85
+ The ` call_count ` argument can vary, caching is only done based on ` x ` .
86
+
87
+ * ` #[cache_cfg(thread_local)] `
85
88
86
- let mut call_count = 0 ;
87
- assert_eq! (fib (39 , & mut call_count ), 102_334_155 );
88
- assert_eq! (call_count , 40 );
89
- ```
90
-
91
- The `call_count ` argument can vary , caching is only done based on `x `.
92
-
93
- * `#[lru_config(thread_local)]`
94
-
95
- Store the cache in thread - local storage instead of global static storage . This avoids the overhead of Mutex locking ,
96
- but each thread will be given its own cache , and all caching will not affect any other thread .
97
-
98
- Expanding on the first example :
99
-
100
- ```rust
101
- use lru_cache_macros :: lru_cache;
102
-
103
- #[lru_cache(20)]
104
- #[lru_config(thread_local)]
105
- fn fib (x : u32 ) -> u64 {
106
- println! (" {:?}" , x );
107
- if x <= 1 {
108
- 1
109
- } else {
110
- fib (x - 1 ) + fib (x - 2 )
111
- }
89
+ Store the cache in thread-local storage instead of global static storage. This avoids the overhead of Mutex locking,
90
+ but each thread will be given its own cache, and all caching will not affect any other thread.
91
+
92
+ Expanding on the first example:
93
+
94
+ ``` rust
95
+ use cache_macro :: cache;
96
+ use lru_cache :: LruCache ;
97
+
98
+ #[cache(LruCache : LruCache :: new(20))]
99
+ #[cache_cfg(thread_local)]
100
+ fn fib (x : u32 ) -> u64 {
101
+ println! (" {:?}" , x );
102
+ if x <= 1 {
103
+ 1
104
+ } else {
105
+ fib (x - 1 ) + fib (x - 2 )
112
106
}
107
+ }
113
108
114
- assert_eq! (fib (19 ), 6765 );
115
- ```
109
+ assert_eq! (fib (19 ), 6765 );
110
+ ```
116
111
117
112
# Details
118
- The created cache is stored as a static variable protected by a mutex unless the `#[lru_config (thread_local)]` configuration
119
- is added .
113
+ The created cache is stored as a static variable protected by a mutex unless the ` #[cache_cfg (thread_local)] `
114
+ configuration is added.
120
115
121
116
With the default settings, the fibonacci example will generate the following code:
122
117
@@ -132,7 +127,7 @@ fn fib(x: u32) -> u64 {
132
127
static ref cache : Mutex <:: lru_cache :: LruCache <(u32 ,), u64 >> =
133
128
Mutex :: new (:: lru_cache :: LruCache :: new (20usize ));
134
129
}
135
-
130
+
136
131
let cloned_args = (x . clone (),);
137
132
let mut cache_unlocked = cache . lock (). unwrap ();
138
133
let stored_result = cache_unlocked . get_mut (& cloned_args );
0 commit comments