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

Improve zeroOrOneCharacterStringMatcher by using utf8.DecodeRuneInString #14090

Conversation

colega
Copy link
Contributor

@colega colega commented May 13, 2024

This replaces the previously introduced custom moreThanOneRune function by the standard utf8.DecodeRuneInString that returns the size of the first rune, which can be used to tell whether the string is longer than one rune.

goos: darwin
goarch: arm64
pkg: github.com/prometheus/prometheus/model/labels
                                   │     main     │                 new                 │
                                   │    sec/op    │   sec/op     vs base                │
ZeroOrOneCharacterStringMatcher-12   72.745n ± 3%   3.159n ± 0%  -95.66% (p=0.000 n=10)

This adds some more test cases for unicode values, and also a benchmark
for zeroOrOneCharacterStringMatcher.Matches()

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
This replaces the custom `moreThanOneRune` function with the standard
`utf8.DecodeRuneInString(s)` that can be used to figure out the size of
the first rune.

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
Copy link
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

Nice. What about the case of invalid UTF-8, where DecodeRuneInString returns (RuneError, 1) ?

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
@colega
Copy link
Contributor Author

colega commented May 13, 2024

Fair enough. Added some test cases for it and a special check.

Copy link
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

Thanks!

@bboreham bboreham merged commit 1e0b0e2 into prometheus:main May 16, 2024
25 checks passed
@colega colega deleted the improve-zeroOrOneCharacterStringMatcher-Matches branch May 16, 2024 08:47
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