Skip to content

Commit 727b6b2

Browse files
committed
Expose a method to return the memory usage of all dynamic atoms.
Signed-off-by: Josh Matthews <[email protected]>
1 parent b92f7eb commit 727b6b2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ name = "string_cache"
2121
[features]
2222
serde_support = ["serde"]
2323
default = ["serde_support"]
24+
malloc_size_of = ["dep:malloc_size_of", "malloc_size_of_derive"]
2425

2526
[dependencies]
2627
precomputed-hash = "0.1"
2728
serde = { version = "1", optional = true }
2829
malloc_size_of = { version = "0.1", default-features = false, optional = true }
30+
malloc_size_of_derive = { version = "0.1", optional = true }
2931
phf_shared = "0.11"
3032
new_debug_unreachable = "1.0.2"
3133
parking_lot = "0.12"

src/dynamic_set.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
use malloc_size_of_derive::MallocSizeOf;
11+
use malloc_size_of::{MallocSizeOf as _, MallocSizeOfOps, MallocShallowSizeOf};
1012
use parking_lot::Mutex;
1113
use std::borrow::Cow;
1214
use std::mem;
@@ -22,6 +24,20 @@ pub(crate) struct Set {
2224
buckets: Box<[Mutex<Option<Box<Entry>>>]>,
2325
}
2426

27+
impl Set {
28+
#[cfg(feature = "malloc_size_of")]
29+
pub(crate) fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
30+
self.buckets.shallow_size_of(ops) +
31+
self.buckets
32+
.iter()
33+
.map(|bucket| {
34+
bucket.lock().as_ref().map_or(0, |entry| entry.size_of(ops))
35+
})
36+
.sum::<usize>()
37+
}
38+
}
39+
40+
#[cfg_attr(feature = "malloc_size_of", derive(MallocSizeOf))]
2541
pub(crate) struct Entry {
2642
pub(crate) string: Box<str>,
2743
pub(crate) hash: u32,

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ pub use static_sets::{EmptyStaticAtomSet, PhfStrSet, StaticAtomSet};
127127
/// Use this if you don’t care about static atoms.
128128
pub type DefaultAtom = Atom<EmptyStaticAtomSet>;
129129

130+
/// Measure the memory usage of the set of dynamic atoms.
131+
#[cfg(feature = "malloc_size_of")]
132+
pub fn size_of_dynamic_atoms(ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
133+
dynamic_set::dynamic_set().size_of(ops)
134+
}
135+
130136
// Some minor tests of internal layout here.
131137
// See ../integration-tests for much more.
132138

0 commit comments

Comments
 (0)