Skip to content

bench(mask): measure intersect_by_rank on rotating random masks - #10138

Merged
robert3005 merged 1 commit into
vortex-data:developfrom
mightsleep:ibr-bench
Oct 1, 2026
Merged

robert3005 merged 1 commit into
vortex-data:developfrom
mightsleep:ibr-bench

Conversation

@mightsleep

Copy link
Copy Markdown
Contributor

Summary

Adds random_rotating to the intersect_by_rank benchmarks: independent random bits, and a different pair of masks on every iteration.

The existing cases use periodic masks ((i * 7 + 13) % 1000) and one pair per benchmark. A branch predictor learns such a pair within a few iterations, so on real hardware they measure the learned state. On mixed masks the portable kernel spends most of its time in the mispredictions this hides: on Neoverse N2, 50/50 random masks cost 35.9 ns per word.

The new cases rotate through 8 pairs of 256K rows. That is 32K words of self, twice the repeated sequence Zen 5 learns in my measurements, in about 0.5 MiB, inside L2 on the walltime legs.

They carry #[cpu_features]. intersect_by_rank picks its kernel at runtime, so the x86 legs measure the BMI2 kernel and the NEON leg the portable one. This gives a walltime baseline for the portable kernel ahead of a follow-up that rewrites it.

The existing cases are unchanged, so their CodSpeed history is kept.

Checks

  • cargo clippy -p vortex-mask --benches -- -D warnings
  • cargo fmt -p vortex-mask --check
  • cargo bench -p vortex-mask --bench intersect_by_rank -- random_rotating on Zen 5 (BMI2 kernel): 14 to 41 µs per iteration.
  • Not run: the CodSpeed walltime legs themselves.

Signed-off-by: mightsleep <acannix@proton.me>
@robert3005

Copy link
Copy Markdown
Contributor

What's the follow up to these benchmarks? Generally we add them to prevent regressions in things we care about, you likely want to open the subsequent pr as well

@mightsleep

Copy link
Copy Markdown
Contributor Author

Yes hello,
follow-up is up: #10168. This bench is what the portable path on the neon leg shows. Maybe you like...

@robert3005 robert3005 added the changelog/chore A trivial change label Oct 1, 2026
@codspeed

codspeed Bot commented Oct 1, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 16.1%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚡ 1 improved benchmark
✅ 2061 untouched benchmarks
🆕 12 new benchmarks
⏩ 503 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ WallTime compare_u64_avx512 3.7 µs 3.2 µs +16.1%
🆕 WallTime random_rotating_avx512[(0.02, 0.5, "self_2pct_mask_50pct")] N/A 19.6 µs N/A
🆕 WallTime random_rotating_avx512[(0.5, 0.02, "self_50pct_mask_2pct")] N/A 60.8 µs N/A
🆕 WallTime random_rotating_avx512[(0.5, 0.5, "self_50pct_mask_50pct")] N/A 24.2 µs N/A
🆕 WallTime random_rotating_avx512[(0.9, 0.9, "self_90pct_mask_90pct")] N/A 26.9 µs N/A
🆕 WallTime random_rotating_avx2[(0.02, 0.5, "self_2pct_mask_50pct")] N/A 19.7 µs N/A
🆕 WallTime random_rotating_avx2[(0.5, 0.02, "self_50pct_mask_2pct")] N/A 61.4 µs N/A
🆕 WallTime random_rotating_avx2[(0.5, 0.5, "self_50pct_mask_50pct")] N/A 23.8 µs N/A
🆕 WallTime random_rotating_avx2[(0.9, 0.9, "self_90pct_mask_90pct")] N/A 26.6 µs N/A
🆕 WallTime random_rotating_neon[(0.02, 0.5, "self_2pct_mask_50pct")] N/A 44.1 µs N/A
🆕 WallTime random_rotating_neon[(0.5, 0.02, "self_50pct_mask_2pct")] N/A 86.4 µs N/A
🆕 WallTime random_rotating_neon[(0.5, 0.5, "self_50pct_mask_50pct")] N/A 183.7 µs N/A
🆕 WallTime random_rotating_neon[(0.9, 0.9, "self_90pct_mask_90pct")] N/A 293.2 µs N/A

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mightsleep:ibr-bench (d0612a7) with develop (0852739)

Open in CodSpeed

Footnotes

  1. 503 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@robert3005
