Skip to content

Commit

Permalink
[chore] preallocate slice (open-telemetry#24940)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Aug 18, 2023
1 parent 267ce81 commit cb6ba56
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions exporter/prometheusremotewriteexporter/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ func batchTimeSeries(tsMap map[string]*prompb.TimeSeries, maxBatchByteSize int)
return nil, errors.New("invalid tsMap: cannot be empty map")
}

var requests []*prompb.WriteRequest
var tsArray []prompb.TimeSeries
requests := make([]*prompb.WriteRequest, 0, len(tsMap))
tsArray := make([]prompb.TimeSeries, 0, len(tsMap))
sizeOfCurrentBatch := 0

i := 0
for _, v := range tsMap {
sizeOfSeries := v.Size()

if sizeOfCurrentBatch+sizeOfSeries >= maxBatchByteSize {
wrapped := convertTimeseriesToRequest(tsArray)
requests = append(requests, wrapped)

tsArray = nil
tsArray = make([]prompb.TimeSeries, 0, len(tsMap)-i)
sizeOfCurrentBatch = 0
}

tsArray = append(tsArray, *v)
sizeOfCurrentBatch += sizeOfSeries
i++
}

if len(tsArray) != 0 {
Expand Down

0 comments on commit cb6ba56

Please sign in to comment.