This is an automated email from the ASF dual-hosted git repository.

tisonkun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new 329913a  feat: Expose Sketch Memory Footprint (#136)
329913a is described below

commit 329913a352b387cf99bdc22ae7251b55dcccbff3
Author: Anastasios Bakogiannis <[email protected]>
AuthorDate: Tue Jun 9 10:30:37 2026 +0200

    feat: Expose Sketch Memory Footprint (#136)
---
 datasketches/src/bloom/sketch.rs     |  5 +++++
 datasketches/src/cpc/pair_table.rs   |  5 +++++
 datasketches/src/cpc/sketch.rs       | 12 ++++++++++++
 datasketches/src/hll/array4.rs       | 10 ++++++++++
 datasketches/src/hll/array6.rs       |  5 +++++
 datasketches/src/hll/array8.rs       |  5 +++++
 datasketches/src/hll/aux_map.rs      |  5 +++++
 datasketches/src/hll/container.rs    |  5 +++++
 datasketches/src/hll/sketch.rs       | 13 +++++++++++++
 datasketches/src/tdigest/sketch.rs   | 12 ++++++++++++
 datasketches/src/theta/hash_table.rs |  5 +++++
 datasketches/src/theta/sketch.rs     | 10 ++++++++++
 12 files changed, 92 insertions(+)

diff --git a/datasketches/src/bloom/sketch.rs b/datasketches/src/bloom/sketch.rs
index 19142a3..bfc5f63 100644
--- a/datasketches/src/bloom/sketch.rs
+++ b/datasketches/src/bloom/sketch.rs
@@ -561,6 +561,11 @@ impl BloomFilter {
             self.num_bits_set += 1;
         }
     }
+
+    /// Returns the estimated size of the filter in bytes
+    pub fn estimated_size(&self) -> usize {
+        std::mem::size_of::<Self>() + self.bit_array.len() * 
std::mem::size_of::<u64>()
+    }
 }
 
 #[cfg(test)]
diff --git a/datasketches/src/cpc/pair_table.rs 
b/datasketches/src/cpc/pair_table.rs
index 6e23067..d36d68d 100644
--- a/datasketches/src/cpc/pair_table.rs
+++ b/datasketches/src/cpc/pair_table.rs
@@ -246,4 +246,9 @@ impl PairTable {
             }
         }
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_size(&self) -> usize {
+        self.slots.capacity() * std::mem::size_of::<u32>()
+    }
 }
diff --git a/datasketches/src/cpc/sketch.rs b/datasketches/src/cpc/sketch.rs
index 5064e45..58cc232 100644
--- a/datasketches/src/cpc/sketch.rs
+++ b/datasketches/src/cpc/sketch.rs
@@ -450,6 +450,18 @@ impl CpcSketch {
 
         matrix
     }
+
+    /// Returns the estimated size of the sketch in bytes
+    pub fn estimated_size(&self) -> usize {
+        let heap_size = self.sliding_window.capacity()
+            + self
+                .surprising_value_table
+                .as_ref()
+                .map(|t| t.estimated_size())
+                .unwrap_or(0);
+
+        std::mem::size_of::<Self>() + heap_size
+    }
 }
 
 impl CpcSketch {
diff --git a/datasketches/src/hll/array4.rs b/datasketches/src/hll/array4.rs
index 27798d0..268db40 100644
--- a/datasketches/src/hll/array4.rs
+++ b/datasketches/src/hll/array4.rs
@@ -425,6 +425,16 @@ impl Array4 {
 
         bytes.into_bytes()
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_size(&self) -> usize {
+        self.bytes.len()
+            + self
+                .aux_map
+                .as_ref()
+                .map(|a| a.estimated_size())
+                .unwrap_or(0)
+    }
 }
 
 #[cfg(test)]
diff --git a/datasketches/src/hll/array6.rs b/datasketches/src/hll/array6.rs
index 2aaff0a..42646d3 100644
--- a/datasketches/src/hll/array6.rs
+++ b/datasketches/src/hll/array6.rs
@@ -272,6 +272,11 @@ impl Array6 {
 
         bytes.into_bytes()
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_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..55cf384 100644
--- a/datasketches/src/hll/array8.rs
+++ b/datasketches/src/hll/array8.rs
@@ -344,6 +344,11 @@ impl Array8 {
 
         bytes.into_bytes()
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_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..fed7fac 100644
--- a/datasketches/src/hll/aux_map.rs
+++ b/datasketches/src/hll/aux_map.rs
@@ -226,6 +226,11 @@ impl AuxMap {
             }
         })
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_size(&self) -> usize {
+        self.entries.len() * std::mem::size_of::<Coupon>()
+    }
 }
 
 /// Iterator over AuxMap entries
