@@ -4,9 +4,19 @@ use std::collections::HashMap;
4
4
use super :: cached_module:: CachedModule ;
5
5
use crate :: VmResult ;
6
6
7
+ /// Struct storing some additional metadata, which is only of interest for the pinned cache,
8
+ /// alongside the cached module.
9
+ // TODO: Maybe implement a `Deref` for this? But would it even worth it considering how little it is actually used?
10
+ pub struct InstrumentedModule {
11
+ /// Number of loads from memory this module received
12
+ pub hits : u32 ,
13
+ /// The actual cached module
14
+ pub module : CachedModule ,
15
+ }
16
+
7
17
/// An pinned in memory module cache
8
18
pub struct PinnedMemoryCache {
9
- modules : HashMap < Checksum , CachedModule > ,
19
+ modules : HashMap < Checksum , InstrumentedModule > ,
10
20
}
11
21
12
22
impl PinnedMemoryCache {
@@ -17,8 +27,19 @@ impl PinnedMemoryCache {
17
27
}
18
28
}
19
29
30
+ pub fn iter ( & self ) -> impl Iterator < Item = ( & Checksum , & InstrumentedModule ) > {
31
+ self . modules . iter ( )
32
+ }
33
+
20
34
pub fn store ( & mut self , checksum : & Checksum , cached_module : CachedModule ) -> VmResult < ( ) > {
21
- self . modules . insert ( * checksum, cached_module) ;
35
+ self . modules . insert (
36
+ * checksum,
37
+ InstrumentedModule {
38
+ hits : 0 ,
39
+ module : cached_module,
40
+ } ,
41
+ ) ;
42
+
22
43
Ok ( ( ) )
23
44
}
24
45
@@ -31,8 +52,11 @@ impl PinnedMemoryCache {
31
52
32
53
/// Looks up a module in the cache and creates a new module
33
54
pub fn load ( & mut self , checksum : & Checksum ) -> VmResult < Option < CachedModule > > {
34
- match self . modules . get ( checksum) {
35
- Some ( cached) => Ok ( Some ( cached. clone ( ) ) ) ,
55
+ match self . modules . get_mut ( checksum) {
56
+ Some ( cached) => {
57
+ cached. hits = cached. hits . saturating_add ( 1 ) ;
58
+ Ok ( Some ( cached. module . clone ( ) ) )
59
+ }
36
60
None => Ok ( None ) ,
37
61
}
38
62
}
@@ -54,7 +78,7 @@ impl PinnedMemoryCache {
54
78
pub fn size ( & self ) -> usize {
55
79
self . modules
56
80
. iter ( )
57
- . map ( |( key, module) | std:: mem:: size_of_val ( key) + module. size_estimate )
81
+ . map ( |( key, module) | std:: mem:: size_of_val ( key) + module. module . size_estimate )
58
82
. sum ( )
59
83
}
60
84
}
0 commit comments