Skip to content

Commit 40460fc

Browse files
committed
Impl get_size::GetSize (behind feature flag)
Shows memory usage of the struct cf. issue #331
1 parent 93b0e20 commit 40460fc

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ debugger_visualizer = []
2828
[dependencies]
2929
serde = { version = "1", optional = true, default-features = false }
3030
arbitrary = { version = "1", optional = true }
31+
get-size = { version = "0.1.4", optional = true, default-features = false }
3132

3233
[dev_dependencies]
3334
bincode = "1.0.1"

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@
8888
//! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch).
8989
//!
9090
//! Tracking issue: [rust-lang/rust#34761](https://github.com/rust-lang/rust/issues/34761)
91+
//!
92+
//! ### `get-size`
93+
//!
94+
//! When this optional dependency is enabled, `SmallVec` implements the `get_size::GetSize` trait.
9195
9296
#![no_std]
9397
#![cfg_attr(docsrs, feature(doc_cfg))]
@@ -141,6 +145,9 @@ use std::io;
141145
#[cfg(feature = "drain_keep_rest")]
142146
use core::mem::ManuallyDrop;
143147

148+
#[cfg(feature = "get-size")]
149+
use get_size::GetSize;
150+
144151
/// Creates a [`SmallVec`] containing the arguments.
145152
///
146153
/// `smallvec!` allows `SmallVec`s to be defined with the same syntax as array expressions.
@@ -2469,3 +2476,22 @@ impl<T> Clone for ConstNonNull<T> {
24692476
}
24702477

24712478
impl<T> Copy for ConstNonNull<T> {}
2479+
2480+
#[cfg(feature = "get-size")]
2481+
impl<A: Array> GetSize for SmallVec<A>
2482+
where
2483+
A::Item: GetSize,
2484+
{
2485+
fn get_heap_size(&self) -> usize {
2486+
let mut total = 0;
2487+
if self.spilled() {
2488+
total += self.capacity * A::Item::get_stack_size();
2489+
}
2490+
2491+
for v in self.iter() {
2492+
total += v.get_heap_size();
2493+
}
2494+
2495+
total
2496+
}
2497+
}

src/tests.rs

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

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

0 commit comments

Comments
 (0)