Repository navigation
Gallop when one array container is much smaller than the other (fixes #912) - #913
Merged
Merged
Conversation
Since 5.2.4, the scalar union, difference and symmetric difference of two array containers handle blocks of four values only while four values remain on each side, and step through everything else one branchless value at a time. When one side holds a few values, every value of the other side goes through that dependent chain, which made OR and ANDNOT of a tiny bitmap against one with thousands of values 3 to 5 times slower than in 5.2.3. When one side is more than 8 times shorter, union_uint16, difference_uint16 and xor_uint16 now locate each of its values in the long side by galloping and copy the values in between in bulk. Once fewer than four values remain on one side, the merges and the intersections do the same when many values remain on the other. On x64, union and xor skip the vectorized code when the sizes differ by more than 32 times, and the vectorized difference is skipped when one side has fewer than 8 values or is 128 times shorter. Adds microbenchmarks/array_merge_bench. Fixes #912
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #912.
Problem
Since 5.2.4 (#904), the scalar
union_uint16,difference_uint16andxor_uint16handle blocks of four values only while four values remain on each side. Everything else goes through the branchless one-value-at-a-time loop, whose load addresses depend on the previous comparison, so it costs several cycles per value. When one array container has a few values and the other has thousands, every value of the large side goes through that chain. OR and ANDNOT of a tiny bitmap against a 2000-value one became 3–5x slower than 5.2.3. On x64 the vectorized union and xor fall back to the scalar code below 8 values, so the regression shows up there as well, and in builds withROARING_DISABLE_X64.Fix
CROARING_SKEWED_MERGE_RATIO), the scalar union, difference and xor locate each value of the short side in the long side by galloping and copy the values in between in bulk.intersect_uint16,intersect_uint16_cardinality) gallop the same way when the other side still has many values. Otherwise they keep the existing branchless tail, which leaves the balanced case unchanged. Calling the gallop helper unconditionally there cost about 4% on balanced inputs with GCC.CROARING_SKEWED_VECTOR_MERGE_RATIO). ANDNOT skipsdifference_vector16when one side has fewer than 8 values (it does not vectorize there) or is 128 times shorter. That path was 0.7–1.7 µs for 1–3 values even in 5.2.3.bufferstartingsize_2values beforeset_1,a_out == a1for difference, andoutequal to either input for intersection.I picked the thresholds from a sweep on both machines.
Testing
ctestpasses on macOS/arm64 and on x64 (default and-DROARING_DISABLE_X64=ON).union_uint16,fast_union_uint16, in-place union,xor_uint16,difference_uint16in and out of place,intersect_uint16withoutequal to either input,intersect_uint16_cardinality). It used sizes 0–4096, uniform and clustered data, under ASan and UBSan, with the ratio forced to 1, 8 and 10⁶ so that every path is exercised.Benchmarks
New
microbenchmarks/array_merge_bench(Google Benchmark):Or,OrInplace(small ⊂ large, the scenario of the issue),AndnotLargeMinusSmall,AndnotSmallMinusLarge,XorandAnd. The small side has 1–128 values and the large side 256–3500, all in one 64K chunk. Balanced pairs guard the regular merge. Each benchmark cycles through 64 distinct random pairs so that the branch predictor does not learn one input.The reporter's
repro.con an Apple M-series laptop went from 1470 / 1500 / 1600 ns on master (520 / 390 / 620 on 5.2.3) to 93 / 97 / 95 ns foror_inplace/or/andnot.Selected rows, ns per operation (median of 3):
Geometric mean over the 140 cases:
-DROARING_DISABLE_X64=ONCases that do not take the new paths (balanced sizes, moderate skew) are unchanged within noise. The worst is +6 ns on
AndnotLargeMinusSmall/8/256with SIMD. One gap remains that this PR does not address: on Apple silicon, moderate skew (Or/500/2000,Or/128/256) is still about 2x slower than 5.2.3. There the old branchy merge beats both the branchless merge from #904 and galloping.Full results:
Apple M-series laptop, macOS, Apple clang
big4: Xeon Gold 6548N (Emerald Rapids), GCC 14.3, default build (AVX2/AVX-512)
big4, same machine, -DROARING_DISABLE_X64=ON (scalar code)