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

optimize to posting group label value matching #7343

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yeya24
Copy link
Contributor

@yeya24 yeya24 commented May 10, 2024

  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user.

Changes

The optimization comes from prometheus/prometheus#13958 and https://github.com/prometheus/prometheus/blob/main/tsdb/querier.go#L358.

With latest Prometheus regex matcher optimization, now empty value "" is optimized into emptyStringMatcher which only checks the value length https://github.com/prometheus/prometheus/blob/main/model/labels/regexp.go#L325.

// emptyStringMatcher matches an empty string.
type emptyStringMatcher struct{}

func (m emptyStringMatcher) Matches(s string) bool {
	return len(s) == 0
}

Since label values should be always non empty, we can optimize some cases to not run matching at all.

Verification

Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Ben Ye <benye@amazon.com>
@@ -2726,7 +2726,7 @@ func matchersToPostingGroups(ctx context.Context, lvalsFn func(name string) ([]s
// Merge PostingGroups with the same matcher into 1 to
// avoid fetching duplicate postings.
for _, val := range values {
pg, vals, err = toPostingGroup(ctx, lvalsFunc, val)
pg, vals, err = toPostingGroup(ctx, lvalsFunc, val, len(values) == 1)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to check for len(values) == 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the difference we have with prometheus/prometheus#13958 is that we cache results of label values. So we cannot reuse vals only if it is the last value we have. I have updated the condition.

Copy link
Contributor Author

@yeya24 yeya24 May 10, 2024

Choose a reason for hiding this comment

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

I am actually a little bit unsure now if it reuses the values array causes any issue or not.

Updated: OK I think it is not safe to reuse the values other than reusing it once at the last value.

yeya24 added 3 commits May 9, 2024 23:01
Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Ben Ye <benye@amazon.com>
Signed-off-by: Ben Ye <benye@amazon.com>
@yeya24 yeya24 requested a review from GiedriusS May 11, 2024 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants