Skip to content

Commit

Permalink
Merge pull request #19 from nhawkes/master
Browse files Browse the repository at this point in the history
 Impl MemoryUsage for std::collections::BTreeMap
  • Loading branch information
syrusakbary authored Jun 5, 2024
2 parents 7699083 + e384add commit 3decb36
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions crates/loupe/src/memory_usage/collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
use crate::{assert_size_of_val_eq, POINTER_BYTE_SIZE};
use crate::{MemoryUsage, MemoryUsageTracker};
use std::collections::HashMap;
use std::collections::{HashMap, BTreeMap};
use std::mem;

impl<T> MemoryUsage for Vec<T>
Expand Down Expand Up @@ -76,6 +76,22 @@ where
}
}


impl<K, V> MemoryUsage for BTreeMap<K, V>
where
K: MemoryUsage,
V: MemoryUsage,
{
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
+ self
.iter()
.map(|(key, value)| key.size_of_val(tracker) + value.size_of_val(tracker))
.sum::<usize>()
}
}


#[cfg(test)]
mod test_collection_types {
use super::*;
Expand Down Expand Up @@ -123,4 +139,48 @@ mod test_collection_types {
empty_hashmap_size + 1 * 3 + (POINTER_BYTE_SIZE + 4) * 2 + POINTER_BYTE_SIZE + 0 /* no i32 */
);
}
}

#[test]
fn test_btreemap() {
let mut btreemap: BTreeMap<i8, i32> = BTreeMap::new();
let empty_btreemap_size = mem::size_of_val(&btreemap);
assert_size_of_val_eq!(btreemap, empty_btreemap_size + 1 * 0 + 4 * 0);

btreemap.insert(1, 1);
assert_size_of_val_eq!(btreemap, empty_btreemap_size + 1 * 1 + 4 * 1);

btreemap.insert(2, 2);
assert_size_of_val_eq!(btreemap, empty_btreemap_size + 1 * 2 + 4 * 2);
}

#[test]
fn test_btreemap_not_unique() {
let mut btreemap: BTreeMap<i8, &i32> = BTreeMap::new();
let empty_btreemap_size = mem::size_of_val(&btreemap);
assert_size_of_val_eq!(
btreemap,
empty_btreemap_size + 1 * 0 + (POINTER_BYTE_SIZE + 4) * 0
);

let one: i32 = 1;
btreemap.insert(1, &one);
assert_size_of_val_eq!(
btreemap,
empty_btreemap_size + 1 * 1 + (POINTER_BYTE_SIZE + 4) * 1
);

let two: i32 = 2;
btreemap.insert(2, &two);
assert_size_of_val_eq!(
btreemap,
empty_btreemap_size + 1 * 2 + (POINTER_BYTE_SIZE + 4) * 2
);

// Push a reference to an item that already exists!
btreemap.insert(3, &one);
assert_size_of_val_eq!(
btreemap,
empty_btreemap_size + 1 * 3 + (POINTER_BYTE_SIZE + 4) * 2 + POINTER_BYTE_SIZE + 0 /* no i32 */
);
}
}

0 comments on commit 3decb36

Please sign in to comment.