diff --git a/datasketches/src/cpc/pair_table.rs b/datasketches/src/cpc/pair_table.rs index 6e23067..07af835 100644 --- a/datasketches/src/cpc/pair_table.rs +++ b/datasketches/src/cpc/pair_table.rs @@ -246,4 +246,9 @@ impl PairTable { } } } + + /// Returns the size of the heap allocations in bytes + pub fn heap_size(&self) -> usize { + self.slots.len() * std::mem::size_of::() + } } diff --git a/datasketches/src/cpc/sketch.rs b/datasketches/src/cpc/sketch.rs index 5064e45..3fca830 100644 --- a/datasketches/src/cpc/sketch.rs +++ b/datasketches/src/cpc/sketch.rs @@ -450,6 +450,18 @@ impl CpcSketch { matrix } + + /// Returns the size of the sketch in bytes + pub fn size(&self) -> usize { + let heap_size = self.sliding_window.len() + + self + .surprising_value_table + .as_ref() + .map(|t| t.heap_size()) + .unwrap_or(0); + + std::mem::size_of::() + heap_size + } } impl CpcSketch { diff --git a/datasketches/src/hll/array4.rs b/datasketches/src/hll/array4.rs index 27798d0..0d7fb43 100644 --- a/datasketches/src/hll/array4.rs +++ b/datasketches/src/hll/array4.rs @@ -425,6 +425,11 @@ impl Array4 { bytes.into_bytes() } + + /// Returns the size of the heap allocations in bytes + pub fn heap_size(&self) -> usize { + self.bytes.len() + self.aux_map.as_ref().map(|a| a.heap_size()).unwrap_or(0) + } } #[cfg(test)] diff --git a/datasketches/src/hll/array6.rs b/datasketches/src/hll/array6.rs index 2aaff0a..439798c 100644 --- a/datasketches/src/hll/array6.rs +++ b/datasketches/src/hll/array6.rs @@ -272,6 +272,11 @@ impl Array6 { bytes.into_bytes() } + + /// Returns the size of the heap allocations in bytes + pub fn heap_size(&self) -> usize { + self.bytes.len() + } } /// Calculate number of bytes needed for k slots with 6 bits each diff --git a/datasketches/src/hll/array8.rs b/datasketches/src/hll/array8.rs index 21f0d53..7b5805d 100644 --- a/datasketches/src/hll/array8.rs +++ b/datasketches/src/hll/array8.rs @@ -344,6 +344,11 @@ impl Array8 { bytes.into_bytes() } + + /// Returns the size of the heap allocations in bytes + pub fn heap_size(&self) -> usize { + self.bytes.len() + } } #[cfg(test)] diff --git a/datasketches/src/hll/aux_map.rs b/datasketches/src/hll/aux_map.rs index 6e43a03..5358b50 100644 --- a/datasketches/src/hll/aux_map.rs +++ b/datasketches/src/hll/aux_map.rs @@ -226,6 +226,11 @@ impl AuxMap { } }) } + + /// Returns the size of the heap allocations in bytes + pub fn heap_size(&self) -> usize { + self.entries.len() * std::mem::size_of::() + } } /// Iterator over AuxMap entries diff --git a/datasketches/src/hll/container.rs b/datasketches/src/hll/container.rs index 8192b04..ff7ca0d 100644 --- a/datasketches/src/hll/container.rs +++ b/datasketches/src/hll/container.rs @@ -135,4 +135,9 @@ impl Container { pub fn iter(&self) -> impl Iterator + '_ { self.coupons.iter().filter(|&&c| !c.is_empty()).copied() } + + /// Returns the size of the heap allocations in bytes + pub fn heap_size(&self) -> usize { + self.coupons.len() * std::mem::size_of::() + } } diff --git a/datasketches/src/hll/sketch.rs b/datasketches/src/hll/sketch.rs index eaa10ae..90ff374 100644 --- a/datasketches/src/hll/sketch.rs +++ b/datasketches/src/hll/sketch.rs @@ -423,6 +423,19 @@ impl HllSketch { Mode::Array8(arr) => arr.serialize(self.lg_config_k), } } + + /// Returns the size of the sketch in bytes + pub fn size(&self) -> usize { + let heap_size = match &self.mode { + Mode::List { list, .. } => list.container().heap_size(), + Mode::Set { set, .. } => set.container().heap_size(), + Mode::Array4(arr) => arr.heap_size(), + Mode::Array6(arr) => arr.heap_size(), + Mode::Array8(arr) => arr.heap_size(), + }; + + std::mem::size_of::() + heap_size + } } fn promote_container_to_set(container: &Container, hll_type: HllType) -> Mode {