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

Fix leaking slice capacity #360

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func Reslice[Slice ~[]model, model any](slice Slice, n int) Slice {
if n > cap(slice) {
slice = slices.Grow(slice, n-len(slice))
}
return slice[:n]
res := make(Slice, n)
copy(res, slice[:n])
return res
Comment on lines +30 to +32
Copy link
Contributor

Choose a reason for hiding this comment

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

This can create multiple allocations of slice (one in Grow and the other one which we are doing here). IIANM, the whole point of this function was to reduce allocation and GC (even when the capacity is higher) so I don't think this change is needed. OTOH, now that we are moving away from pooling, we might not even require Reslice but that would be a bigger change overall.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I see. Thanks. I'll close this.

}

// ResliceAndPopulateNil ensures a slice of pointers has atleast
Expand Down
Loading