From b703a33531cd8606aed9009d6e5ee824240156aa Mon Sep 17 00:00:00 2001 From: David Roundy Date: Wed, 17 Apr 2024 14:21:05 -0700 Subject: [PATCH] add support for deepsize --- CHANGELOG.md | 4 ++++ Cargo.toml | 3 ++- src/arc.rs | 22 ++++++++++++++++++++++ src/arena.rs | 16 ++++++++++++++++ src/boxedset.rs | 14 ++++++++++++++ src/intern.rs | 16 ++++++++++++++++ src/lib.rs | 6 ++++++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac13240..9647fe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +* 0.8.2 - April 17, 2024 + + - Added support for `deepsize` for all three intern types. + * 0.8.1 - April 11, 2024 - Increased MSRV to 1.70. diff --git a/Cargo.toml b/Cargo.toml index cbf0cbe..4f1d599 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "internment" -version = "0.8.1" +version = "0.8.2" authors = ["David Roundy "] edition = "2018" @@ -32,6 +32,7 @@ tinyset = { version = "0.4.2", optional = true } memorable-wordlist = { version = "0.1.7", optional = true } hashbrown = { version = "0.14.3" } serde = { version = "1.0", optional = true } +deepsize = { version = "0.2.0", optional = true } arc-interner = { version = "0.7", optional = true } diff --git a/src/arc.rs b/src/arc.rs index f648334..0e92b3b 100644 --- a/src/arc.rs +++ b/src/arc.rs @@ -47,6 +47,28 @@ pub struct ArcIntern { pub(crate) pointer: std::ptr::NonNull>, } +#[cfg(feature = "deepsize")] +impl deepsize::DeepSizeOf for ArcIntern { + fn deep_size_of_children(&self, _context: &mut deepsize::Context) -> usize { + 0 + } +} + +#[cfg_attr(docsrs, doc(cfg(all(feature = "deepsize", feature = "arc"))))] +/// Return the memory used by all interned objects of the given type. +#[cfg(feature = "deepsize")] +pub fn deep_size_of_arc_interned< + T: ?Sized + Eq + Hash + Send + Sync + 'static + deepsize::DeepSizeOf, +>() -> usize { + let x = ArcIntern::::get_container(); + let pointers = x.capacity() * std::mem::size_of::>(); + let heap_memory = x + .iter() + .map(|n| std::mem::size_of::() + n.key().0.data.deep_size_of()) + .sum::(); + pointers + heap_memory +} + unsafe impl Send for ArcIntern {} unsafe impl Sync for ArcIntern {} diff --git a/src/arena.rs b/src/arena.rs index bf7a095..0b212be 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -41,12 +41,28 @@ use std::sync::Mutex; pub struct Arena { data: Mutex>>, } + +#[cfg(feature = "deepsize")] +impl deepsize::DeepSizeOf for Arena { + fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize { + let hashset = self.data.lock().unwrap(); + hashset.deep_size_of_children(context) + } +} + /// An interned object reference with the data stored in an `Arena`. #[cfg_attr(docsrs, doc(cfg(feature = "arena")))] pub struct ArenaIntern<'a, T: ?Sized> { pointer: &'a T, } +#[cfg(feature = "deepsize")] +impl<'a, T: ?Sized + deepsize::DeepSizeOf> deepsize::DeepSizeOf for ArenaIntern<'a, T> { + fn deep_size_of_children(&self, _context: &mut deepsize::Context) -> usize { + std::mem::size_of::<&T>() + } +} + impl<'a, T: ?Sized> Clone for ArenaIntern<'a, T> { fn clone(&self) -> Self { ArenaIntern { diff --git a/src/boxedset.rs b/src/boxedset.rs index 91a5509..febeb0c 100644 --- a/src/boxedset.rs +++ b/src/boxedset.rs @@ -16,6 +16,20 @@ impl

HashSet

{ HashSet(HashMap::new()) } } + +#[cfg(feature = "deepsize")] +impl deepsize::DeepSizeOf for HashSet

{ + fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize { + let pointers = self.0.capacity() * std::mem::size_of::

(); + let heap_memory = self + .0 + .keys() + .map(|n| n.deep_size_of_children(context)) + .sum::(); + pointers + heap_memory + } +} + impl HashSet

{ pub fn get<'a, Q: ?Sized + Eq + Hash>(&'a self, key: &Q) -> Option<&'a P> where diff --git a/src/intern.rs b/src/intern.rs index d38c991..dd86a3d 100644 --- a/src/intern.rs +++ b/src/intern.rs @@ -63,6 +63,22 @@ pub struct Intern { pointer: &'static T, } +#[cfg(feature = "deepsize")] +impl deepsize::DeepSizeOf for Intern { + fn deep_size_of_children(&self, _context: &mut deepsize::Context) -> usize { + 0 + } +} + +#[cfg_attr(docsrs, doc(cfg(all(feature = "deepsize", feature = "intern"))))] +/// Return the memory used by all interned objects of the given type. +#[cfg(feature = "deepsize")] +pub fn deep_size_of_interned() -> usize +{ + use deepsize::DeepSizeOf; + INTERN_CONTAINERS.with(|m: &mut HashSet<&'static T>| -> usize { m.deep_size_of() }) +} + #[test] fn has_niche() { assert_eq!( diff --git a/src/lib.rs b/src/lib.rs index 7c1054b..63bf962 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,3 +79,9 @@ mod arc_dst; #[cfg(feature = "arc")] pub use arc::ArcIntern; + +#[cfg(all(feature = "arc", feature = "deepsize"))] +pub use arc::deep_size_of_arc_interned; + +#[cfg(all(feature = "intern", feature = "deepsize"))] +pub use intern::deep_size_of_interned;