-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
pkg: add slices and maps package #44981
Conversation
6d0b0c8
to
afc2e5d
Compare
there's one thing I want to ask long before: |
Istio/pkg was formed when we had plan to have multi-repo and needed common code. Given we have consolidated on a mostly monorepo I don't think it provides much value separated and makes it harder to maintain so I wouldn't be in favor. it's probably reasonable to fold the whole repo into Istio/Istio but not sure if everyone else is on board with that. |
c5b160d
to
edefd1f
Compare
I'd be in favor of moving istio/pkg to istio. There have been times when it has needed updates or has changed and has broken istio/istio. Minimizing the chances of this would be great. |
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.
Most functions are a wrapper and widely used. Only function like Filter
is added for special case.
Not sure whether it makes much sense
there is also sort returning the slice to avoid needing multiple lines,
filter, etc. We can add more as needed (I actually have multiple WIP PR
where I added another one to help the implementation), was just keeping
this PR pretty small
…On Thu, May 18, 2023, 6:38 PM Zhonghu Xu ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Most functions are a wrapper and widely used. Only function like Filter
is added for special case.
Not sure whether it makes much sense
—
Reply to this email directly, view it on GitHub
<#44981 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEYGXIZMZAM6QWEK5FX4WTXG3FJJANCNFSM6AAAAAAYGXK4MA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Mostly i think those new functions are not very widely used. If so, we can add to upstream. |
Upstream has explicitly said they do not want these functions. Standard
library is very conservative
…On Thu, May 18, 2023 at 11:36 PM Zhonghu Xu ***@***.***> wrote:
Mostly i think those new functions are not very widely used. If so, we can
add to upstream.
—
Reply to this email directly, view it on GitHub
<#44981 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEYGXKR7DB37JGBEFATOXTXG4IGPANCNFSM6AAAAAAYGXK4MA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@hzxuzhonghu here is an example of a PR adding low-performance code because we do not have these abstractions: https://github.com/istio/istio/pull/44891/files#diff-e3b667b806175fc66cb679217641e1638639507c364df2432a60c1d4e87a9739R150-R154 This could have been |
} | ||
|
||
// FindFunc finds the first element matching the function, or nil if none do | ||
func FindFunc[E any](s []E, f func(E) bool) *E { |
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.
What if it is a []*pointer, but with an elment == nil.
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.
In that case, E=*pointer
. So **E
is the response.
If we made it func FindFunc[E any](s []E, f func(E) bool) E
then you would not be able to distinguish in your case.
We could make it func FindFunc[E any](s []E, f func(E) bool) (E, bool)
but I think its easier to use as written here
pkg/slices/slices.go
Outdated
// Filter retains all elements in []E that f(E) returns true for. | ||
// The array is *mutated in place* and returned. | ||
// Used Filtered to avoid mutation | ||
func Filter[E any](s []E, f func(E) bool) []E { |
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.
It is kind of confusing with Filtered
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.
I renamed it
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.
Seems these wrapper are inspired by rustlang.
Filter on a slice is common in almost every language - rust, python, java, etc |
edefd1f
to
972fc1d
Compare
972fc1d
to
71dc22f
Compare
I have built #45058 on top of this PR, so it would be good to get direction one way or another on this. I can remove some functions if they are controversial |
71dc22f
to
2edd69b
Compare
|
||
// FilterInPlace retains all elements in []E that f(E) returns true for. | ||
// The array is *mutated in place* and returned. | ||
// Used Filter to avoid mutation |
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.
// Used Filter to avoid mutation | |
// Use Filter to avoid mutation |
2edd69b
to
126ac64
Compare
This PR introduces two new packages,
slices
andmaps
.The motivation behind this is that there are a variety of slice/map operations that are useful in Istio but not in exp/x/slices or exp/x/maps. We could just make a package only for those additions, but then we run into needing to differentiate like istioslices.Sort vs slices.Sort. So we import all the functions we need, and modify / add as needed.
Changes from upstream ones are:
The benefits of using these functions instead of upstream one is less boilerplate, clearer intent, and easier to optimize (for example, we do slow filtering in many places - easy to do the right thing when its just a function call away)
This PR moves a few call sites to the new package to show example usages, but it is intentionally not changing all of them since that would cause a lot of churn