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

pkg: add slices and maps package #44981

Merged
merged 1 commit into from
May 24, 2023

Conversation

howardjohn
Copy link
Member

This PR introduces two new packages, slices and maps.

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:

  • Filter, Map, and Convert
  • Contains/Find instead of Index
  • Sort functions return the array for easier usage

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

@howardjohn howardjohn added the release-notes-none Indicates a PR that does not require release notes. label May 18, 2023
@howardjohn howardjohn requested review from a team as code owners May 18, 2023 17:18
@istio-testing istio-testing added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 18, 2023
@zirain
Copy link
Member

zirain commented May 18, 2023

there's one thing I want to ask long before:
will these package(include sets) move to istio/pkg?

@howardjohn
Copy link
Member Author

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.

@howardjohn howardjohn force-pushed the pkg/maps-slices branch 2 times, most recently from c5b160d to edefd1f Compare May 18, 2023 23:28
@jacob-delgado
Copy link
Contributor

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.

Copy link
Member

@hzxuzhonghu hzxuzhonghu left a 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

@howardjohn
Copy link
Member Author

howardjohn commented May 19, 2023 via email

@hzxuzhonghu
Copy link
Member

Mostly i think those new functions are not very widely used. If so, we can add to upstream.

@howardjohn
Copy link
Member Author

howardjohn commented May 19, 2023 via email

@howardjohn
Copy link
Member Author

@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 slices.Filter(deletedClusters, builtClusters.Contains), saving 7 LOC, more clear, and fewer allocations (filter in place instead of new slice)

}

// FindFunc finds the first element matching the function, or nil if none do
func FindFunc[E any](s []E, f func(E) bool) *E {
Copy link
Member

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.

Copy link
Member Author

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

// 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 {
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it

Copy link
Member

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

@howardjohn
Copy link
Member Author

Seems these wrapper are inspired by rustlang.

Filter on a slice is common in almost every language - rust, python, java, etc

@istio-testing istio-testing added the needs-rebase Indicates a PR needs to be rebased before being merged label May 22, 2023
@istio-testing istio-testing removed the needs-rebase Indicates a PR needs to be rebased before being merged label May 23, 2023
@howardjohn howardjohn requested a review from hzxuzhonghu May 23, 2023 15:28
@howardjohn
Copy link
Member Author

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


// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Used Filter to avoid mutation
// Use Filter to avoid mutation

@istio-testing istio-testing merged commit 9a9d1c8 into istio:master May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes-none Indicates a PR that does not require release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants