From bbfa1b4eb9b2ff90d7328281205821cd40af2088 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 15 Feb 2025 12:57:10 +1300 Subject: [PATCH 1/4] Implement MallocSizeOf for ThinVec Signed-off-by: Nico Burns --- .github/workflows/rust.yml | 2 ++ Cargo.toml | 3 ++- src/lib.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 54e4fba..796c73e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -31,6 +31,8 @@ jobs: - uses: actions/checkout@v2 - name: Build run: cargo build --verbose + - name: Build (malloc_size_of) + run: cargo build --features=malloc_size_of --verbose - name: Run tests run: cargo test --verbose - name: Run tests diff --git a/Cargo.toml b/Cargo.toml index 0d74561..2600887 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,8 @@ std = [] gecko-ffi = [] [dependencies] -serde = {version = "1.0", optional = true} +serde = { version = "1.0", optional = true } +malloc_size_of = { version = "0.1", default-features = false, optional = true } [dev-dependencies] serde_test = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 6616514..39ef50b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,6 +163,9 @@ use core::{fmt, mem, ptr, slice}; use impl_details::*; +#[cfg(feature = "malloc_size_of")] +use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; + // modules: a simple way to cfg a whole bunch of impl details at once #[cfg(not(feature = "gecko-ffi"))] @@ -1873,6 +1876,33 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ThinVec { } } +#[cfg(feature = "malloc_size_of")] +impl MallocShallowSizeOf for ThinVec { + fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + if self.capacity() == 0 { + // If it's the singleton we might not be a heap pointer. + return 0; + } + + assert_eq!( + std::mem::size_of::(), + std::mem::size_of::<*const ()>() + ); + unsafe { ops.malloc_size_of(*(self as *const Self as *const *const ())) } + } +} + +#[cfg(feature = "malloc_size_of")] +impl MallocSizeOf for ThinVec { + fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + let mut n = self.shallow_size_of(ops); + for elem in self.iter() { + n += elem.size_of(ops); + } + n + } +} + macro_rules! array_impls { ($($N:expr)*) => {$( impl PartialEq<[B; $N]> for ThinVec where A: PartialEq { From 16163cbc3cc43fe9879a4e9500ebd6d42ddc623d Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 15 Feb 2025 12:58:18 +1300 Subject: [PATCH 2/4] Bump version to 0.2.14 Signed-off-by: Nico Burns --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2600887..4ff6ecf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2018" name = "thin-vec" -version = "0.2.13" +version = "0.2.14" authors = ["Aria Beingessner "] description = "A vec that takes up less space on the stack" license = "MIT/Apache-2.0" From 67f857625f6d125e27ca8c2b7b1b70bdcada403b Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 15 Feb 2025 12:59:22 +1300 Subject: [PATCH 3/4] Add changelog entry for 0.2.14 Signed-off-by: Nico Burns --- RELEASES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RELEASES.md b/RELEASES.md index 9e398e5..e87ad6f 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,3 +1,6 @@ +# Version 0.2.14 (2025-02-19) +* Add "malloc_size_of" feature for heap size measurement support + # Version 0.2.13 (2023-12-02) * add default-on "std" feature for no_std support From c88c1495f71669a568539a240b214035319258cf Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Thu, 27 Feb 2025 13:03:32 +1300 Subject: [PATCH 4/4] Allow no_global_oom_handling cfg Signed-off-by: Nico Burns --- Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 4ff6ecf..c5c86d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,6 @@ malloc_size_of = { version = "0.1", default-features = false, optional = true } [dev-dependencies] serde_test = "1.0" + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_global_oom_handling)'] }