Skip to content

Commit 38863e8

Browse files
amandasaurusmbrubeck
authored andcommitted
Impl get_size::GetSize (behind feature flag)
Shows memory usage of the struct cf. issue #331
1 parent 93b0e20 commit 38863e8

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-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", 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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,70 @@ fn drain_keep_rest() {
10231023

10241024
assert_eq!(a, SmallVec::<[i32; 3]>::from_slice(&[1i32, 3, 5, 6, 7, 8]));
10251025
}
1026+
1027+
#[cfg(all(feature = "get-size", target_pointer_width = "64"))]
1028+
mod get_size {
1029+
use super::*;
1030+
1031+
#[test]
1032+
fn end_to_end1() {
1033+
use ::get_size::GetSize;
1034+
1035+
let mut a: SmallVec<[i32; 2]> = smallvec![];
1036+
assert!(!a.spilled());
1037+
assert_eq!(a.len(), 0);
1038+
assert_eq!(a.get_size(), 24);
1039+
assert_eq!(a.get_heap_size(), 0);
1040+
1041+
a.push(0);
1042+
assert_eq!(a.len(), 1);
1043+
assert!(!a.spilled());
1044+
assert_eq!(a.get_size(), 24);
1045+
assert_eq!(a.get_heap_size(), 0);
1046+
1047+
a.push(1);
1048+
assert_eq!(a.len(), 2);
1049+
assert!(!a.spilled());
1050+
assert_eq!(a.get_size(), 24);
1051+
assert_eq!(a.get_heap_size(), 0);
1052+
1053+
a.push(2);
1054+
assert_eq!(a.len(), 3);
1055+
assert!(a.spilled());
1056+
assert_eq!(a.get_size(), 40);
1057+
assert_eq!(a.get_heap_size(), 16);
1058+
1059+
a.push(3);
1060+
assert_eq!(a.len(), 4);
1061+
assert!(a.spilled());
1062+
assert_eq!(a.get_size(), 40);
1063+
assert_eq!(a.get_heap_size(), 16);
1064+
1065+
a.push(4);
1066+
assert_eq!(a.len(), 5);
1067+
assert!(a.spilled());
1068+
assert_eq!(a.get_size(), 56);
1069+
assert_eq!(a.get_heap_size(), 32);
1070+
1071+
}
1072+
1073+
#[cfg(not(feature = "union"))]
1074+
#[test]
1075+
fn stack_size_no_union1() {
1076+
use ::get_size::GetSize;
1077+
1078+
assert_eq!(SmallVec::<[i32; 2]>::get_stack_size(), 24);
1079+
assert_eq!(SmallVec::<[i32; 10]>::get_stack_size(), 56);
1080+
}
1081+
1082+
#[cfg(feature="union")]
1083+
#[test]
1084+
fn stack_size_union1() {
1085+
use ::get_size::GetSize;
1086+
1087+
assert_eq!(SmallVec::<[i32; 2]>::get_stack_size(), 24);
1088+
assert_eq!(SmallVec::<[i32; 10]>::get_stack_size(), 48);
1089+
}
1090+
1091+
}
1092+

0 commit comments

Comments
 (0)