-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More tests for MallocSizeOf impl for SmallVec
- Loading branch information
Showing
1 changed file
with
38 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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); | ||
|
@@ -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.
Sorry, something went wrong. |
||
} | ||
|
||
#[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.
Sorry, something went wrong.
cheme
Collaborator
|
||
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); | ||
} | ||
} |
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).