Skip to content

Commit

Permalink
fix: compile in stable
Browse files Browse the repository at this point in the history
  • Loading branch information
gmryuuko committed Jun 22, 2024
1 parent 5c8f2d9 commit 23a8b06
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::boxedset::HashSet;
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Mutex;

/// A arena for storing interned data
Expand Down Expand Up @@ -46,8 +45,6 @@ pub struct Arena<T: ?Sized> {
#[cfg(feature = "deepsize")]
impl<T: ?Sized + deepsize::DeepSizeOf> deepsize::DeepSizeOf for Arena<T> {
fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
use deepsize::DeepSizeOf;

let hashset = self.data.lock().unwrap();
(*hashset).deep_size_of_children(context)
}
Expand Down
11 changes: 6 additions & 5 deletions src/boxedset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ impl<P> HashSet<P> {
#[cfg(feature = "deepsize")]
impl<P: deepsize::DeepSizeOf> deepsize::DeepSizeOf for HashSet<&'static P> {
fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
let pointers = self.0.capacity() * size_of::<&'static P>();
let pointers = self.0.capacity() * std::mem::size_of::<&'static P>();
let heap_memory = self
.0
.keys()
.map(|n| (**n).deep_size_of_children(context) + size_of::<P>())
.map(|n| (**n).deep_size_of_children(context) + std::mem::size_of::<P>())
.sum::<usize>();
pointers + heap_memory
}
}

#[cfg(feature = "deepsize")]
impl <P: deepsize::DeepSizeOf + ?Sized> deepsize::DeepSizeOf for HashSet<Box<P>> {
impl<P: deepsize::DeepSizeOf + ?Sized> deepsize::DeepSizeOf for HashSet<Box<P>> {
fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
let pointers = self.0.capacity() * size_of::<Box<P>>();
let pointers = self.0.capacity() * std::mem::size_of::<Box<P>>();
let heap_memory = self
.0
.keys()
.map(|n| (**n).deep_size_of_children(context) + size_of_val(&**n))
.map(|n| (**n).deep_size_of_children(context) + std::mem::size_of_val(&**n))
.sum::<usize>();
pointers + heap_memory
}
Expand Down Expand Up @@ -77,6 +77,7 @@ impl<P: Deref + Eq + Hash> HashSet<P> {
pub fn len(&self) -> usize {
self.0.len()
}
#[allow(dead_code)] // maybe unused without `deepsize` feature
pub fn capacity(&self) -> usize {
self.0.capacity()
}
Expand Down
27 changes: 15 additions & 12 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,21 @@ mod intern_tests {
#[test]
fn test_deep_size() {
let string1 = String::from("abcdefghijklmnopqrstuvwxyz");
let string2 = String::from("abcdefghijklmnopqrstuvwxyz");
let string3 = String::from("abcdefghijklmnopqrstuvwxyz");
let string2 = string1.clone();
let string3 = string1.clone();
// 3 string are the same, interned only once
let string_size = string1.deep_size_of();

let _ = Intern::new(string1);
let _ = Intern::new(string2);
let _ = Intern::new(string3);
// size of set
let set_size = INTERN_CONTAINERS.with(|m: &mut HashSet<&'static String>| size_of_val(m));
let set_size =
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static String>| std::mem::size_of_val(m));
// size of pointers in the set
let pointers_in_set_size = INTERN_CONTAINERS
.with(|m: &mut HashSet<&'static String>| size_of::<&'static String>() * m.capacity());
let pointers_in_set_size = INTERN_CONTAINERS.with(|m: &mut HashSet<&'static String>| {
std::mem::size_of::<&'static String>() * m.capacity()
});

let interned_size = deep_size_of_interned::<String>();
assert_eq!(interned_size, string_size + set_size + pointers_in_set_size);
Expand Down Expand Up @@ -596,33 +598,34 @@ mod intern_tests {
pointer: a1.pointer.clone(),
};
// size of ArcInside, 16 bytes each
let object_size = size_of::<ArcInside>() * 3;
let object_size = std::mem::size_of::<ArcInside>() * 3;

let _ = Intern::new(a1);
let _ = Intern::new(a2);
let _ = Intern::new(a3);

// size of set
let set_size = INTERN_CONTAINERS.with(|m: &mut HashSet<&'static ArcInside>| size_of_val(m));
let set_size =
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static ArcInside>| std::mem::size_of_val(m));
// size of pointers in the set
let pointers_in_set_size = INTERN_CONTAINERS.with(|m: &mut HashSet<&'static ArcInside>| {
size_of::<&'static ArcInside>() * m.capacity()
std::mem::size_of::<&'static ArcInside>() * m.capacity()
});

// 3 ArcInside has different hash values
INTERN_CONTAINERS.with(|m: &mut HashSet<&'static ArcInside>| assert_eq!(m.len(), 3));

let interned_size = deep_size_of_interned::<ArcInside>();
assert_eq!(
interned_size,
string_size + object_size + set_size + pointers_in_set_size
);

println!("string_size: {}", string_size);
println!("object_size: {}", object_size);
println!("set_size: {}", set_size);
println!("pointers_in_set_size: {}", pointers_in_set_size);
println!("interned_size: {}", interned_size);
assert_eq!(
interned_size,
string_size + object_size + set_size + pointers_in_set_size
);
}
}

Expand Down

0 comments on commit 23a8b06

Please sign in to comment.