Avoid reallocating map in MatchLabels#10715
Merged
roidelapluie merged 1 commit intoprometheus:mainfrom May 20, 2022
Merged
Conversation
We know the max size of our map so we can create it with that information and avoid extra allocations Signed-off-by: Łukasz Mierzwa <l.mierzwa@gmail.com>
👷 Deploy request for prometheus-react accepted.
|
Contributor
Author
|
I've benchmarked it with: func BenchmarkLabels_MatchLabels(b *testing.B) {
maxLabels := 30
allLabels := make(Labels, maxLabels)
allNames := [][]string{}
for i := 0; i < maxLabels; i++ {
allLabels[i] = Label{Name: strings.Repeat(string('a'+byte(i)), 5)}
if i%5 == 0 || i == maxLabels-1 {
names := []string{}
for j := 0; j <= i; j++ {
names = append(names, allLabels[j].Name)
}
allNames = append(allNames, names)
names = names[:0]
for j := 0; j <= i; j++ {
names = append(names, allLabels[j].Name+"_miss")
}
allNames = append(allNames, names)
}
}
for _, size := range []int{5, 10, maxLabels} {
labels := allLabels[:size]
for _, on := range []bool{true, false} {
for _, names := range allNames {
b.Run(fmt.Sprintf("with %d labels on=%v names=%d", size, on, len(names)), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
labels.MatchLabels(on, names...)
}
})
}
}
}
}But wasn't sure if that's a useful benchmark so didn't add it. Diff is below. benchstat diff |
roidelapluie
approved these changes
May 20, 2022
Member
|
I don't expect this to make a visible difference, but it's good to have it. |
roidelapluie
pushed a commit
to roidelapluie/prometheus
that referenced
this pull request
Jun 22, 2022
We know the max size of our map so we can create it with that information and avoid extra allocations Signed-off-by: Łukasz Mierzwa <l.mierzwa@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We know the max size of our map so we can create it with that information and avoid extra allocations
Signed-off-by: Łukasz Mierzwa l.mierzwa@gmail.com