Skip to content

Commit

Permalink
add support for deepsize
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Apr 17, 2024
1 parent 1c4931f commit b703a33
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "internment"
version = "0.8.1"
version = "0.8.2"
authors = ["David Roundy <[email protected]>"]
edition = "2018"

Expand Down Expand Up @@ -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 }

Expand Down
22 changes: 22 additions & 0 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ pub struct ArcIntern<T: ?Sized + Eq + Hash + Send + Sync + 'static> {
pub(crate) pointer: std::ptr::NonNull<RefCount<T>>,
}

#[cfg(feature = "deepsize")]
impl<T: ?Sized + Eq + Hash + Send + Sync + 'static> deepsize::DeepSizeOf for ArcIntern<T> {
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::<T>::get_container();
let pointers = x.capacity() * std::mem::size_of::<BoxRefCount<T>>();
let heap_memory = x
.iter()
.map(|n| std::mem::size_of::<usize>() + n.key().0.data.deep_size_of())
.sum::<usize>();
pointers + heap_memory
}

unsafe impl<T: ?Sized + Eq + Hash + Send + Sync> Send for ArcIntern<T> {}
unsafe impl<T: ?Sized + Eq + Hash + Send + Sync> Sync for ArcIntern<T> {}

Expand Down
16 changes: 16 additions & 0 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ use std::sync::Mutex;
pub struct Arena<T: ?Sized> {
data: Mutex<HashSet<Box<T>>>,
}

#[cfg(feature = "deepsize")]
impl<T: ?Sized + deepsize::DeepSizeOf> deepsize::DeepSizeOf for Arena<T> {
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<T>`.
#[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 {
Expand Down
14 changes: 14 additions & 0 deletions src/boxedset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ impl<P> HashSet<P> {
HashSet(HashMap::new())
}
}

#[cfg(feature = "deepsize")]
impl<P: deepsize::DeepSizeOf> deepsize::DeepSizeOf for HashSet<P> {
fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
let pointers = self.0.capacity() * std::mem::size_of::<P>();
let heap_memory = self
.0
.keys()
.map(|n| n.deep_size_of_children(context))
.sum::<usize>();
pointers + heap_memory
}
}

impl<P: Deref + Eq + Hash> HashSet<P> {
pub fn get<'a, Q: ?Sized + Eq + Hash>(&'a self, key: &Q) -> Option<&'a P>
where
Expand Down
16 changes: 16 additions & 0 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ pub struct Intern<T: 'static + ?Sized> {
pointer: &'static T,
}

#[cfg(feature = "deepsize")]
impl<T: 'static + ?Sized> deepsize::DeepSizeOf for Intern<T> {
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<T: Eq + Hash + Send + Sync + 'static + deepsize::DeepSizeOf>() -> usize
{
use deepsize::DeepSizeOf;
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static T>| -> usize { m.deep_size_of() })
}

#[test]
fn has_niche() {
assert_eq!(
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit b703a33

Please sign in to comment.