11use std:: collections:: HashMap ;
22use wasmer:: Module ;
33
4+ use super :: sized_module:: SizedModule ;
45use crate :: { Checksum , VmResult } ;
56
67/// An pinned in memory module cache
78pub struct PinnedMemoryCache {
8- modules : HashMap < Checksum , Module > ,
9+ modules : HashMap < Checksum , SizedModule > ,
910}
1011
1112impl PinnedMemoryCache {
@@ -16,8 +17,8 @@ impl PinnedMemoryCache {
1617 }
1718 }
1819
19- pub fn store ( & mut self , checksum : & Checksum , module : Module ) -> VmResult < ( ) > {
20- self . modules . insert ( * checksum, module) ;
20+ pub fn store ( & mut self , checksum : & Checksum , module : Module , size : usize ) -> VmResult < ( ) > {
21+ self . modules . insert ( * checksum, SizedModule { module, size } ) ;
2122 Ok ( ( ) )
2223 }
2324
@@ -31,7 +32,7 @@ impl PinnedMemoryCache {
3132 /// Looks up a module in the cache and creates a new module
3233 pub fn load ( & mut self , checksum : & Checksum ) -> VmResult < Option < Module > > {
3334 match self . modules . get ( checksum) {
34- Some ( module) => Ok ( Some ( module. clone ( ) ) ) ,
35+ Some ( module) => Ok ( Some ( module. module . clone ( ) ) ) ,
3536 None => Ok ( None ) ,
3637 }
3738 }
@@ -45,6 +46,14 @@ impl PinnedMemoryCache {
4546 pub fn len ( & self ) -> usize {
4647 self . modules . len ( )
4748 }
49+
50+ /// Returns cumulative size of all elements in the cache.
51+ ///
52+ /// This is based on the values provided with `store`. No actual
53+ /// memory size is measured here.
54+ pub fn size ( & self ) -> usize {
55+ self . modules . iter ( ) . map ( |( _, module) | module. size ) . sum ( )
56+ }
4857}
4958
5059#[ cfg( test) ]
@@ -90,7 +99,7 @@ mod tests {
9099 }
91100
92101 // Store module
93- cache. store ( & checksum, original) . unwrap ( ) ;
102+ cache. store ( & checksum, original, 0 ) . unwrap ( ) ;
94103
95104 // Load module
96105 let cached = cache. load ( & checksum) . unwrap ( ) . unwrap ( ) ;
@@ -126,7 +135,7 @@ mod tests {
126135
127136 // Add
128137 let original = compile ( & wasm, None ) . unwrap ( ) ;
129- cache. store ( & checksum, original) . unwrap ( ) ;
138+ cache. store ( & checksum, original, 0 ) . unwrap ( ) ;
130139
131140 assert_eq ! ( cache. has( & checksum) , true ) ;
132141
@@ -157,7 +166,7 @@ mod tests {
157166
158167 // Add
159168 let original = compile ( & wasm, None ) . unwrap ( ) ;
160- cache. store ( & checksum, original) . unwrap ( ) ;
169+ cache. store ( & checksum, original, 0 ) . unwrap ( ) ;
161170
162171 assert_eq ! ( cache. len( ) , 1 ) ;
163172
@@ -166,4 +175,53 @@ mod tests {
166175
167176 assert_eq ! ( cache. len( ) , 0 ) ;
168177 }
178+
179+ #[ test]
180+ fn size_works ( ) {
181+ let mut cache = PinnedMemoryCache :: new ( ) ;
182+
183+ // Create module
184+ let wasm1 = wat:: parse_str (
185+ r#"(module
186+ (type $t0 (func (param i32) (result i32)))
187+ (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)
188+ get_local $p0
189+ i32.const 1
190+ i32.add)
191+ )"# ,
192+ )
193+ . unwrap ( ) ;
194+ let checksum1 = Checksum :: generate ( & wasm1) ;
195+ let wasm2 = wat:: parse_str (
196+ r#"(module
197+ (type $t0 (func (param i32) (result i32)))
198+ (func $add_one (export "add_two") (type $t0) (param $p0 i32) (result i32)
199+ get_local $p0
200+ i32.const 2
201+ i32.add)
202+ )"# ,
203+ )
204+ . unwrap ( ) ;
205+ let checksum2 = Checksum :: generate ( & wasm2) ;
206+
207+ assert_eq ! ( cache. size( ) , 0 ) ;
208+
209+ // Add 1
210+ let original = compile ( & wasm1, None ) . unwrap ( ) ;
211+ cache. store ( & checksum1, original, 500 ) . unwrap ( ) ;
212+ assert_eq ! ( cache. size( ) , 500 ) ;
213+
214+ // Add 2
215+ let original = compile ( & wasm2, None ) . unwrap ( ) ;
216+ cache. store ( & checksum2, original, 300 ) . unwrap ( ) ;
217+ assert_eq ! ( cache. size( ) , 800 ) ;
218+
219+ // Remove 1
220+ cache. remove ( & checksum1) . unwrap ( ) ;
221+ assert_eq ! ( cache. size( ) , 300 ) ;
222+
223+ // Remove 2
224+ cache. remove ( & checksum2) . unwrap ( ) ;
225+ assert_eq ! ( cache. size( ) , 0 ) ;
226+ }
169227}
0 commit comments