Skip to content

Commit 5d09b91

Browse files
committed
Impl get_size::GetSize (behind feature flag)
Shows memory usage of the struct cf. issue #331
1 parent 0d8b30a commit 5d09b91

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ drain_keep_rest = ["drain_filter"]
2121

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

2526
[dev_dependencies]
2627
bincode = "1.0.1"

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
//! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch).
4646
//!
4747
//! Tracking issue: [rust-lang/rust#34761](https://github.com/rust-lang/rust/issues/34761)
48+
//!
49+
//! ### `get-size`
50+
//!
51+
//! When this optional dependency is enabled, `SmallVec` implements the `get_size::GetSize` trait.
4852
4953
#![no_std]
5054
#![cfg_attr(docsrs, feature(doc_cfg))]
@@ -88,6 +92,8 @@ use serde::{
8892
};
8993
#[cfg(feature = "write")]
9094
use std::io;
95+
#[cfg(feature = "get-size")]
96+
use get_size::GetSize;
9197

9298
/// Error type for APIs with fallible heap allocation
9399
#[derive(Debug)]
@@ -2176,3 +2182,22 @@ impl<const N: usize> io::Write for SmallVec<u8, N> {
21762182
Ok(())
21772183
}
21782184
}
2185+
2186+
#[cfg(feature = "get-size")]
2187+
impl<A: Array> GetSize for SmallVec<A>
2188+
where
2189+
A::Item: GetSize,
2190+
{
2191+
fn get_heap_size(&self) -> usize {
2192+
let mut total = 0;
2193+
if self.spilled() {
2194+
total += self.capacity * A::Item::get_stack_size();
2195+
}
2196+
2197+
for v in self.iter() {
2198+
total += v.get_heap_size();
2199+
}
2200+
2201+
total
2202+
}
2203+
}

src/tests.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,51 @@ fn drain_keep_rest() {
10281028

10291029
assert_eq!(a, SmallVec::<i32, 3>::from_slice(&[1i32, 3, 5, 6, 7, 8]));
10301030
}
1031+
1032+
#[cfg(feature = "get-size")]
1033+
#[test]
1034+
fn test_get_size() {
1035+
use get_size::GetSize;
1036+
1037+
assert_eq!(SmallVec::<[i32; 1]>::get_stack_size(), 24);
1038+
assert_eq!(SmallVec::<[i32; 2]>::get_stack_size(), 24);
1039+
assert_eq!(SmallVec::<[i32; 10]>::get_stack_size(), 56);
1040+
1041+
let mut a: SmallVec<[i32; 2]> = smallvec![];
1042+
assert_eq!(SmallVec::<[i32; 2]>::get_stack_size(), 24);
1043+
assert!(!a.spilled());
1044+
assert_eq!(a.len(), 0);
1045+
assert_eq!(a.get_size(), 24);
1046+
assert_eq!(a.get_heap_size(), 0);
1047+
1048+
a.push(0);
1049+
assert_eq!(a.len(), 1);
1050+
assert!(!a.spilled());
1051+
assert_eq!(a.get_size(), 24);
1052+
assert_eq!(a.get_heap_size(), 0);
1053+
1054+
a.push(1);
1055+
assert_eq!(a.len(), 2);
1056+
assert!(!a.spilled());
1057+
assert_eq!(a.get_size(), 24);
1058+
assert_eq!(a.get_heap_size(), 0);
1059+
1060+
a.push(2);
1061+
assert_eq!(a.len(), 3);
1062+
assert!(a.spilled());
1063+
assert_eq!(a.get_size(), 40);
1064+
assert_eq!(a.get_heap_size(), 16);
1065+
1066+
a.push(3);
1067+
assert_eq!(a.len(), 4);
1068+
assert!(a.spilled());
1069+
assert_eq!(a.get_size(), 40);
1070+
assert_eq!(a.get_heap_size(), 16);
1071+
1072+
a.push(4);
1073+
assert_eq!(a.len(), 5);
1074+
assert!(a.spilled());
1075+
assert_eq!(a.get_size(), 56);
1076+
assert_eq!(a.get_heap_size(), 32);
1077+
1078+
}

0 commit comments

Comments
 (0)