Skip to content

Commit

Permalink
Nil out the slices to reduce alloc (#38)
Browse files Browse the repository at this point in the history
Enable backing arrays filled with nil to be reused by eventTo*Metrics
  • Loading branch information
carsonip authored Jul 25, 2023
1 parent f55d623 commit b373390
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions aggregators/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,10 @@ func (c combinedMetricsCollector) add(
to.SpanMetrics = mergeSlices[aggregationpb.KeyedSpanMetrics](
to.SpanMetrics, from.SpanMetrics,
)
from.ServiceTransactionMetrics = nil
from.TransactionMetrics = nil
from.SpanMetrics = nil
// nil out the slices to avoid ReturnToVTPool from releasing the underlying metrics in the slices
nilSlice(from.ServiceTransactionMetrics)
nilSlice(from.TransactionMetrics)
nilSlice(from.SpanMetrics)
from.ReturnToVTPool()
}

Expand All @@ -933,3 +934,9 @@ func mergeSlices[T any](to []*T, from []*T) []*T {
to = slices.Grow(to, len(from))
return append(to, from...)
}

func nilSlice[T any](s []*T) {
for i := 0; i < len(s); i++ {
s[i] = nil
}
}

0 comments on commit b373390

Please sign in to comment.