From 33f1bc6ace685bc9637f5a3855bcb66e81b75bba Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Tue, 14 Mar 2017 11:17:19 -0700 Subject: [PATCH] Add HeapSizeOf trait and from_slice method --- Cargo.toml | 3 +++ lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0a7b176..84b2fce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,6 @@ documentation = "http://doc.servo.org/smallvec/" [lib] name = "smallvec" path = "lib.rs" + +[dependencies] +heapsize = "0.3" diff --git a/lib.rs b/lib.rs index 762b838..ebb7e22 100644 --- a/lib.rs +++ b/lib.rs @@ -6,16 +6,20 @@ //! to the heap for larger allocations. This can be a useful optimization for improving cache //! locality and reducing allocator traffic for workloads that fit within the inline buffer. +extern crate heapsize; + use std::borrow::{Borrow, BorrowMut}; use std::cmp; use std::fmt; use std::hash::{Hash, Hasher}; use std::iter::{IntoIterator, FromIterator}; use std::mem; +use std::os::raw::c_void; use std::ops; use std::ptr; use std::slice; +use heapsize::{HeapSizeOf, heap_size_of}; use SmallVecData::{Inline, Heap}; /// Common operations implemented by both `Vec` and `SmallVec`. @@ -478,6 +482,12 @@ impl SmallVec { } impl SmallVec where A::Item: Copy { + pub fn from_slice(slice: &[A::Item]) -> Self { + let mut vec = Self::new(); + vec.extend_from_slice(slice); + vec + } + pub fn insert_from_slice(&mut self, index: usize, slice: &[A::Item]) { self.reserve(slice.len()); @@ -500,6 +510,19 @@ impl SmallVec where A::Item: Copy { } } +impl HeapSizeOf for SmallVec where A::Item: HeapSizeOf { + fn heap_size_of_children(&self) -> usize { + match self.data { + Inline { .. } => 0, + Heap { ptr, .. } => { + self.iter().fold( + unsafe { heap_size_of(ptr as *const c_void) }, + |n, elem| n + elem.heap_size_of_children()) + }, + } + } +} + impl ops::Deref for SmallVec { type Target = [A::Item]; #[inline] @@ -823,15 +846,17 @@ macro_rules! impl_array( } ); -impl_array!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, +impl_array!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000, 0x80000, 0x100000); #[cfg(test)] pub mod tests { use SmallVec; + use heapsize::HeapSizeOf; use std::borrow::ToOwned; use std::iter::FromIterator; + use std::mem::size_of; // We heap allocate all these strings so that double frees will show up under valgrind. @@ -1248,6 +1273,35 @@ pub mod tests { assert_eq!(&SmallVec::<[u32; 2]>::from(&[1, 2, 3][..])[..], [1, 2, 3]); } + #[test] + fn test_from_slice() { + assert_eq!(&SmallVec::<[u32; 2]>::from_slice(&[1][..])[..], [1]); + assert_eq!(&SmallVec::<[u32; 2]>::from_slice(&[1, 2, 3][..])[..], [1, 2, 3]); + } + + #[test] + fn test_heap_size_of_children() { + let mut vec = SmallVec::<[u32; 2]>::new(); + assert_eq!(vec.heap_size_of_children(), 0); + vec.push(1); + vec.push(2); + assert_eq!(vec.heap_size_of_children(), 0); + vec.push(3); + assert_eq!(vec.heap_size_of_children(), 16); + + // Now check with reserved space + let mut vec = SmallVec::<[u32; 2]>::new(); + vec.reserve(10); // Rounds up to 16 + assert_eq!(vec.heap_size_of_children(), 64); + + // Check with nested heap structures + let mut vec = SmallVec::<[Vec; 2]>::new(); + vec.reserve(10); + vec.push(vec![2, 3, 4]); + assert_eq!(vec.heap_size_of_children(), + vec![2, 3, 4].heap_size_of_children() + 16 * size_of::>()); + } + #[test] fn test_exact_size_iterator() { let mut vec = SmallVec::<[u32; 2]>::from(&[1, 2, 3][..]);