diff --git a/datasketches/src/hll/container.rs 
b/datasketches/src/hll/container.rs
index 8192b04..716f045 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<Item = Coupon> + '_ {
         self.coupons.iter().filter(|&&c| !c.is_empty()).copied()
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_size(&self) -> usize {
+        self.coupons.len() * std::mem::size_of::<Coupon>()
+    }
 }
diff --git a/datasketches/src/hll/sketch.rs b/datasketches/src/hll/sketch.rs
index eaa10ae..91d79b0 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 estimated size of the sketch in bytes
+    pub fn estimated_size(&self) -> usize {
+        let heap_size = match &self.mode {
+            Mode::List { list, .. } => list.container().estimated_size(),
+            Mode::Set { set, .. } => set.container().estimated_size(),
+            Mode::Array4(arr) => arr.estimated_size(),
+            Mode::Array6(arr) => arr.estimated_size(),
+            Mode::Array8(arr) => arr.estimated_size(),
+        };
+
+        std::mem::size_of::<Self>() + heap_size
+    }
 }
 
 fn promote_container_to_set(container: &Container, hll_type: HllType) -> Mode {
diff --git a/datasketches/src/tdigest/sketch.rs 
b/datasketches/src/tdigest/sketch.rs
index 51e2b62..9e14759 100644
--- a/datasketches/src/tdigest/sketch.rs
+++ b/datasketches/src/tdigest/sketch.rs
@@ -793,6 +793,13 @@ impl TDigestMut {
         self.reverse_merge = !self.reverse_merge;
         self.buffer.clear();
     }
+
+    /// Returns the estimated size of the sketch in bytes
+    pub fn estimated_size(&self) -> usize {
+        std::mem::size_of::<Self>()
+            + self.centroids.capacity() * std::mem::size_of::<Centroid>()
+            + self.buffer.capacity() * std::mem::size_of::<f64>()
+    }
 }
 
 /// Immutable (frozen) T-Digest sketch for estimating quantiles and ranks.
@@ -1001,6 +1008,11 @@ impl TDigest {
             vec![],
         )
     }
+
+    /// Returns the estimated size of the sketch in bytes
+    pub fn estimated_size(&self) -> usize {
+        std::mem::size_of::<Self>() + self.centroids.capacity() * 
std::mem::size_of::<Centroid>()
+    }
 }
 
 struct TDigestView<'a> {
diff --git a/datasketches/src/theta/hash_table.rs 
b/datasketches/src/theta/hash_table.rs
index fb8fec9..1ee498d 100644
--- a/datasketches/src/theta/hash_table.rs
+++ b/datasketches/src/theta/hash_table.rs
@@ -381,6 +381,11 @@ impl ThetaHashTable {
     fn get_stride(key: u64, lg_size: u8) -> usize {
         (2 * ((key >> (lg_size)) & STRIDE_MASK) + 1) as usize
     }
+
+    /// Returns the estimated size of the heap allocations in bytes
+    pub fn estimated_size(&self) -> usize {
+        self.entries.capacity() * std::mem::size_of::<u64>()
+    }
 }
 
 /// Compute initial lg_size for hash table based on target lg_size, minimum 
lg_size, and resize
diff --git a/datasketches/src/theta/sketch.rs b/datasketches/src/theta/sketch.rs
index 1313675..6ee9a23 100644
--- a/datasketches/src/theta/sketch.rs
+++ b/datasketches/src/theta/sketch.rs
@@ -313,6 +313,11 @@ impl ThetaSketch {
         )
         .expect("theta should always be valid")
     }
+
+    /// Returns the estimated size of the sketch in bytes
+    pub fn estimated_size(&self) -> usize {
+        std::mem::size_of::<Self>() + self.table.estimated_size()
+    }
 }
 
 impl ThetaSketchView for ThetaSketch {
@@ -888,6 +893,11 @@ impl CompactThetaSketch {
             empty,
         })
     }
+
+    /// Returns the estimated size of the sketch in bytes
+    pub fn estimated_size(&self) -> usize {
+        std::mem::size_of::<Self>() + self.entries.capacity() * 
std::mem::size_of::<u64>()
+    }
 }
 
 impl ThetaSketchView for CompactThetaSketch {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to