New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slices: Introduce slices.UniqueFunc() #25743
Conversation
Let me ask for your review @pippolo84 since you are the original author of the |
/test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I've just left inline a non-blocking comment regarding the benchmark.
Add a new function UniqueFunc() that deduplicates given slice like Unique(), but users can pass function to extract comparable key to compare the elements. We didn't applied the optimization applied for Unique in UniqueFunc, because when we measure the performance, a simple map-based deduplication showed a good performance in terms of the deduplication speed (but has extra allocation). Unique ``` cpu: 11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz BenchmarkUnique BenchmarkUnique/96 BenchmarkUnique/96-8 576111 1877 ns/op 0 B/op 0 allocs/op BenchmarkUnique/128 BenchmarkUnique/128-8 376249 3162 ns/op 0 B/op 0 allocs/op BenchmarkUnique/160 BenchmarkUnique/160-8 267973 4758 ns/op 0 B/op 0 allocs/op BenchmarkUnique/192 BenchmarkUnique/192-8 205845 5518 ns/op 3134 B/op 4 allocs/op BenchmarkUnique/256 BenchmarkUnique/256-8 165156 6981 ns/op 6177 B/op 2 allocs/op BenchmarkUnique/512 BenchmarkUnique/512-8 89644 13566 ns/op 10927 B/op 3 allocs/op BenchmarkUnique/1024 BenchmarkUnique/1024-8 46180 26177 ns/op 21819 B/op 4 allocs/op ``` UniqueFunc with the Unique-like optimization (compare elements like `key(i) == key(j)`) ``` cpu: 11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz BenchmarkUniqueFunc BenchmarkUniqueFunc/96 BenchmarkUniqueFunc/96-8 109359 11018 ns/op 0 B/op 0 allocs/op BenchmarkUniqueFunc/128 BenchmarkUniqueFunc/128-8 63355 17134 ns/op 0 B/op 0 allocs/op BenchmarkUniqueFunc/160 BenchmarkUniqueFunc/160-8 42169 26700 ns/op 0 B/op 0 allocs/op BenchmarkUniqueFunc/192 BenchmarkUniqueFunc/192-8 192640 6253 ns/op 3143 B/op 4 allocs/op BenchmarkUniqueFunc/256 BenchmarkUniqueFunc/256-8 150264 7652 ns/op 6176 B/op 2 allocs/op BenchmarkUniqueFunc/512 BenchmarkUniqueFunc/512-8 79173 15185 ns/op 10927 B/op 3 allocs/op BenchmarkUniqueFunc/1024 BenchmarkUniqueFunc/1024-8 41577 29245 ns/op 21821 B/op 4 allocs/op ``` UniqueFunc with simple map-based deduplication ``` cpu: 11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz BenchmarkUniqueFunc BenchmarkUniqueFunc/96 BenchmarkUniqueFunc/96-8 393656 3258 ns/op 1474 B/op 3 allocs/op BenchmarkUniqueFunc/128 BenchmarkUniqueFunc/128-8 272712 4091 ns/op 3099 B/op 2 allocs/op BenchmarkUniqueFunc/160 BenchmarkUniqueFunc/160-8 232684 5242 ns/op 3118 B/op 3 allocs/op BenchmarkUniqueFunc/192 BenchmarkUniqueFunc/192-8 191180 6159 ns/op 3132 B/op 4 allocs/op BenchmarkUniqueFunc/256 BenchmarkUniqueFunc/256-8 148922 7928 ns/op 6179 B/op 2 allocs/op BenchmarkUniqueFunc/512 BenchmarkUniqueFunc/512-8 78356 15101 ns/op 10923 B/op 3 allocs/op BenchmarkUniqueFunc/1024 BenchmarkUniqueFunc/1024-8 41091 29386 ns/op 21821 B/op 4 allocs/op ``` Signed-off-by: Yutaro Hayakawa <yutaro.hayakawa@isovalent.com>
f055145
to
ae50feb
Compare
/test |
ConformanceK8sKind: New flake. Filed an issue: #25758. |
ConformanceK8sKind: New flake. Filed an issue: #25759 |
Now all green. Ready to merge. |
This commit is extracted from #25477 since it is an independent change.
Add a new function UniqueFunc() that deduplicates given slice like Unique(), but users can pass function to extract comparable key to compare the elements.
We didn't applied the optimization applied for Unique in UniqueFunc, because when we measure the performance, a simple map-based deduplication showed a good performance in terms of the deduplication speed (but has extra allocation).
Unique
UniqueFunc with the Unique-like optimization (compare elements like
key(i) == key(j)
)UniqueFunc with simple map-based deduplication