From f6655e24a65ffad54fbc2c1dafa928fec4937ecd Mon Sep 17 00:00:00 2001 From: Carson Ip Date: Thu, 13 Jul 2023 10:18:21 +0100 Subject: [PATCH] Reuse backing array of pb from vt pool (#18) --- aggregators/codec.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/aggregators/codec.go b/aggregators/codec.go index 6efc9de..c77d5c0 100644 --- a/aggregators/codec.go +++ b/aggregators/codec.go @@ -63,7 +63,9 @@ func (k *CombinedMetricsKey) SizeBinary() int { // ToProto converts CombinedMetrics to its protobuf representation. func (m *CombinedMetrics) ToProto() *aggregationpb.CombinedMetrics { pb := aggregationpb.CombinedMetricsFromVTPool() - pb.ServiceMetrics = make([]*aggregationpb.KeyedServiceMetrics, 0, len(m.Services)) + if len(m.Services) > cap(pb.ServiceMetrics) { + pb.ServiceMetrics = make([]*aggregationpb.KeyedServiceMetrics, 0, len(m.Services)) + } for k, m := range m.Services { ksm := aggregationpb.KeyedServiceMetricsFromVTPool() ksm.Key = k.ToProto() @@ -136,7 +138,9 @@ func (k *ServiceAggregationKey) FromProto(pb *aggregationpb.ServiceAggregationKe // ToProto converts ServiceMetrics to its protobuf representation. func (m *ServiceMetrics) ToProto() *aggregationpb.ServiceMetrics { pb := aggregationpb.ServiceMetricsFromVTPool() - pb.ServiceInstanceMetrics = make([]*aggregationpb.KeyedServiceInstanceMetrics, 0, len(m.ServiceInstanceGroups)) + if len(m.ServiceInstanceGroups) > cap(pb.ServiceInstanceMetrics) { + pb.ServiceInstanceMetrics = make([]*aggregationpb.KeyedServiceInstanceMetrics, 0, len(m.ServiceInstanceGroups)) + } for k, m := range m.ServiceInstanceGroups { ksim := aggregationpb.KeyedServiceInstanceMetricsFromVTPool() ksim.Key = k.ToProto() @@ -175,21 +179,27 @@ func (k *ServiceInstanceAggregationKey) FromProto(pb *aggregationpb.ServiceInsta // ToProto converts ServiceInstanceMetrics to its protobuf representation. func (m *ServiceInstanceMetrics) ToProto() *aggregationpb.ServiceInstanceMetrics { pb := aggregationpb.ServiceInstanceMetricsFromVTPool() - pb.TransactionMetrics = make([]*aggregationpb.KeyedTransactionMetrics, 0, len(m.TransactionGroups)) + if len(m.TransactionGroups) > cap(pb.TransactionMetrics) { + pb.TransactionMetrics = make([]*aggregationpb.KeyedTransactionMetrics, 0, len(m.TransactionGroups)) + } for k, m := range m.TransactionGroups { ktm := aggregationpb.KeyedTransactionMetricsFromVTPool() ktm.Key = k.ToProto() ktm.Metrics = m.ToProto() pb.TransactionMetrics = append(pb.TransactionMetrics, ktm) } - pb.ServiceTransactionMetrics = make([]*aggregationpb.KeyedServiceTransactionMetrics, 0, len(m.ServiceTransactionGroups)) + if len(m.ServiceTransactionGroups) > cap(pb.ServiceTransactionMetrics) { + pb.ServiceTransactionMetrics = make([]*aggregationpb.KeyedServiceTransactionMetrics, 0, len(m.ServiceTransactionGroups)) + } for k, m := range m.ServiceTransactionGroups { kstm := aggregationpb.KeyedServiceTransactionMetricsFromVTPool() kstm.Key = k.ToProto() kstm.Metrics = m.ToProto() pb.ServiceTransactionMetrics = append(pb.ServiceTransactionMetrics, kstm) } - pb.SpanMetrics = make([]*aggregationpb.KeyedSpanMetrics, 0, len(m.SpanGroups)) + if len(m.SpanGroups) > cap(pb.SpanMetrics) { + pb.SpanMetrics = make([]*aggregationpb.KeyedSpanMetrics, 0, len(m.SpanGroups)) + } for k, m := range m.SpanGroups { ksm := aggregationpb.KeyedSpanMetricsFromVTPool() ksm.Key = k.ToProto() @@ -367,8 +377,13 @@ func HistogramToProto(h *hdrhistogram.HistogramRepresentation) *aggregationpb.HD pb.LowestTrackableValue = h.LowestTrackableValue pb.HighestTrackableValue = h.HighestTrackableValue pb.SignificantFigures = h.SignificantFigures - pb.Buckets = make([]int32, 0, h.CountsRep.Len()) - pb.Counts = make([]int64, 0, h.CountsRep.Len()) + countsLen := h.CountsRep.Len() + if countsLen > cap(pb.Buckets) { + pb.Buckets = make([]int32, 0, countsLen) + } + if countsLen > cap(pb.Counts) { + pb.Counts = make([]int64, 0, countsLen) + } h.CountsRep.ForEach(func(bucket int32, value int64) { pb.Buckets = append(pb.Buckets, bucket) pb.Counts = append(pb.Counts, value)