Skip to content

Commit

Permalink
Impl get_size::GetSize (behind feature flag)
Browse files Browse the repository at this point in the history
Shows memory usage of the struct

cf. issue #331
  • Loading branch information
amandasaurus committed Jan 17, 2024
1 parent 0d8b30a commit 5d09b91
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ drain_keep_rest = ["drain_filter"]

[dependencies]
serde = { version = "1", optional = true, default-features = false }
get-size = { version = "0.1.4", optional = true, default-features = false }

[dev_dependencies]
bincode = "1.0.1"
Expand Down
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
//! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch).
//!
//! Tracking issue: [rust-lang/rust#34761](https://github.com/rust-lang/rust/issues/34761)
//!
//! ### `get-size`
//!
//! When this optional dependency is enabled, `SmallVec` implements the `get_size::GetSize` trait.

#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
Expand Down Expand Up @@ -88,6 +92,8 @@ use serde::{
};
#[cfg(feature = "write")]
use std::io;
#[cfg(feature = "get-size")]
use get_size::GetSize;

/// Error type for APIs with fallible heap allocation
#[derive(Debug)]
Expand Down Expand Up @@ -2176,3 +2182,22 @@ impl<const N: usize> io::Write for SmallVec<u8, N> {
Ok(())
}
}

#[cfg(feature = "get-size")]
impl<A: Array> GetSize for SmallVec<A>
where
A::Item: GetSize,
{
fn get_heap_size(&self) -> usize {
let mut total = 0;
if self.spilled() {
total += self.capacity * A::Item::get_stack_size();
}

for v in self.iter() {
total += v.get_heap_size();
}

total
}
}
48 changes: 48 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,51 @@ fn drain_keep_rest() {

assert_eq!(a, SmallVec::<i32, 3>::from_slice(&[1i32, 3, 5, 6, 7, 8]));
}

#[cfg(feature = "get-size")]
#[test]
fn test_get_size() {
use get_size::GetSize;

assert_eq!(SmallVec::<[i32; 1]>::get_stack_size(), 24);
assert_eq!(SmallVec::<[i32; 2]>::get_stack_size(), 24);
assert_eq!(SmallVec::<[i32; 10]>::get_stack_size(), 56);

let mut a: SmallVec<[i32; 2]> = smallvec![];
assert_eq!(SmallVec::<[i32; 2]>::get_stack_size(), 24);
assert!(!a.spilled());
assert_eq!(a.len(), 0);
assert_eq!(a.get_size(), 24);
assert_eq!(a.get_heap_size(), 0);

a.push(0);
assert_eq!(a.len(), 1);
assert!(!a.spilled());
assert_eq!(a.get_size(), 24);
assert_eq!(a.get_heap_size(), 0);

a.push(1);
assert_eq!(a.len(), 2);
assert!(!a.spilled());
assert_eq!(a.get_size(), 24);
assert_eq!(a.get_heap_size(), 0);

a.push(2);
assert_eq!(a.len(), 3);
assert!(a.spilled());
assert_eq!(a.get_size(), 40);
assert_eq!(a.get_heap_size(), 16);

a.push(3);
assert_eq!(a.len(), 4);
assert!(a.spilled());
assert_eq!(a.get_size(), 40);
assert_eq!(a.get_heap_size(), 16);

a.push(4);
assert_eq!(a.len(), 5);
assert!(a.spilled());
assert_eq!(a.get_size(), 56);
assert_eq!(a.get_heap_size(), 32);

}

0 comments on commit 5d09b91

Please sign in to comment.