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

slice.Remove #185

Merged
merged 1 commit into from
Dec 12, 2023
Merged

slice.Remove #185

merged 1 commit into from
Dec 12, 2023

Conversation

MatthewEdge
Copy link
Contributor

@MatthewEdge MatthewEdge commented Dec 4, 2023

  • Adds slice.Remove as a safer variant of the slices.Delete() function that avoids the panic case for invalid indecies

This function does fall within the warning about potential memory leaks if the slices modified are long-lived. See slices.Delete() docs for more info

Implementation Notes

  • Remove does use the same in-place approach go/x/exp uses to achieve the in-place update.

Benchmarking Checks

On an Intel Mac, to gut check the no-allocation claim:

goos: darwin
goarch: amd64
pkg: github.com/mailgun/holster/v4/slice
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRemove-12       	242117806	         4.944 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	223956164	         4.946 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	244528929	         4.919 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	244414112	         4.878 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	244394427	         5.028 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	229395693	         4.942 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	243575570	         4.853 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	244754611	         5.171 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	234260016	         5.056 ns/op	       0 B/op	       0 allocs/op
BenchmarkRemove-12       	246132768	         4.855 ns/op	       0 B/op	       0 allocs/op

@MatthewEdge MatthewEdge closed this Dec 8, 2023
@MatthewEdge MatthewEdge changed the title slice.Remove and slice.RemoveAll slice.Remove Dec 11, 2023
@MatthewEdge MatthewEdge reopened this Dec 11, 2023
slice/remove.go Show resolved Hide resolved
slice/remove.go Outdated
package slice

// Remove removes the given index range from the given slice, preserving element order.
// It is the safer version of go/x/exp/slices.Delete() as it will
Copy link
Contributor

Choose a reason for hiding this comment

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

It's just slices package already - https://pkg.go.dev/slices#Delete

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With one key difference: Delete panics if s[i:j] is not a valid slice of s <- this function does not panic. That's what I was trying to get across with this comment. That snippet is pulled from that link btw

Copy link
Contributor

Choose a reason for hiding this comment

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

I just meant that go/x/exp/slices and build-in slices are different packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhh I see. I misread your comment, apologies!

@MatthewEdge MatthewEdge merged commit d6898a2 into master Dec 12, 2023
4 checks passed
@MatthewEdge MatthewEdge deleted the medge/slice-remove branch December 12, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants