mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
fix(chunk): Copy chunk in a new slice (#648)
Prevents memory leak and free memory from initial collection.
This commit is contained in:
2
Makefile
2
Makefile
@@ -25,6 +25,8 @@ tools:
|
||||
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||
go get -t -u golang.org/x/tools/cmd/cover
|
||||
go install github.com/sonatype-nexus-community/nancy@latest
|
||||
go install golang.org/x/perf/cmd/benchstat@latest
|
||||
go install github.com/cespare/prettybench@latest
|
||||
go mod tidy
|
||||
|
||||
lint:
|
||||
|
6
slice.go
6
slice.go
@@ -221,7 +221,11 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
|
||||
if last > len(collection) {
|
||||
last = len(collection)
|
||||
}
|
||||
result = append(result, collection[i*size:last:last])
|
||||
|
||||
// Copy chunk in a new slice, to prevent memory leak and free memory from initial collection.
|
||||
newSlice := make(Slice, last-i*size)
|
||||
copy(newSlice, collection[i*size:last])
|
||||
result = append(result, newSlice)
|
||||
}
|
||||
|
||||
return result
|
||||
|
Reference in New Issue
Block a user