Skip to content

perf(mask): SVE2 BDEP and a branch-free portable deposit for intersect_by_rank - #10168

Merged
robert3005 merged 5 commits into
vortex-data:developfrom
mightsleep:ibr-portable
Oct 12, 2026
Merged

robert3005 merged 5 commits into
vortex-data:developfrom
mightsleep:ibr-portable

Conversation

@mightsleep

Copy link
Copy Markdown
Contributor

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.

CodSpeed's neon leg runs on Graviton3, which has SVE but not SVE2, so it will only show the portable path.

Written with the help of AI. I reviewed the code and ran the measurements.

@mightsleep mightsleep changed the title perf(mask): SVE2 BDEP and a branch-free portable deposit for intersect by rank perf(mask): SVE2 BDEP and a branch-free portable deposit for intersect_by_rank Sep 30, 2026
…t_by_rank

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

Copy link
Copy Markdown
Contributor

can you split this into its own sve mod in a new file

@codspeed

codspeed Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 2 benchmarks

⚠️ 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 benchmark measured no execution time

Nothing ran under measurement, usually because the compiler removed the code under test. This result is not comparable, so it counts as unchanged.

Preventing compiler optimizations

⚠️ 10 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 24 improved benchmarks
❌ 2 regressed benchmarks
✅ 2175 untouched benchmarks
🆕 6 new benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ WallTime bitpack_blocked_compress_avx2 6.7 µs 7.7 µs -12.85%
❌ WallTime starts_with_avx512[64, VarBin] 54.4 µs 62 µs -12.36%
⚡ WallTime random_rotating_neon[(0.9, 0.9, "self_90pct_mask_90pct")] 291.9 µs 54.1 µs ×5.4
⚡ WallTime random_rotating_neon[(0.5, 0.5, "self_50pct_mask_50pct")] 183.6 µs 52.8 µs ×3.5
⚡ WallTime random_rotating_neon[(0.02, 0.5, "self_2pct_mask_50pct")] 44.3 µs 24.8 µs +78.56%
⚡ Simulation density_matrix[(0.05, 0.5, "self_sparse_mask_dense")] 86.8 µs 62.8 µs +38.23%
⚡ Simulation rank_indices[(0.05, 0.5, "self_sparse_rank_dense")] 86.9 µs 62.9 µs +38.2%
⚡ Simulation sparse[(100000, 0.05, "sparse_5pct")] 86.7 µs 62.8 µs +38.17%
⚡ Simulation density_matrix[(0.05, 0.05, "self_sparse_mask_sparse")] 86.8 µs 63 µs +37.7%
⚡ Simulation sparse[(100000, 0.1, "sparse_10pct")] 88 µs 63.9 µs +37.62%
⚡ Simulation rank_indices[(0.05, 0.05, "self_sparse_rank_sparse")] 82.2 µs 60.2 µs +36.62%
⚡ Simulation intersect_by_rank[(100000, "random")] 97.7 µs 72.2 µs +35.35%
⚡ Simulation intersect_by_rank[(100000, "runs")] 97.7 µs 72.2 µs +35.32%
⚡ Simulation density_matrix[(0.5, 0.5, "self_dense_mask_dense")] 97.6 µs 72.2 µs +35.22%
⚡ Simulation density_matrix[(0.5, 0.05, "self_dense_mask_sparse")] 97.6 µs 72.2 µs +35.19%
⚡ Simulation sparse[(100000, 0.5, "dense_50pct")] 97.7 µs 72.3 µs +35.1%
⚡ Simulation rank_indices[(0.5, 0.5, "self_dense_rank_dense")] 97.9 µs 72.5 µs +35.08%
⚡ Simulation rank_indices[(0.5, 0.05, "self_dense_rank_sparse")] 114 µs 92.4 µs +23.42%
⚡ WallTime random_rotating_avx512[(0.5, 0.5, "self_50pct_mask_50pct")] 24.1 µs 19.6 µs +22.88%
⚡ WallTime random_rotating_avx512[(0.02, 0.5, "self_2pct_mask_50pct")] 19.8 µs 16.1 µs +22.48%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mightsleep:ibr-portable (9b9ee64) with develop (ea4ba3a)

Open in CodSpeed

@joseph-isaacs joseph-isaacs added the changelog/performance A performance improvement label Oct 1, 2026
Signed-off-by: mightsleep <acannix@proton.me>
@mightsleep

Copy link
Copy Markdown
Contributor Author

yes hello
all done

let self_count = self_chunk.count_ones() as usize;
let rank_bits = reader.read(self_count);
push_result_chunk::<D>(&mut result, self_chunk, self_count, rank_bits);
let trivial = D::SKIP_TRIVIAL_RUNS && runs.next_is_trivial();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure how much benchmarking you have done but looking at instruction count it seems that this parameter is more overhead than it saves

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.

