Skip to content

Commit

Permalink
Merge pull request #305 from go-faster/perf/optimize-attributes
Browse files Browse the repository at this point in the history
perf(chstorage): do not make an unnecessary copy of encoded attributes
  • Loading branch information
ernado authored Jan 21, 2024
2 parents 8463a94 + d06609c commit b411a2a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
9 changes: 8 additions & 1 deletion internal/chstorage/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ClickHouse/ch-go/proto"
"github.com/go-faster/errors"
"github.com/go-faster/jx"
"go.opentelemetry.io/collector/pdata/pcommon"
"golang.org/x/exp/maps"

Expand Down Expand Up @@ -68,7 +69,13 @@ func (a *attributeCol) Append(v otelstorage.Attrs) {
if !ok {
idx = len(a.hashes)
a.hashes[h] = idx
a.index.Append(encodeAttributes(v.AsMap()))

e := jx.GetEncoder()
defer jx.PutEncoder(e)
encodeMap(e, v.AsMap())

// Append will copy passed bytes.
a.index.Append(e.Bytes())
}
a.col.AppendKey(idx)
}
Expand Down
14 changes: 0 additions & 14 deletions internal/chstorage/attributes_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

type lazyAttributes struct {
orig pcommon.Map

encoded []byte
}

func (l *lazyAttributes) Attributes(additional ...[2]string) otelstorage.Attrs {
Expand All @@ -25,18 +23,6 @@ func (l *lazyAttributes) Attributes(additional ...[2]string) otelstorage.Attrs {
return otelstorage.Attrs(l.orig)
}

func (l *lazyAttributes) Encode(additional ...[2]string) []byte {
switch {
case len(additional) > 0:
return encodeAttributes(l.orig, additional...)
case len(l.encoded) > 0:
return l.encoded
default:
l.encoded = encodeAttributes(l.orig)
return l.encoded
}
}

func encodeAttributes(attrs pcommon.Map, additional ...[2]string) []byte {
e := &jx.Encoder{}
encodeMap(e, attrs, additional...)
Expand Down
28 changes: 14 additions & 14 deletions internal/chstorage/inserter_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ func (b *metricsBatch) Insert(ctx context.Context, tables Tables, client Clickho
return nil
}

func (b *metricsBatch) addPoints(name string, res *lazyAttributes, slice pmetric.NumberDataPointSlice) error {
func (b *metricsBatch) addPoints(name string, res lazyAttributes, slice pmetric.NumberDataPointSlice) error {
c := b.points

for i := 0; i < slice.Len(); i++ {
point := slice.At(i)
ts := point.Timestamp().AsTime()
flags := point.Flags()
attrs := &lazyAttributes{
attrs := lazyAttributes{
orig: point.Attributes(),
}

Expand Down Expand Up @@ -223,12 +223,12 @@ func (b *metricsBatch) addPoints(name string, res *lazyAttributes, slice pmetric
return nil
}

func (b *metricsBatch) addHistogramPoints(name string, res *lazyAttributes, slice pmetric.HistogramDataPointSlice) error {
func (b *metricsBatch) addHistogramPoints(name string, res lazyAttributes, slice pmetric.HistogramDataPointSlice) error {
for i := 0; i < slice.Len(); i++ {
point := slice.At(i)
ts := point.Timestamp().AsTime()
flags := point.Flags()
attrs := &lazyAttributes{
attrs := lazyAttributes{
orig: point.Attributes(),
}
count := point.Count()
Expand Down Expand Up @@ -392,7 +392,7 @@ type histogramBucketBounds struct {
bucketKey [2]string
}

func (b *metricsBatch) addExpHistogramPoints(name string, res *lazyAttributes, slice pmetric.ExponentialHistogramDataPointSlice) error {
func (b *metricsBatch) addExpHistogramPoints(name string, res lazyAttributes, slice pmetric.ExponentialHistogramDataPointSlice) error {
var (
c = b.expHistograms
mapBuckets = func(b pmetric.ExponentialHistogramDataPointBuckets) (offset int32, counts []uint64) {
Expand All @@ -406,7 +406,7 @@ func (b *metricsBatch) addExpHistogramPoints(name string, res *lazyAttributes, s
point = slice.At(i)
ts = point.Timestamp().AsTime()
flags = point.Flags()
attrs = &lazyAttributes{orig: point.Attributes()}
attrs = lazyAttributes{orig: point.Attributes()}
count = point.Count()
sum = proto.Nullable[float64]{Set: point.HasSum(), Value: point.Sum()}
vmin = proto.Nullable[float64]{Set: point.HasMin(), Value: point.Min()}
Expand Down Expand Up @@ -451,13 +451,13 @@ func (b *metricsBatch) addExpHistogramPoints(name string, res *lazyAttributes, s
return nil
}

func (b *metricsBatch) addSummaryPoints(name string, res *lazyAttributes, slice pmetric.SummaryDataPointSlice) error {
func (b *metricsBatch) addSummaryPoints(name string, res lazyAttributes, slice pmetric.SummaryDataPointSlice) error {
for i := 0; i < slice.Len(); i++ {
var (
point = slice.At(i)
ts = point.Timestamp().AsTime()
flags = point.Flags()
attrs = &lazyAttributes{orig: point.Attributes()}
attrs = lazyAttributes{orig: point.Attributes()}
count = point.Count()
sum = point.Sum()
qv = point.QuantileValues()
Expand Down Expand Up @@ -502,8 +502,8 @@ func (b *metricsBatch) addSummaryPoints(name string, res *lazyAttributes, slice
type mappedSeries struct {
Timestamp time.Time
Flags pmetric.DataPointFlags
Attributes *lazyAttributes
Resource *lazyAttributes
Attributes lazyAttributes
Resource lazyAttributes
}

func (b *metricsBatch) addMappedSample(
Expand All @@ -528,8 +528,8 @@ func (b *metricsBatch) addMappedSample(
type exemplarSeries struct {
Name string
Timestamp time.Time
Attributes *lazyAttributes
Resource *lazyAttributes
Attributes lazyAttributes
Resource lazyAttributes
}

func (b *metricsBatch) addExemplars(p exemplarSeries, exemplars pmetric.ExemplarSlice) error {
Expand Down Expand Up @@ -577,7 +577,7 @@ func (b *metricsBatch) addName(name string) {
b.labels[[2]string{labels.MetricName, name}] = struct{}{}
}

func (b *metricsBatch) addLabels(attrs *lazyAttributes) {
func (b *metricsBatch) addLabels(attrs lazyAttributes) {
attrs.orig.Range(func(key string, value pcommon.Value) bool {
pair := [2]string{
key,
Expand All @@ -593,7 +593,7 @@ func (b *metricsBatch) mapMetrics(metrics pmetric.Metrics) error {
resMetrics := metrics.ResourceMetrics()
for i := 0; i < resMetrics.Len(); i++ {
resMetric := resMetrics.At(i)
resAttrs := &lazyAttributes{
resAttrs := lazyAttributes{
orig: resMetric.Resource().Attributes(),
}
b.addLabels(resAttrs)
Expand Down

0 comments on commit b411a2a

Please sign in to comment.