Skip to content
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

Merged
merged 1 commit into from May 30, 2023

Conversation

YutaroHayakawa
Copy link
Member

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

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
slices: Introduce slices.UniqueFunc()

@YutaroHayakawa YutaroHayakawa added the release-note/misc This PR makes changes that have no direct user impact. label May 29, 2023
@YutaroHayakawa YutaroHayakawa marked this pull request as ready for review May 29, 2023 12:43
@YutaroHayakawa YutaroHayakawa requested a review from a team as a code owner May 29, 2023 12:43
@YutaroHayakawa
Copy link
Member Author

Let me ask for your review @pippolo84 since you are the original author of the Unique 🙏

@YutaroHayakawa
Copy link
Member Author

/test

Copy link
Member

@pippolo84 pippolo84 left a 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.

pkg/slices/slices_test.go Outdated Show resolved Hide resolved
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>
@YutaroHayakawa
Copy link
Member Author

/test

@YutaroHayakawa
Copy link
Member Author

ConformanceK8sKind: New flake. Filed an issue: #25758.

@YutaroHayakawa
Copy link
Member Author

ConformanceK8sKind: New flake. Filed an issue: #25759

@YutaroHayakawa
Copy link
Member Author

Now all green. Ready to merge.

@YutaroHayakawa YutaroHayakawa added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label May 30, 2023
@julianwiedmann julianwiedmann merged commit 6b84bef into cilium:main May 30, 2023
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants