1
1
use std:: {
2
- borrow:: { BorrowMut } ,
2
+ borrow:: BorrowMut ,
3
3
sync:: { Arc , RwLock } ,
4
4
} ;
5
5
@@ -9,18 +9,6 @@ use crate::error::Error;
9
9
use clru:: CLruCache ;
10
10
use wasmer:: { Instance , Module , Store } ;
11
11
12
- #[ derive( Debug , Default , Clone , Copy , PartialEq ) ]
13
- pub struct Stats {
14
- pub hits : u32 ,
15
- pub misses : u32 ,
16
- }
17
-
18
- impl Stats {
19
- pub fn new ( ) -> Self {
20
- Self { hits : 0 , misses : 0 }
21
- }
22
- }
23
-
24
12
/// An in-memory module cache
25
13
pub struct InMemoryCache {
26
14
modules : CLruCache < Checksum , Module > ,
@@ -48,55 +36,45 @@ pub struct CacheOptions {
48
36
49
37
pub struct Cache {
50
38
memory_cache : Arc < RwLock < InMemoryCache > > ,
51
- stats : Stats ,
52
39
}
53
40
54
41
impl Cache {
55
42
pub fn new ( options : CacheOptions ) -> Self {
56
43
let CacheOptions { cache_size } = options;
57
44
58
- Self {
59
- memory_cache : Arc :: new ( RwLock :: new ( InMemoryCache :: new ( cache_size) ) ) ,
60
- stats : Stats :: new ( ) ,
61
- }
62
- }
63
-
64
- pub fn stats ( & self ) -> & Stats {
65
- & self . stats
45
+ Self { memory_cache : Arc :: new ( RwLock :: new ( InMemoryCache :: new ( cache_size) ) ) }
66
46
}
67
47
68
48
fn with_in_memory_cache < C , R > ( & mut self , callback : C ) -> R
69
49
where
70
- C : FnOnce ( & mut InMemoryCache , & mut Stats ) -> R ,
50
+ C : FnOnce ( & mut InMemoryCache ) -> R ,
71
51
{
72
52
let mut guard = self . memory_cache . as_ref ( ) . write ( ) . unwrap ( ) ;
73
53
let in_memory_cache = guard. borrow_mut ( ) ;
74
- callback ( in_memory_cache, & mut self . stats )
54
+ callback ( in_memory_cache)
75
55
}
76
56
77
57
pub fn get_instance (
78
58
& mut self ,
79
59
wasm : & [ u8 ] ,
80
60
store : & Store ,
81
61
import_object : & wasmer:: ImportObject ,
82
- ) -> Result < wasmer:: Instance , Error > {
62
+ ) -> Result < ( wasmer:: Instance , bool ) , Error > {
83
63
let checksum = Checksum :: generate ( wasm) ;
84
- self . with_in_memory_cache ( |in_memory_cache, stats | {
64
+ self . with_in_memory_cache ( |in_memory_cache| {
85
65
// lookup cache
86
66
if let Some ( module) = in_memory_cache. load ( & checksum) {
87
- stats. hits += 1 ;
88
- return Ok ( Instance :: new ( & module, & import_object) . unwrap ( ) ) ;
67
+ return Ok ( ( Instance :: new ( & module, & import_object) . unwrap ( ) , true ) ) ;
89
68
}
90
- stats. misses += 1 ;
91
-
69
+
92
70
// recompile
93
71
let module = Module :: new ( store, & wasm) . map_err ( |_| Error :: InstantiationError ) ?;
94
72
let instance =
95
- Instance :: new ( & module, & import_object) . map_err ( |_| Error :: InstantiationError ) ?;
96
-
73
+ Instance :: new ( & module, & import_object) . map_err ( |_| Error :: InstantiationError ) ?;
74
+
97
75
in_memory_cache. store ( & checksum, module) ;
98
-
99
- Ok ( instance)
76
+
77
+ Ok ( ( instance, false ) )
100
78
} )
101
79
}
102
80
}
@@ -126,13 +104,13 @@ mod test {
126
104
wasm
127
105
}
128
106
129
- fn get_instance_without_err ( cache : & mut Cache , wasm : & [ u8 ] ) -> wasmer:: Instance {
107
+ fn get_instance_without_err ( cache : & mut Cache , wasm : & [ u8 ] ) -> ( wasmer:: Instance , bool ) {
130
108
let compiler = Singlepass :: new ( ) ;
131
109
let store = Store :: new ( & Universal :: new ( compiler) . engine ( ) ) ;
132
110
let import_object = imports ! { } ;
133
111
134
112
match cache. get_instance ( & wasm, & store, & import_object) {
135
- Ok ( instance) => instance,
113
+ Ok ( ( instance, is_hit ) ) => ( instance, is_hit ) ,
136
114
Err ( _) => panic ! ( "Fail to get instance" ) ,
137
115
}
138
116
}
@@ -155,14 +133,14 @@ mod test {
155
133
)"# ,
156
134
) ;
157
135
158
- let instance1 = get_instance_without_err ( & mut cache, & wasm) ;
159
- assert_eq ! ( & Stats { hits : 0 , misses : 1 } , cache . stats ( ) ) ;
136
+ let ( instance1, is_hit ) = get_instance_without_err ( & mut cache, & wasm) ;
137
+ assert_eq ! ( false , is_hit ) ;
160
138
161
- let instance2 = get_instance_without_err ( & mut cache, & wasm) ;
162
- assert_eq ! ( & Stats { hits : 1 , misses : 1 } , cache . stats ( ) ) ;
139
+ let ( instance2, is_hit ) = get_instance_without_err ( & mut cache, & wasm) ;
140
+ assert_eq ! ( true , is_hit ) ;
163
141
164
- get_instance_without_err ( & mut cache, & wasm2) ;
165
- assert_eq ! ( & Stats { hits : 1 , misses : 2 } , cache . stats ( ) ) ;
142
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm2) ;
143
+ assert_eq ! ( false , is_hit ) ;
166
144
167
145
let ser1 = match instance1. module ( ) . serialize ( ) {
168
146
Ok ( r) => r,
@@ -205,31 +183,31 @@ mod test {
205
183
) ;
206
184
207
185
// miss [_ _] => [1 _]
208
- get_instance_without_err ( & mut cache, & wasm1) ;
209
- assert_eq ! ( & Stats { hits : 0 , misses : 1 } , cache . stats ( ) ) ;
186
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm1) ;
187
+ assert_eq ! ( false , is_hit ) ;
210
188
211
189
// miss [1 _] => [2 1]
212
- get_instance_without_err ( & mut cache, & wasm2) ;
213
- assert_eq ! ( & Stats { hits : 0 , misses : 2 } , cache . stats ( ) ) ;
190
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm2) ;
191
+ assert_eq ! ( false , is_hit ) ;
214
192
215
193
// miss [2 1] => [3 2]
216
- get_instance_without_err ( & mut cache, & wasm3) ;
217
- assert_eq ! ( & Stats { hits : 0 , misses : 3 } , cache . stats ( ) ) ;
194
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm3) ;
195
+ assert_eq ! ( false , is_hit ) ;
218
196
219
197
// hit [3 2] => [2 3]
220
- get_instance_without_err ( & mut cache, & wasm2) ;
221
- assert_eq ! ( & Stats { hits : 1 , misses : 3 } , cache . stats ( ) ) ;
198
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm2) ;
199
+ assert_eq ! ( true , is_hit ) ;
222
200
223
201
// miss [2 3] => [1 2]
224
- get_instance_without_err ( & mut cache, & wasm1) ;
225
- assert_eq ! ( & Stats { hits : 1 , misses : 4 } , cache . stats ( ) ) ;
202
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm1) ;
203
+ assert_eq ! ( false , is_hit ) ;
226
204
227
205
// hit [1 2] => [2 1]
228
- get_instance_without_err ( & mut cache, & wasm2) ;
229
- assert_eq ! ( & Stats { hits : 2 , misses : 4 } , cache . stats ( ) ) ;
206
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm2) ;
207
+ assert_eq ! ( true , is_hit ) ;
230
208
231
209
// miss [2 1] => [3 2]
232
- get_instance_without_err ( & mut cache, & wasm3) ;
233
- assert_eq ! ( & Stats { hits : 2 , misses : 5 } , cache . stats ( ) ) ;
210
+ let ( _ , is_hit ) = get_instance_without_err ( & mut cache, & wasm3) ;
211
+ assert_eq ! ( false , is_hit ) ;
234
212
}
235
213
}
0 commit comments