robert3005 enabled auto-merge (squash) October 1, 2026 14:48
@robert3005
robert3005 merged commit 900c1f2 into vortex-data:develop Oct 1, 2026
115 of 117 checks passed
robert3005 pushed a commit that referenced this pull request Oct 12, 2026
…t_by_rank (#10168)

## Summary

Follow-up to #10138. Without BMI2, `intersect_by_rank` deposits the rank
bits with a loop over the set bits of each `self` chunk. That covers
every aarch64 build, plus x86 without BMI2. On masks that don't repeat,
the loop mispredicts about once per chunk and runs 3 to 8 times slower
than PDEP.

This PR uses SVE2 BDEP where the CPU has it and a branch-free deposit
elsewhere:

- **SVE2 BDEP** on Neoverse N2 and V2 (Graviton4, Cobalt 100, Axion),
detected at runtime. It is used for the deposit and for the select of
the mask-driven path, the same way BMI2 is used on x86. The kernels are
compiled with SVE2 enabled as a whole, so BDEP inlines into the loop.
Without that it costs a call and a register reload per chunk, which I
found in the assembly rather than in the design.
- **Portable network** otherwise. Masks with at most two set bits or at
most two clear bits get closed forms. For the rest, each byte takes its
source bits from the count of mask bits below it (one multiply sums
them), and inside each byte the compress network of Hacker's Delight 7-4
runs backwards. That is the `uN::extract_bits` scheme from core, with
three stages instead of six.
- **Loop below 1/16 density of `self`**, where a chunk has about four
set bits. The first version used the network everywhere and lost to the
old loop on every sparse bench, so the loop is back for sparse `self`,
chosen once per call, and now stops when the source bits run out.

The BMI2 path is unchanged.

Median µs before / after on Neoverse N2, and the speedup of the portable
path on Apple M1:

| | N2, BDEP | N2, portable | M1, portable |
|---|---|---|---|
| random, self 50 %, mask 50 % | 151 / 13.0 (11.7x) | 151 / 55.8 (2.7x)
| 3.8x to 4.6x |
| random, self 90 %, mask 90 % | 240 / 14.6 (16.5x) | 240 / 57.5 (4.2x)
| 5.4x to 6.2x |
| random, self 2 %, mask 50 % | 34.4 / 10.4 (3.3x) | 34.4 / 20.1 (1.7x)
| 1.7x to 2.4x |
| random, self 5 %, mask 50 % | 39.9 / 11.5 (3.5x) | 39.9 / 44.4 (0.90x)
| 0.79x to 0.94x |
| random, self 50 %, mask 2 % | 67.2 / 31.9 (2.1x) | 66.5 / 66.6 (1.0x)
| 0.9x to 1.16x |
| `density_matrix` dense | 24.8 / 4.6 (5.4x) | 24.8 / 19.8 (1.26x) |
1.5x to 1.9x |
| `density_matrix` self sparse | 7.3 / 3.9 (1.85x) | 7.3 / 7.8 (0.93x) |
0.92x to 1.19x |
| `intersect_by_rank` runs | 3.82 / 4.13 (0.92x) | 3.82 / 4.94 (0.77x) |
0.73x to 1.0x |
| `very_sparse_mask_cached` | 1.2x to 1.4x | unchanged | unchanged |

`random_rotating` is the bench from #10138, with extra densities added
for this measurement. The runners are GitHub's `ubuntu-24.04-arm`
(Cobalt 100) and `macos-15` (a virtualized M1), with two rounds per run.
For the portable column on N2 the portable path is forced. The M1
numbers are ranges over several runs, because a virtual M1 has opinions.
I also tried HD 7-5 through PMULL, on the theory that the Apple cores
love it. The M1 ran it at the speed of the network, so it isn't in this
PR.

Where it's slower:

- **Runs of 64 set and 64 clear bits:** 0.77x to 0.92x. Aligned blocks
of eight empty or full chunks skip the deposit, but the old per-chunk
shortcuts were cheaper.
- **The other benches' masks:** they come from `(i * 7 + 13) % 1024`,
repeat every 16 chunks, and the predictor learns the old loop on them.
With a sparse mask over a dense `self`, the portable path stays on the
network and runs 0.67x to 0.9x. With random masks at the same densities
it's 2.9x to 5.4x faster, so I didn't also pick the kernel by mask
density.
- **Random `self` at 5 %:** about 0.9x, just above the crossover. The
threshold is set for the M1, since N2 takes BDEP anyway.

---------

Signed-off-by: mightsleep <acannix@proton.me>
Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/chore A trivial change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants