Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/profilestore: Reduce allocs in writeSeries #2176

Merged
merged 2 commits into from
Nov 29, 2022

Conversation

marselester
Copy link
Contributor

Potentially it's possible to reduce allocs in ProfileColumnStore.writeSeries if it's possible to reuse a slice of labels (I followed the call stack and didn't find any concurrent access, but I might be missing something).

# new
BenchmarkProfileColumnStoreWriteSeries-12  5725592  197.7 ns/op  120 B/op  5 allocs/op
# old
BenchmarkProfileColumnStoreWriteSeries-12  4249389  259.8 ns/op  184 B/op  7 allocs/op
new
(pprof) list writeS
Total: 1.26GB
ROUTINE ======================== github.com/parca-dev/parca/pkg/profilestore.(*ProfileColumnStore).writeSeries
  655.02MB   762.02MB (flat, cum) 59.16% of Total
         .          .    105:}
         .          .    106:
         .          .    107:func (s *ProfileColumnStore) writeSeries(ctx context.Context, req *profilestorepb.WriteRawRequest) error {
         .          .    108:	ingester := parcacol.NewIngester(
         .          .    109:		s.logger,
         .      107MB    110:		parcacol.NewNormalizer(s.metastore),
         .          .    111:		s.table,
         .          .    112:		s.schema,
         .          .    113:		s.bufferPool,
         .          .    114:	)
         .          .    115:
         .          .    116:	ls := make(labels.Labels, 0)
         .          .    117:	for _, series := range req.Series {
         .          .    118:		ls = ls[:0]
         .          .    119:		for _, l := range series.Labels.Labels {
         .          .    120:			if valid := model.LabelName(l.Name).IsValid(); !valid {
         .          .    121:				return status.Errorf(codes.InvalidArgument, "invalid label name: %v", l.Name)
         .          .    122:			}
         .          .    123:
  215.51MB   215.51MB    124:			ls = append(ls, labels.Label{
         .          .    125:				Name:  l.Name,
         .          .    126:				Value: l.Value,
         .          .    127:			})
         .          .    128:		}
         .          .    129:
         .          .    130:		// Must ensure label-set is sorted and HasDuplicateLabelNames also required a sorted label-set
  439.51MB   439.51MB    131:		sort.Sort(ls)
         .          .    132:		if name, has := ls.HasDuplicateLabelNames(); has {
         .          .    133:			return status.Errorf(codes.InvalidArgument, "duplicate label names: %v", name)
         .          .    134:		}
         .          .    135:
         .          .    136:		for _, sample := range series.Samples {
old
(pprof) list writeS
Total: 1.43GB
ROUTINE ======================== github.com/parca-dev/parca/pkg/profilestore.(*ProfileColumnStore).writeSeries
  859.52MB   941.52MB (flat, cum) 64.23% of Total
         .          .    105:}
         .          .    106:
         .          .    107:func (s *ProfileColumnStore) writeSeries(ctx context.Context, req *profilestorepb.WriteRawRequest) error {
         .          .    108:	ingester := parcacol.NewIngester(
         .          .    109:		s.logger,
         .       82MB    110:		parcacol.NewNormalizer(s.metastore),
         .          .    111:		s.table,
         .          .    112:		s.schema,
         .          .    113:		s.bufferPool,
         .          .    114:	)
         .          .    115:
         .          .    116:	for _, series := range req.Series {
  508.02MB   508.02MB    117:		ls := make(labels.Labels, 0, len(series.Labels.Labels))
         .          .    118:		for _, l := range series.Labels.Labels {
         .          .    119:			if valid := model.LabelName(l.Name).IsValid(); !valid {
         .          .    120:				return status.Errorf(codes.InvalidArgument, "invalid label name: %v", l.Name)
         .          .    121:			}
         .          .    122:
         .          .    123:			ls = append(ls, labels.Label{
         .          .    124:				Name:  l.Name,
         .          .    125:				Value: l.Value,
         .          .    126:			})
         .          .    127:		}
         .          .    128:
         .          .    129:		// Must ensure label-set is sorted and HasDuplicateLabelNames also required a sorted label-set
  351.51MB   351.51MB    130:		sort.Sort(ls)
         .          .    131:		if name, has := ls.HasDuplicateLabelNames(); has {
         .          .    132:			return status.Errorf(codes.InvalidArgument, "duplicate label names: %v", name)
         .          .    133:		}
         .          .    134:
         .          .    135:		for _, sample := range series.Samples {

@marselester marselester requested a review from a team as a code owner November 24, 2022 20:57
Copy link
Member

@brancz brancz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find! lgtm

@brancz brancz merged commit e886e05 into parca-dev:main Nov 29, 2022
fabxc pushed a commit to fabxc/parca that referenced this pull request Dec 5, 2022
* Add writeSeries benchmark

* Reduce labels slice to reduce allocs in writeSeries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants