tisonkun opened a new pull request, #386:
URL: https://github.com/apache/datasketches-rust/pull/386
Grouped distinct counts can create thousands of CPC sketches that see only
one to three distinct values. On the two ClickBench Q13 shards below, 17,900 of
18,315 groups and 55,876 of 56,622 groups fit that description. Each sketch
previously allocated a 16-byte pair table on its first update.
This change stores the four initial slots inside `PairTable`. The fourth
distinct coupon grows the table onto the heap, and a table that shrinks back to
four slots uses inline storage again. A regression test exercises colliding
coupons across both transitions. Related to #385.
## ClickBench Q13
Each shard contains one million rows. The replay filters out empty search
phrases, assigns one CPC sketch (`lg_k = 11`) per remaining phrase, and feeds
it the original `i64` UserIDs in file order. Timing covers sketch construction,
updates, reading every estimate, and dropping the sketches. Parquet decoding,
group-key lookup, and SQL sorting are outside the timed region.
| Shard | Rows after filter | Groups | Before, ms (min–max) | After, ms
(min–max) | Time saved |
|---------|------------------:|-------:|----------------------:|----------------------:|-----------:|
| hits_0 | 69,354 | 18,315 | 1.043 (1.026–1.107) | 0.773
(0.758–0.828) | 25.9% |
| hits_37 | 91,906 | 56,622 | 2.229 (2.152–2.335) | 1.467
(1.440–1.558) | 34.2% |
| Shard | Backing heap before → after, B | Heap saved | Sketches including
objects before → after, B | Total saved |
|---------|-------------------------------:|-----------:|---------------------------------------------:|------------:|
| hits_0 | 337,216 → 50,688 | 85.0% |
2,095,456 → 1,808,928 | 13.7% |
| hits_37 | 970,496 → 76,448 | 92.1% |
6,406,208 → 5,512,160 | 14.0% |
Backing heap is the sum of `CpcSketch::estimated_size()` over all live
groups after the last update. The total adds `groups × size_of::<CpcSketch>()`,
which was 96 bytes per object on both revisions. These figures exclude input
rows, group keys, and allocator overhead. Every group's estimate bits and
serialized bytes matched across revisions on both shards.
## Repository benchmark
The new offline workload creates 16,384 sketches per round, shuffles the
updates with a fixed seed, and feeds each distinct user twice. In every 64
groups, 60 have one distinct user, two have two, one has three, and one has
sixteen. The last group exercises the transition to heap storage.
| Scenario | Rounds | Before, ms (min–max) | After,
ms (min–max) |
|----------------------------------|-------:|---------------------:|--------------------:|
| `cpc::aggregation::small_groups` | 350 | 295 (286–308) |
205 (200–209) |
| `cpc::update::u64` | 3,000 | 249 (247–250) |
247 (246–251) |
Measured on an Apple M4 Max, macOS 26.6.2 arm64, `rustc 1.101.0-nightly
(8006091eb)`, using Cargo's optimized profiles. The benchmark-only commit
`d92afee` provides the baseline on top of `6d8b342`. The inline implementation
is `b178321`. Tables report medians over nine samples, alternating which
revision ran first. Each shard sample averaged 128 replays after eight warm-ups.
```sh
cargo x bench -- cpc::aggregation::small_groups --exact --repeat 9 --raw
cargo x bench -- cpc::update::u64 --exact --repeat 9 --raw
```
<details>
<summary>Reproduce the two shard replays</summary>
Download the partitioned ClickBench data and use DuckDB to export the dense
group ID and UserID in file order.
```sh
for shard in 0 37; do
curl -fsSLO
"https://datasets.clickhouse.com/hits_compatible/athena_partitioned/hits_${shard}.parquet"
duckdb -c "COPY (
SELECT dense_rank() OVER (ORDER BY SearchPhrase) - 1 AS grp, UserID
FROM read_parquet('hits_${shard}.parquet', file_row_number=true)
WHERE SearchPhrase <> ''
ORDER BY file_row_number
) TO 'q13_${shard}.csv' (HEADER false)"
done
```
Build this Rust binary in release mode against each revision with the `cpc`
feature. Run it with each CSV path as its argument. It prints bytes per object,
live backing bytes, and milliseconds per aggregation. An optional second
argument writes each group’s estimate and length-prefixed serialized sketch for
bytewise comparison.
```rust
use datasketches::cpc::CpcSketch;
use std::{env, fs, hint::black_box, time::Instant};
fn aggregate(rows: &[(usize, i64)], groups: usize) -> Vec<CpcSketch> {
let mut sketches: Vec<_> = (0..groups).map(|_|
CpcSketch::new(11).unwrap()).collect();
for &(group, user) in rows {
sketches[group].update(user);
}
sketches
}
fn main() {
let rows: Vec<(usize, i64)> =
fs::read_to_string(env::args().nth(1).unwrap()).unwrap()
.lines().map(|s| {
let (g, u) = s.split_once(',').unwrap();
(g.parse().unwrap(), u.parse().unwrap())
}).collect();
let groups = rows.iter().map(|r| r.0).max().unwrap() + 1;
let sketches = aggregate(&rows, groups);
let heap: usize = sketches.iter().map(CpcSketch::estimated_size).sum();
println!("groups={groups} object={} heap={heap}",
size_of::<CpcSketch>());
if let Some(path) = env::args().nth(2) {
let mut images = Vec::new();
for s in &sketches {
let bytes = s.serialize();
images.extend(s.estimate().to_bits().to_le_bytes());
images.extend((bytes.len() as u64).to_le_bytes());
images.extend(bytes);
}
fs::write(path, images).unwrap();
}
drop(sketches);
for _ in 0..8 {
let s = aggregate(black_box(&rows), groups);
black_box(s.iter().map(CpcSketch::estimate).sum::<f64>());
}
let start = Instant::now();
for _ in 0..128 {
let s = aggregate(black_box(&rows), groups);
black_box(s.iter().map(CpcSketch::estimate).sum::<f64>());
}
println!("{:.6}", start.elapsed().as_secs_f64() * 1000.0 / 128.0);
}
```
</details>
## Validation
`cargo x check`, `cargo x test`, `cargo x lint`, and `cargo x bench --
--test` passed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]