Search for first duplicate in vectorized unique#5363
Merged
StephanTLavavej merged 3 commits intomicrosoft:mainfrom Apr 10, 2025
Merged
Search for first duplicate in vectorized unique#5363StephanTLavavej merged 3 commits intomicrosoft:mainfrom
unique#5363StephanTLavavej merged 3 commits intomicrosoft:mainfrom
Conversation
StephanTLavavej
approved these changes
Apr 1, 2025
Member
|
I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed. |
Member
🏇 🏎️ 🚀 |
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.
🔍 Missing piece
Vectorized
removehas carefully consideredfindbefore actual removal loop. But the similaradjacent_findwas 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. Butuniquehas 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
mainbefore #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.