Skip to content

Commit 329913a

Browse files
authored
feat: Expose Sketch Memory Footprint (#136)
1 parent c6ed433 commit 329913a

12 files changed

Lines changed: 92 additions & 0 deletions

File tree

datasketches/src/bloom/sketch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ impl BloomFilter {
561561
self.num_bits_set += 1;
562562
}
563563
}
564+
565+
/// Returns the estimated size of the filter in bytes
566+
pub fn estimated_size(&self) -> usize {
567+
std::mem::size_of::<Self>() + self.bit_array.len() * std::mem::size_of::<u64>()
568+
}
564569
}
565570

566571
#[cfg(test)]

datasketches/src/cpc/pair_table.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,9 @@ impl PairTable {
246246
}
247247
}
248248
}
249+
250+
/// Returns the estimated size of the heap allocations in bytes
251+
pub fn estimated_size(&self) -> usize {
252+
self.slots.capacity() * std::mem::size_of::<u32>()
253+
}
249254
}

datasketches/src/cpc/sketch.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,18 @@ impl CpcSketch {
450450

451451
matrix
452452
}
453+
454+
/// Returns the estimated size of the sketch in bytes
455+
pub fn estimated_size(&self) -> usize {
456+
let heap_size = self.sliding_window.capacity()
457+
+ self
458+
.surprising_value_table
459+
.as_ref()
460+
.map(|t| t.estimated_size())
461+
.unwrap_or(0);
462+
463+
std::mem::size_of::<Self>() + heap_size
464+
}
453465
}
454466

455467
impl CpcSketch {

datasketches/src/hll/array4.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ impl Array4 {
425425

426426
bytes.into_bytes()
427427
}
428+
429+
/// Returns the estimated size of the heap allocations in bytes
430+
pub fn estimated_size(&self) -> usize {
431+
self.bytes.len()
432+
+ self
433+
.aux_map
434+
.as_ref()
435+
.map(|a| a.estimated_size())
436+
.unwrap_or(0)
437+
}
428438
}
429439

430440
#[cfg(test)]

datasketches/src/hll/array6.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ impl Array6 {
272272

273273
bytes.into_bytes()
274274
}
275+
276+
/// Returns the estimated size of the heap allocations in bytes
277+
pub fn estimated_size(&self) -> usize {
278+
self.bytes.len()
279+
}
275280
}
276281

277282
/// Calculate number of bytes needed for k slots with 6 bits each

datasketches/src/hll/array8.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ impl Array8 {
344344

345345
bytes.into_bytes()
346346
}
347+
348+
/// Returns the estimated size of the heap allocations in bytes
349+
pub fn estimated_size(&self) -> usize {
350+
self.bytes.len()
351+
}
347352
}
348353

349354
#[cfg(test)]

datasketches/src/hll/aux_map.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ impl AuxMap {
226226
}
227227
})
228228
}
229+
230+
/// Returns the estimated size of the heap allocations in bytes
231+
pub fn estimated_size(&self) -> usize {
232+
self.entries.len() * std::mem::size_of::<Coupon>()
233+
}
229234
}
230235

231236
/// Iterator over AuxMap entries

datasketches/src/hll/container.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,9 @@ impl Container {
135135
pub fn iter(&self) -> impl Iterator<Item = Coupon> + '_ {
136136
self.coupons.iter().filter(|&&c| !c.is_empty()).copied()
137137
}
138+
139+
/// Returns the estimated size of the heap allocations in bytes
140+
pub fn estimated_size(&self) -> usize {
141+
self.coupons.len() * std::mem::size_of::<Coupon>()
142+
}
138143
}

datasketches/src/hll/sketch.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,19 @@ impl HllSketch {
423423
Mode::Array8(arr) => arr.serialize(self.lg_config_k),
424424
}
425425
}
426+
427+
/// Returns the estimated size of the sketch in bytes
428+
pub fn estimated_size(&self) -> usize {
429+
let heap_size = match &self.mode {
430+
Mode::List { list, .. } => list.container().estimated_size(),
431+
Mode::Set { set, .. } => set.container().estimated_size(),
432+
Mode::Array4(arr) => arr.estimated_size(),
433+
Mode::Array6(arr) => arr.estimated_size(),
434+
Mode::Array8(arr) => arr.estimated_size(),
435+
};
436+
437+
std::mem::size_of::<Self>() + heap_size
438+
}
426439
}
427440

428441
fn promote_container_to_set(container: &Container, hll_type: HllType) -> Mode {

datasketches/src/tdigest/sketch.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,13 @@ impl TDigestMut {
793793
self.reverse_merge = !self.reverse_merge;
794794
self.buffer.clear();
795795
}
796+
797+
/// Returns the estimated size of the sketch in bytes
798+
pub fn estimated_size(&self) -> usize {
799+
std::mem::size_of::<Self>()
800+
+ self.centroids.capacity() * std::mem::size_of::<Centroid>()
801+
+ self.buffer.capacity() * std::mem::size_of::<f64>()
802+
}
796803
}
797804

798805
/// Immutable (frozen) T-Digest sketch for estimating quantiles and ranks.
@@ -1001,6 +1008,11 @@ impl TDigest {
10011008
vec![],
10021009
)
10031010
}
1011+
1012+
/// Returns the estimated size of the sketch in bytes
1013+
pub fn estimated_size(&self) -> usize {
1014+
std::mem::size_of::<Self>() + self.centroids.capacity() * std::mem::size_of::<Centroid>()
1015+
}
10041016
}
10051017

10061018
struct TDigestView<'a> {

0 commit comments

Comments
 (0)