Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datasketches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = []
default = ["hll", "cpc", "bloom", "countmin", "frequencies", "tdigest", "theta"]
Comment thread
tisonkun marked this conversation as resolved.
Outdated

# Each sketch has its own feature, so that users can opt in to only the sketches they need.
bloom = []
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/bloom/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/cpc/pair_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
}
}
12 changes: 12 additions & 0 deletions datasketches/src/cpc/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment thread
tisonkun marked this conversation as resolved.
}

impl CpcSketch {
Expand Down
10 changes: 10 additions & 0 deletions datasketches/src/hll/array4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Comment on lines +429 to +437

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can add but not necessay

}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/hll/array6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/hll/array8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/hll/aux_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/hll/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
}
}
13 changes: 13 additions & 0 deletions datasketches/src/hll/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment thread
tisonkun marked this conversation as resolved.
}

fn promote_container_to_set(container: &Container, hll_type: HllType) -> Mode {
Expand Down
12 changes: 12 additions & 0 deletions datasketches/src/tdigest/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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> {
Expand Down
5 changes: 5 additions & 0 deletions datasketches/src/theta/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions datasketches/src/theta/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down