From 0400ff65480987aaa0ae8fefd20ce1a54de597e8 Mon Sep 17 00:00:00 2001 From: Matthias Loibl Date: Wed, 30 Nov 2022 16:11:18 +0100 Subject: [PATCH] pkg: Pass labelset as map[string]string for quicker access I stopped converting labels.Labels to map[string]string in the separateNameFromLabels. It seem like a good abstraction there still. --- pkg/parcacol/ingest.go | 7 ++++--- pkg/parcacol/ingest_test.go | 3 +-- pkg/parcacol/sample.go | 21 ++++++++------------- pkg/query/columnquery_test.go | 4 ++-- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/pkg/parcacol/ingest.go b/pkg/parcacol/ingest.go index eae93fffb50..94ee3363f81 100644 --- a/pkg/parcacol/ingest.go +++ b/pkg/parcacol/ingest.go @@ -85,6 +85,7 @@ func (ing Ingester) Ingest(ctx context.Context, ls labels.Labels, p *pprofproto. if err != nil { return fmt.Errorf("prepare labels: %w", err) } + lset := ls.Map() if err := validatePprofProfile(p); err != nil { return err @@ -101,7 +102,7 @@ func (ing Ingester) Ingest(ctx context.Context, ls labels.Labels, p *pprofproto. continue } - if err := ing.IngestProfile(ctx, ls, p); err != nil { + if err := ing.IngestProfile(ctx, lset, p); err != nil { return fmt.Errorf("ingest profile: %w", err) } } @@ -109,12 +110,12 @@ func (ing Ingester) Ingest(ctx context.Context, ls labels.Labels, p *pprofproto. return nil } -func (ing Ingester) IngestProfile(ctx context.Context, ls labels.Labels, p *profile.NormalizedProfile) error { +func (ing Ingester) IngestProfile(ctx context.Context, lset map[string]string, p *profile.NormalizedProfile) error { buf := ing.bufferPool.Get().(*bytes.Buffer) buf.Reset() defer ing.bufferPool.Put(buf) - err := NormalizedProfileToParquetBuffer(buf, ing.schema, ls, p) + err := NormalizedProfileToParquetBuffer(buf, ing.schema, lset, p) if err != nil { return fmt.Errorf("failed to convert samples to buffer: %w", err) } diff --git a/pkg/parcacol/ingest_test.go b/pkg/parcacol/ingest_test.go index da8f2790a9c..33dc9d918d1 100644 --- a/pkg/parcacol/ingest_test.go +++ b/pkg/parcacol/ingest_test.go @@ -25,7 +25,6 @@ import ( "github.com/go-kit/log" "github.com/polarsignals/frostdb/dynparquet" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/prometheus/model/labels" "github.com/segmentio/parquet-go" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/trace" @@ -72,7 +71,7 @@ func TestPprofToParquet(t *testing.T) { for i, np := range nps { buf := bytes.NewBuffer(nil) - require.NoError(t, NormalizedProfileToParquetBuffer(buf, schema, labels.Labels{}, np)) + require.NoError(t, NormalizedProfileToParquetBuffer(buf, schema, map[string]string{}, np)) serBuf, err := dynparquet.ReaderFromBytes(buf.Bytes()) require.NoError(t, err) diff --git a/pkg/parcacol/sample.go b/pkg/parcacol/sample.go index 19fa2b05af4..efc8ad319fc 100644 --- a/pkg/parcacol/sample.go +++ b/pkg/parcacol/sample.go @@ -19,16 +19,16 @@ import ( "sort" "github.com/polarsignals/frostdb/dynparquet" - "github.com/prometheus/prometheus/model/labels" "github.com/segmentio/parquet-go" + "golang.org/x/exp/maps" "github.com/parca-dev/parca/pkg/profile" ) // NormalizedProfileToParquetBuffer converts a normalized profile to a Parquet // buffer. The passed labels must be sorted. -func NormalizedProfileToParquetBuffer(w io.Writer, schema *dynparquet.Schema, ls labels.Labels, p *profile.NormalizedProfile) error { - names := labelNames(ls) +func NormalizedProfileToParquetBuffer(w io.Writer, schema *dynparquet.Schema, lset map[string]string, p *profile.NormalizedProfile) error { + names := labelNames(lset) pprofLabels := profileLabelNames(p) pprofNumLabels := profileNumLabelNames(p) @@ -50,7 +50,7 @@ func NormalizedProfileToParquetBuffer(w io.Writer, schema *dynparquet.Schema, ls names, pprofLabels, pprofNumLabels, - ls, + lset, p.Meta, sample, ) @@ -64,13 +64,9 @@ func NormalizedProfileToParquetBuffer(w io.Writer, schema *dynparquet.Schema, ls return schema.SerializeBuffer(w, pb.Buffer) } -func labelNames(ls labels.Labels) []string { - names := []string{} - - for _, label := range ls { - names = append(names, label.Name) - } - +func labelNames(ls map[string]string) []string { + names := maps.Keys(ls) + sort.Strings(names) return names } @@ -114,7 +110,7 @@ func SampleToParquetRow( schema *dynparquet.Schema, row parquet.Row, labelNames, profileLabelNames, profileNumLabelNames []string, - ls labels.Labels, + lset map[string]string, meta profile.Meta, s *profile.NormalizedSample, ) parquet.Row { @@ -157,7 +153,6 @@ func SampleToParquetRow( // All remaining cases take care of dynamic columns case ColumnLabels: - lset := ls.Map() // Convert labels to map for quicker lookup in loop. for _, name := range labelNames { if value, ok := lset[name]; ok { row = append(row, parquet.ValueOf(value).Level(0, 1, columnIndex)) diff --git a/pkg/query/columnquery_test.go b/pkg/query/columnquery_test.go index 2822063655e..dd345aa7a06 100644 --- a/pkg/query/columnquery_test.go +++ b/pkg/query/columnquery_test.go @@ -521,7 +521,7 @@ func TestColumnQueryAPIQueryDiff(t *testing.T) { err = ingester.IngestProfile( ctx, - labels.Labels{{Name: "job", Value: "default"}}, + labels.Labels{{Name: "job", Value: "default"}}.Map(), &profile.NormalizedProfile{ Meta: profile.Meta{ Name: "memory", @@ -539,7 +539,7 @@ func TestColumnQueryAPIQueryDiff(t *testing.T) { err = ingester.IngestProfile( ctx, - labels.Labels{{Name: "job", Value: "default"}}, + labels.Labels{{Name: "job", Value: "default"}}.Map(), &profile.NormalizedProfile{ Meta: profile.Meta{ Name: "memory",