Skip to content

Conversation

@PsiACE
Copy link
Member

@PsiACE PsiACE commented Dec 22, 2025

Related to #35

  • Implement Count-Min sketch.
  • Add unit tests for core behavior, merge, serialization, and cases adapted from rust-count-min-sketch.

Copilot AI review requested due to automatic review settings December 22, 2025 13:16
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a Count-Min sketch data structure for approximate frequency counting in streaming data. The implementation provides configurable accuracy through relative error and confidence parameters, supports negative weights, and includes serialization/deserialization compatibility with the DataSketches format.

Key changes:

  • Core Count-Min sketch with update, estimate, merge, and bound calculation operations
  • Helper functions for parameter suggestions based on desired error and confidence levels
  • Serialization support compatible with DataSketches Count-Min format
  • Comprehensive test suite covering basic operations, edge cases, and serialization

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/lib.rs Adds countmin module to the library's public API
src/countmin/mod.rs Module definition with documentation and public exports
src/countmin/sketch.rs Core Count-Min sketch implementation with update, estimation, merge, and serialization methods
src/countmin/serialization.rs Serialization constants and helper functions for DataSketches format compatibility
tests/countmin_test.rs Comprehensive test suite covering initialization, updates, bounds, merge, serialization, and edge cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +120 to +121
if confidence == 1.0 {
return 127;
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The magic number 127 returned when confidence is 1.0 lacks explanation. This appears to be chosen as a practical upper limit (since num_hashes is u8 and 127 is the max signed byte value), but it would be clearer to either use u8::MAX (255) if that's the intent, or document why 127 specifically is chosen. If 127 is a deliberate choice related to the algorithm's behavior, this should be documented in a comment.

Copilot uses AI. Check for mistakes.
Comment on lines +117 to +118
(0.0..=1.0).contains(&confidence),
"confidence must be between 0 and 1.0 (inclusive)"
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The documentation states that the function panics if confidence is not in (0, 1], which means 0 should be excluded. However, the assertion uses a closed interval [0.0, 1.0] which includes 0. This creates an inconsistency between the documentation and implementation. If confidence is 0.0, the function would not panic but would cause a division by zero at line 123 (1.0 / (1.0 - 0.0) = 1.0 / 1.0 = 1.0, which is fine, but conceptually a confidence of 0 doesn't make sense for this algorithm). The documentation should either be corrected to say [0, 1] or the assertion should exclude 0.0.

Suggested change
(0.0..=1.0).contains(&confidence),
"confidence must be between 0 and 1.0 (inclusive)"
confidence > 0.0 && confidence <= 1.0,
"confidence must be in (0, 1.0]"

Copilot uses AI. Check for mistakes.
///
/// Panics if `relative_error` is negative.
pub fn suggest_num_buckets(relative_error: f64) -> u32 {
assert!(relative_error >= 0.0, "relative_error must be at least 0");
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

When relative_error is 0.0, the function will compute E/0.0, resulting in infinity. Converting infinity to u32 via 'as' casting results in saturating behavior, which may produce unexpected results. Consider adding validation to reject a relative_error of 0.0, or document this edge case behavior.

Suggested change
assert!(relative_error >= 0.0, "relative_error must be at least 0");
assert!(relative_error >= 0.0, "relative_error must be at least 0");
if relative_error == 0.0 {
// For zero relative error, return the maximum possible number of buckets.
// This preserves the previous behavior where E / 0.0 produced infinity,
// which saturated to u32::MAX when cast with `as u32`.
return u32::MAX;
}

Copilot uses AI. Check for mistakes.
Comment on lines +366 to +367
} else {
value.wrapping_neg()
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The abs_i64 function handles i64::MIN incorrectly. When value is i64::MIN, wrapping_neg() returns i64::MIN (because -i64::MIN overflows). This means abs_i64(i64::MIN) returns i64::MIN, which is still negative. This negative value is then added to total_weight at line 138, potentially causing incorrect total_weight calculations. Consider either documenting this limitation or using value.abs() with proper overflow handling, or saturating to i64::MAX.

Suggested change
} else {
value.wrapping_neg()
} else if value == i64::MIN {
i64::MAX
} else {
value.abs()

Copilot uses AI. Check for mistakes.
@notfilippo
Copy link
Contributor

notfilippo commented Dec 22, 2025

Cool! Thanks. I'll take a look soon...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants