…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>
Summary
#[target_feature(enable = "bmi2")],"avx2"and"avx512f,..."do not imply POPCNT, socount_ones()inside these kernels compiles to a SWAR sequence (withimulfor narrow masks) in any build withouttarget-cpu. The bench runners usetarget-cpu=native, so they won't show a difference.Changes
Add
popcntto the target features and to the runtime detection of theintersect_by_rankBMI2 kernels and the x86simd_compresskernels. No other#[target_feature]function in the tree counts bits without it (thefearless_simdlevels already enable POPCNT).Default build, Zen 5, divan medians:
intersect_by_rank100k randomrandom_rotating0.5/0.5very_sparse_mask_cached10%/1%filter_fixed_widthrandom_i160.8filter_fixed_widthrandom_i320.5filter_fixed_widthrandom_i640.5filter_fixed_widthrandom_i1280.5 (no kernel)The filter numbers are the AVX-512 kernels; the AVX2 ones should show up in the CodSpeed simulation shard, where AFAIK valgrind has no AVX-512.