Skip to content
Open
Show file tree
Hide file tree
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
64 changes: 64 additions & 0 deletions benchmark/core_slice_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,67 @@ func BenchmarkFromSlicePtrOr(b *testing.B) {
})
}
}

func BenchmarkChunkIntSmallDataSmallChunk(b *testing.B) {
data := genSliceInt(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 3)
}
}

func BenchmarkChunkIntSmallDataLargeChunk(b *testing.B) {
data := genSliceInt(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 50)
}
}

func BenchmarkChunkIntMediumDataSmallChunk(b *testing.B) {
data := genSliceInt(10_000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 7)
}
}

func BenchmarkChunkIntMediumDataLargeChunk(b *testing.B) {
data := genSliceInt(10_000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 1024)
}
}

func BenchmarkChunkIntLargeDataSmallChunk(b *testing.B) {
data := genSliceInt(1_000_000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 13)
}
}

func BenchmarkChunkIntLargeDataLargeChunk(b *testing.B) {
data := genSliceInt(1_000_000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 8192)
}
}

func BenchmarkChunkStringMediumDataSmallChunk(b *testing.B) {
data := genSliceString(10_000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 7)
}
}

func BenchmarkChunkStringMediumDataLargeChunk(b *testing.B) {
data := genSliceString(10_000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lo.Chunk(data, 1024)
}
}
35 changes: 22 additions & 13 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,39 @@ func GroupByMapErr[T any, K comparable, V any](collection []T, transform func(it
// Chunk returns a slice of elements split into groups of length size. If the slice can't be split evenly,
// the final chunk will be the remaining elements.
// Play: https://go.dev/play/p/kEMkFbdu85g
// Chunk returns a slice of elements split into groups of length size.
// If the slice can't be split evenly, the final chunk will be the remaining elements.
// Copy chunk in a new slice, to prevent memory leak and free memory from initial collection.
// Play: https://go.dev/play/p/kEMkFbdu85g
func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
if size <= 0 {
panic("lo.Chunk: size must be greater than 0")
}

chunksNum := len(collection) / size
if len(collection)%size != 0 {
chunksNum++
if len(collection) == 0 {
return []Slice{}
}

result := make([]Slice, 0, chunksNum)
// Reason: https://github.com/golang/go/issues/77287
clone := func(s Slice) Slice {
dst := make(Slice, len(s))
copy(dst, s)
return dst
}

for i := 0; i < chunksNum; i++ {
last := (i + 1) * size
if last > len(collection) {
last = len(collection)
}
chunksNum := (len(collection) + size - 1) / size
result := make([]Slice, chunksNum)

// 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)
// Process all full-sized chunks
i := 0
for ; i < chunksNum-1; i++ {
pos := i * size
result[i] = clone(collection[pos : pos+size])
}

// Handle the final chunk (may be smaller than size)
result[chunksNum-1] = clone(collection[i*size:])

return result
}

Expand Down