Skip to content

Commit 9a82c64

Browse files
committed
remove cache stats
1 parent 10187cf commit 9a82c64

File tree

4 files changed

+38
-60
lines changed

4 files changed

+38
-60
lines changed

packages/vm/src/cache.rs

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
borrow::{BorrowMut},
2+
borrow::BorrowMut,
33
sync::{Arc, RwLock},
44
};
55

@@ -9,18 +9,6 @@ use crate::error::Error;
99
use clru::CLruCache;
1010
use wasmer::{Instance, Module, Store};
1111

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-
2412
/// An in-memory module cache
2513
pub struct InMemoryCache {
2614
modules: CLruCache<Checksum, Module>,
@@ -48,55 +36,45 @@ pub struct CacheOptions {
4836

4937
pub struct Cache {
5038
memory_cache: Arc<RwLock<InMemoryCache>>,
51-
stats: Stats,
5239
}
5340

5441
impl Cache {
5542
pub fn new(options: CacheOptions) -> Self {
5643
let CacheOptions { cache_size } = options;
5744

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))) }
6646
}
6747

6848
fn with_in_memory_cache<C, R>(&mut self, callback: C) -> R
6949
where
70-
C: FnOnce(&mut InMemoryCache, &mut Stats) -> R,
50+
C: FnOnce(&mut InMemoryCache) -> R,
7151
{
7252
let mut guard = self.memory_cache.as_ref().write().unwrap();
7353
let in_memory_cache = guard.borrow_mut();
74-
callback(in_memory_cache, &mut self.stats)
54+
callback(in_memory_cache)
7555
}
7656

7757
pub fn get_instance(
7858
&mut self,
7959
wasm: &[u8],
8060
store: &Store,
8161
import_object: &wasmer::ImportObject,
82-
) -> Result<wasmer::Instance, Error> {
62+
) -> Result<(wasmer::Instance, bool), Error> {
8363
let checksum = Checksum::generate(wasm);
84-
self.with_in_memory_cache(|in_memory_cache, stats| {
64+
self.with_in_memory_cache(|in_memory_cache| {
8565
// lookup cache
8666
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));
8968
}
90-
stats.misses += 1;
91-
69+
9270
// recompile
9371
let module = Module::new(store, &wasm).map_err(|_| Error::InstantiationError)?;
9472
let instance =
95-
Instance::new(&module, &import_object).map_err(|_| Error::InstantiationError)?;
96-
73+
Instance::new(&module, &import_object).map_err(|_| Error::InstantiationError)?;
74+
9775
in_memory_cache.store(&checksum, module);
98-
99-
Ok(instance)
76+
77+
Ok((instance, false))
10078
})
10179
}
10280
}
@@ -126,13 +104,13 @@ mod test {
126104
wasm
127105
}
128106

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) {
130108
let compiler = Singlepass::new();
131109
let store = Store::new(&Universal::new(compiler).engine());
132110
let import_object = imports! {};
133111

134112
match cache.get_instance(&wasm, &store, &import_object) {
135-
Ok(instance) => instance,
113+
Ok((instance, is_hit)) => (instance, is_hit),
136114
Err(_) => panic!("Fail to get instance"),
137115
}
138116
}
@@ -155,14 +133,14 @@ mod test {
155133
)"#,
156134
);
157135

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);
160138

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);
163141

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);
166144

167145
let ser1 = match instance1.module().serialize() {
168146
Ok(r) => r,
@@ -205,31 +183,31 @@ mod test {
205183
);
206184

207185
// 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);
210188

211189
// 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);
214192

215193
// 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);
218196

219197
// 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);
222200

223201
// 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);
226204

227205
// 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);
230208

231209
// 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);
234212
}
235213
}

packages/vm/src/calls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121
let store = make_store();
2222
let import_object = create_import_object(&store, owasm_env.clone());
2323

24-
let instance = cache.get_instance(code, &store, &import_object)?;
24+
let (instance, _) = cache.get_instance(code, &store, &import_object)?;
2525
let instance_ptr = NonNull::from(&instance);
2626
owasm_env.set_wasmer_instance(Some(instance_ptr));
2727
owasm_env.set_gas_left(gas_limit);

packages/vm/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ mod test {
322322
let store = make_store();
323323
let import_object = create_import_object(&store, owasm_env.clone());
324324
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
325-
let instance = cache.get_instance(&code, &store, &import_object).unwrap();
325+
let (instance, _) = cache.get_instance(&code, &store, &import_object).unwrap();
326326

327327
return (owasm_env, instance);
328328
}

packages/vm/src/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ mod test {
253253
let store = Store::new(&Universal::new(compiler).engine());
254254
let import_object = imports! {};
255255
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
256-
let instance = cache.get_instance(&wasm, &store, &import_object).unwrap();
256+
let (instance, _) = cache.get_instance(&wasm, &store, &import_object).unwrap();
257257
env.set_wasmer_instance(Some(NonNull::from(&instance)));
258258
assert_eq!(Ok(()), env.with_wasmer_instance(|_| { Ok(()) }));
259259
}
@@ -270,7 +270,7 @@ mod test {
270270
let store = make_store();
271271
let import_object = imports! {};
272272
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
273-
let instance = cache.get_instance(&wasm, &store, &import_object).unwrap();
273+
let (instance, _) = cache.get_instance(&wasm, &store, &import_object).unwrap();
274274
env.set_wasmer_instance(Some(NonNull::from(&instance)));
275275

276276
assert_eq!(0, env.get_gas_left());

0 commit comments

Comments
 (0)