Skip to content

Commit cf03b15

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

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-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", 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<T, const N: usize> GetSize for SmallVec<T, N>
2188+
where
2189+
T: GetSize,
2190+
{
2191+
fn get_heap_size(&self) -> usize {
2192+
let mut total = 0;
2193+
if self.spilled() {
2194+
total += self.capacity() * T::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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,49 @@ fn drain_keep_rest() {
10281028

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

0 commit comments

Comments
 (0)