From b37339052eedd9b993940488d10e11f90a43c5e4 Mon Sep 17 00:00:00 2001 From: Carson Ip Date: Tue, 25 Jul 2023 13:39:54 +0100 Subject: [PATCH] Nil out the slices to reduce alloc (#38) Enable backing arrays filled with nil to be reused by eventTo*Metrics --- aggregators/converter.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/aggregators/converter.go b/aggregators/converter.go index fb79ea4..3d7adc3 100644 --- a/aggregators/converter.go +++ b/aggregators/converter.go @@ -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() } @@ -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 + } +}