Weird.. with the portable kernel forced on Zen 5: on nightly 1.101 the lookahead buys ~6% on runs and costs ~9% on random. On 1.98 it mostly just costs, runs within 0-3%, random -9 to -10%. Removed. Interesting enough that I'd like to look at it properly, just not in this PR.

0..=2 => deposit_two_lowest(source, mask),
62.. => deposit_two_holes(source, mask),
_ => deposit_bytewise(source, mask),
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This function grew and lost it's inlining. I think we can fix the calling loop to have hot and cold branches? Did you check whether inlining of this function matters?

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.

My bad, and it goes further than this PR. In the bench profile Portable::deposit_bits was a call per word. The BMI2 kernel has the same problem, and that one is already on develop: pdep_bmi2 is #[target_feature(enable = "bmi2")] and the generic loop isn't, so LLVM never inlines it. On develop (3750d50, 1.98) the loop makes an indirect call per word to a five-instruction push rbp; pdep; pop rbp; ret. The CpuKernel docs cite this code as an example to follow...

Fixed with the pattern from bit/pack.rs and the bool filter: a #[target_feature] wrapper around an #[inline(always)] kernel body. Applied to develop alone (+63/-7, BMI2 only) it gives 1.22-1.31x on the chunk paths, 1.52x on sparse/sparse rank indices and 1.0-1.1x on mask-driven, with no regressions (Zen 5, 3 interleaved rounds). In this PR the same treatment covers SVE2, and the portable deposit inlines too (1.13-1.18x).

I can send the BMI2 part as a separate small PR so it lands on its own and this one rebases on it, or keep everything here. Your call.

mightsleep and others added 3 commits October 2, 2026 15:52
Signed-off-by: mightsleep <acannix@proton.me>
…hreshold in benches

Assert in debug builds that mask_count equals mask.count_ones(), because the
portable deposits pick a closed form from it. Inline bdep_sve2 like pdep_bmi2.
Add 5 % and 10 % self densities to random_rotating, so the
PORTABLE_LOOP_MAX_DENSITY crossover can be measured, and update the bench doc.

Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Measured on Graviton3 (Neoverse V1) and Graviton2 (Neoverse N1) with random
masks: the network beats the loop from about 3.5 % self density on V1, but
not below 8 % on N1. 1/16 is close to where the two balance, so keep it.

Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@robert3005 robert3005 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks for the contribution, I have fixed up minor nits

@robert3005
robert3005 enabled auto-merge (squash) October 12, 2026 12:26
@robert3005
robert3005 merged commit 9d5a844 into vortex-data:develop Oct 12, 2026
89 of 90 checks passed
robert3005 pushed a commit that referenced this pull request Oct 14, 2026
…10480)

## Summary

Follow-up to #10168 and #10456. `RankBitReader` pulled the rank bits
through a two-word window over `BitChunkIterator` and refilled it behind
a branch on every 64 bits consumed. With random masks that branch
mispredicts often.

The kernel already counts the set bits of each `self` chunk, so the rank
offset of the next chunk is known without any reader state. `RankBits`
reads 16 bytes at that offset and shifts by `offset % 8`, which covers
any 64-bit read. There is one bounds check per read and no iterator, so
`RankBitReader` and `low_bits` are gone. This applies to every deposit
(BMI2, SVE2 BDEP, portable), not just one path.

Reads near the end of the buffer come from a copy of its last 16 bytes
followed by zeros, made once per call. With a sparse `self` the last 128
rank bits cover many chunks: at 1/64 density that is about 120 of them.
A first version copied the tail on each such read in a `#[cold]`
function, and the call made the whole loop spill to the stack, so sparse
masks ran 5 % slower than before. `cold_path()` keeps the branch:
without it LLVM picks the address with three `cmov`s, which puts the
comparisons in front of every load and costs dense masks 8 %.

## Numbers

Zen 5, default build, `perf stat` over the kernel only: each bench runs
with `--sample-count 100` at `--sample-size 200` and `400`, and the
difference between the two runs is the cost of 100 x 200 calls with the
setup subtracted out. Wall-clock medians on this machine moved by up to
1.4x between processes of the same binary, so I'm not quoting them.

| | before | after | |
|---|---|---|---|
| `random_rotating`, cycles | 4894 M | 4256 M | 1.15x |
| `random_rotating`, instructions | 17.9 G | 15.8 G | -12 % |
| `sparse`, cycles | 3184 M | 3110 M | 1.02x |

`random_rotating` and `sparse` are all their cases together, weighted by
time.

## Testing

`kernels_match_reference` now also slices the rank buffer at a bit
offset (`past_a_word`, 67) so that a read starting past the first word
is covered. Dropping `start` from `RankBits::read` fails four tests,
ignoring `tail_start` fails five.

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

Labels

changelog/performance A performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants