11extern crate byteorder;
22extern crate digest;
3- extern crate malloc_size_of_derive;
43extern crate murmurhash3;
54extern crate sha2;
6- extern crate wr_malloc_size_of;
7-
8- use wr_malloc_size_of as malloc_size_of;
95
106use byteorder:: ReadBytesExt ;
11- use malloc_size_of_derive:: MallocSizeOf ;
127use murmurhash3:: murmurhash3_x86_32;
138use sha2:: { Digest , Sha256 } ;
149use std:: convert:: { TryFrom , TryInto } ;
1510use std:: fmt;
1611use std:: io:: { Error , ErrorKind , Read } ;
12+ use std:: mem:: size_of;
1713
1814/// Helper struct to provide read-only bit access to a vector of bytes.
19- #[ derive( MallocSizeOf ) ]
2015struct BitVector {
2116 /// The bytes we're interested in.
2217 bytes : Vec < u8 > ,
@@ -74,7 +69,6 @@ impl BitVector {
7469}
7570
7671/// A Bloom filter representing a specific level in a multi-level cascading Bloom filter.
77- #[ derive( MallocSizeOf ) ]
7872struct Bloom {
7973 /// What level this filter is in
8074 level : u8 ,
@@ -92,7 +86,6 @@ struct Bloom {
9286#[ derive( Copy , Clone ) ]
9387/// These enumerations need to match the python filter-cascade project:
9488/// https://github.com/mozilla/filter-cascade/blob/v0.3.0/filtercascade/fileformats.py
95- #[ derive( MallocSizeOf ) ]
9689enum HashAlgorithm {
9790 MurmurHash3 = 1 ,
9891 Sha256 = 2 ,
@@ -222,7 +215,6 @@ impl fmt::Display for Bloom {
222215}
223216
224217/// A multi-level cascading Bloom filter.
225- #[ derive( MallocSizeOf ) ]
226218pub struct Cascade {
227219 /// The Bloom filter for this level in the cascade
228220 filter : Bloom ,
@@ -317,6 +309,22 @@ impl Cascade {
317309 }
318310 false
319311 }
312+
313+ /// Determine the approximate amount of memory in bytes used by this
314+ /// Cascade. Because this implementation does not integrate with the
315+ /// allocator, it can't get an accurate measurement of how much memory it
316+ /// uses. However, it can make a reasonable guess, assuming the sizes of
317+ /// the bloom filters are large enough to dominate the overall allocated
318+ /// size.
319+ pub fn approximate_size_of ( & self ) -> usize {
320+ size_of :: < Cascade > ( )
321+ + self . filter . bit_vector . bytes . len ( )
322+ + self
323+ . child_layer
324+ . as_ref ( )
325+ . map_or ( 0 , |child_layer| child_layer. approximate_size_of ( ) )
326+ + self . salt . as_ref ( ) . map_or ( 0 , |salt| salt. len ( ) )
327+ }
320328}
321329
322330impl fmt:: Display for Cascade {
@@ -406,6 +414,8 @@ mod tests {
406414 0x77 , 0x8e ] ;
407415 assert ! ( !cascade. has( & key_for_valid_cert) ) ;
408416
417+ assert_eq ! ( cascade. approximate_size_of( ) , 15632 ) ;
418+
409419 let v = include_bytes ! ( "../test_data/test_v1_murmur_short_mlbf" ) . to_vec ( ) ;
410420 assert ! ( Cascade :: from_bytes( v) . is_err( ) ) ;
411421 }
@@ -422,6 +432,7 @@ mod tests {
422432 assert ! ( cascade. has( b"this" ) == true ) ;
423433 assert ! ( cascade. has( b"that" ) == true ) ;
424434 assert ! ( cascade. has( b"other" ) == false ) ;
435+ assert_eq ! ( cascade. approximate_size_of( ) , 10247 ) ;
425436 }
426437
427438 #[ test]
@@ -436,6 +447,7 @@ mod tests {
436447 assert ! ( cascade. has( b"this" ) == true ) ;
437448 assert ! ( cascade. has( b"that" ) == true ) ;
438449 assert ! ( cascade. has( b"other" ) == false ) ;
450+ assert_eq ! ( cascade. approximate_size_of( ) , 10251 ) ;
439451 }
440452
441453 #[ test]
@@ -450,6 +462,7 @@ mod tests {
450462 assert ! ( cascade. has( b"this" ) == true ) ;
451463 assert ! ( cascade. has( b"that" ) == true ) ;
452464 assert ! ( cascade. has( b"other" ) == false ) ;
465+ assert_eq ! ( cascade. approximate_size_of( ) , 10247 ) ;
453466 }
454467
455468 #[ test]
@@ -464,6 +477,7 @@ mod tests {
464477 assert ! ( cascade. has( b"this" ) == true ) ;
465478 assert ! ( cascade. has( b"that" ) == true ) ;
466479 assert ! ( cascade. has( b"other" ) == false ) ;
480+ assert_eq ! ( cascade. approximate_size_of( ) , 10247 ) ;
467481 }
468482
469483 #[ test]
@@ -478,6 +492,7 @@ mod tests {
478492 assert ! ( cascade. has( b"this" ) == true ) ;
479493 assert ! ( cascade. has( b"that" ) == true ) ;
480494 assert ! ( cascade. has( b"other" ) == false ) ;
495+ assert_eq ! ( cascade. approximate_size_of( ) , 10247 ) ;
481496 }
482497
483498 #[ test]
0 commit comments