Skip to content

Commit 1700810

Browse files
authored
Merge pull request #30 from mozilla/29-approximate-size
fixes #29: implement an approximate size function
2 parents dd8be67 + 7e8fa73 commit 1700810

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rust_cascade"
33
description = "A simple bloom filter cascade implementation in Rust."
44
license = "MPL-2.0"
5-
version = "1.1.0"
5+
version = "1.2.0"
66
authors = ["Mark Goodwin <mgoodwin@mozilla.com>", "Dana Keeler <dkeeler@mozilla.com>", "J.C. Jones <jc@mozilla.com>"]
77
documentation = "https://docs.rs/rust_cascade/"
88
homepage = "https://github.com/mozilla/rust-cascade"
@@ -11,7 +11,5 @@ repository = "https://github.com/mozilla/rust-cascade"
1111
[dependencies]
1212
byteorder="1.3.1"
1313
digest="0.8.0"
14-
malloc_size_of_derive = "0.1"
1514
murmurhash3 = "0.0.5"
1615
sha2="^0.8"
17-
wr_malloc_size_of = "0.1"

src/lib.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
extern crate byteorder;
22
extern crate digest;
3-
extern crate malloc_size_of_derive;
43
extern crate murmurhash3;
54
extern crate sha2;
6-
extern crate wr_malloc_size_of;
7-
8-
use wr_malloc_size_of as malloc_size_of;
95

106
use byteorder::ReadBytesExt;
11-
use malloc_size_of_derive::MallocSizeOf;
127
use murmurhash3::murmurhash3_x86_32;
138
use sha2::{Digest, Sha256};
149
use std::convert::{TryFrom, TryInto};
1510
use std::fmt;
1611
use 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)]
2015
struct 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)]
7872
struct 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)]
9689
enum 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)]
226218
pub 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

322330
impl 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

Comments
 (0)