Skip to content

perf: Countmin performance improvements - #300

Merged
tisonkun merged 2 commits into
mainfrom
countmin-minimal
Oct 23, 2026
Merged

tisonkun merged 2 commits into
mainfrom
countmin-minimal

Conversation

@pmcgleenon

Copy link
Copy Markdown
Contributor

Replaces the remainder operation with a faster implementation as described in #298

bucket_index reduced each row's hash with a 64-bit `%`, which cost about
as much as the hash itself on update and estimate. Use a mask when
num_buckets is a power of two or Lemire's direct remainder when not.
Both return `h1 % num_buckets`, so bucket placement and serialized
bytes are unchanged. The stored multiplier adds 24 bytes to
estimated_size.
@pmcgleenon pmcgleenon changed the title Countmin performance improvements perf: Countmin performance improvements Oct 22, 2026

@tisonkun tisonkun left a comment

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.

Thanks! One minor comment inline.

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.

Will this perf improvement subtly change the estimation but still within the margin of error?

I think we may assert against the range.

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.

I have reviewed the logic here, and found that:

The reduction is exact, not approximate: fast_remainder and the power-of-two mask both return exactly what % returned, so bucket placement, estimates, and error bounds are bit-for-bit unchanged.

test_fast_remainder_matches_remainder already cross-checks this against % over the full supported divisor range.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for the review, yes the intent is that it will give the same result as before

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.

Oops. This is estimated_size not estimate 🤣

@tisonkun
tisonkun merged commit 7bf60b2 into main Oct 23, 2026
10 checks passed
@tisonkun
tisonkun deleted the countmin-minimal branch October 23, 2026 05:34
Comment on lines +58 to +59
/// Multiplier for [`fast_remainder`], derived from `num_buckets`.
bucket_multiplier: u128,

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.

I personally find this field name strange, but I cannot find a better alternative.

I can live with it anyway.

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.

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.

3 participants