Skip to content

Commit

Permalink
More tests for MallocSizeOf impl for SmallVec
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdplm committed Dec 15, 2019
1 parent 28f46f2 commit 6e2745d
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions parity-util-mem/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ macro_rules! impl_smallvec {
where
T: MallocSizeOf,
{
fn size_of(&self, _: &mut MallocSizeOfOps) -> usize {
if self.spilled() {
self.capacity() * core::mem::size_of::<T>()
} else {
0
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = 0;
for elem in self.iter() {
n += elem.size_of(ops);
}
n
}
}
};
}

// todo[dvdplm]: check if we really need all these impls.
impl_smallvec!(32); // kvdb uses this
impl_smallvec!(36); // trie-db uses this

Expand All @@ -74,7 +73,7 @@ mod tests {
impl_smallvec!(3);

#[test]
fn test_smallvec() {
fn test_smallvec_stack_allocated_type() {
let mut v: SmallVec<[u8; 3]> = SmallVec::new();
let mut ops = new_malloc_size_ops();
assert_eq!(v.size_of(&mut ops), 0);
Expand All @@ -85,7 +84,38 @@ mod tests {
assert!(!v.spilled());
v.push(4);
assert!(v.spilled(), "SmallVec spills when going beyond the capacity of the inner backing array");
assert_eq!(v.len(), 4);
assert_eq!(v.size_of(&mut ops), 0); // <–– WHY NOT 4?

This comment has been minimized.

Copy link
@cheme

cheme Dec 15, 2019

Collaborator

Yes you can keep your code to get 4 on spilled and combine with the result from the iterator (here the iterator go over u8 which do not heap allocate).

}

#[test]
fn test_smallvec_boxed_stack_allocated_type() {
let mut v: SmallVec<[Box<u8>; 3]> = SmallVec::new();
let mut ops = new_malloc_size_ops();
assert_eq!(v.size_of(&mut ops), 0);
v.push(Box::new(1u8));
v.push(Box::new(2u8));
v.push(Box::new(3u8));
assert_eq!(v.size_of(&mut ops), 3); // <– shouldn't this be 3 * pointer_size = 24?

This comment has been minimized.

Copy link
@cheme

cheme Dec 15, 2019

Collaborator

yes I guess we should add your old code to this value, resulting in 3 + len * size of pointer

assert!(!v.spilled());
v.push(Box::new(4u8));
assert!(v.spilled(), "SmallVec spills when going beyond the capacity of the inner backing array");
let mut ops = new_malloc_size_ops();
assert_eq!(v.size_of(&mut ops), 4);
}

#[test]
fn test_smallvec_heap_allocated_type() {
let mut v: SmallVec<[String; 3]> = SmallVec::new();
let mut ops = new_malloc_size_ops();
assert_eq!(v.size_of(&mut ops), 0);
v.push("COW".into());
v.push("PIG".into());
v.push("DUCK".into());
assert!(!v.spilled());
assert_eq!(v.size_of(&mut ops), 10);
v.push("ÖWL".into());
assert!(v.spilled());
let mut ops = new_malloc_size_ops();
assert_eq!(v.size_of(&mut ops), 14);
}
}

0 comments on commit 6e2745d

Please sign in to comment.