Skip to content

Commit be45204

Browse files
authored
Refactor prepareBatches (#750)
Removes the unnecessary func and preallocates the slice.
1 parent 2e7f51b commit be45204

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

cmd/sync/aws.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,17 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
225225
}
226226

227227
func prepareBatches(maxItems int, items []string) [][]string {
228-
var batches [][]string
228+
totalBatches := (len(items) + maxItems - 1) / maxItems
229+
batches := make([][]string, 0, totalBatches)
229230

230-
min := func(a, b int) int {
231-
if a <= b {
232-
return a
231+
for i := 0; i < len(items); i += maxItems {
232+
end := i + maxItems
233+
if end > len(items) {
234+
end = len(items)
233235
}
234-
return b
236+
batches = append(batches, items[i:end])
235237
}
236238

237-
for i := 0; i < len(items); i += maxItems {
238-
batches = append(batches, items[i:min(i+maxItems, len(items))])
239-
}
240239
return batches
241240
}
242241

0 commit comments

Comments
 (0)