Search for first duplicate in vectorized unique
#5363
Merged
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.
🔍 Missing piece
Vectorized
remove
has carefully consideredfind
before actual removal loop. But the similaradjacent_find
was missed inunique
.The effect is at least performance. Find is faster as vectorization, and it also avoids extra writes. I doubt about whether they have correctness impact though.
I've added the missing piece in separately compiled code. This is not the same as in
remove
, where it is in the header. Butunique
has in-place search for duplicates in scalar part that is tightly coupled with the rest of it, so I thought it is better to avoid disrupting that.🏁 Benchmark
The modified benchmark adds two regions without duplicates: at the beginning and at the end of the range. The beginning one is avoidable with
adjacent_find
.I've ran the modified benchmark for the
main
before #5092, to have the updated non-vectorized baseline to compare against⏱️ Benchmark results
💡 Results interpretation
The effect is way more than expected. in particular, the "Before PR" column has shown that vectorization performance has severely degraded after adding already-unique ranges parts to the input.
Apparently it is due to reading just written output. Before the first removed element we read back last element previously written along with newly read elements. The mix of cache data and store buffer data causes the CPU to stall until the store buffer entry reaches the cache. So when we skip to the first non-unique, it is no longer a problem.