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

Calculate common tags only once. #250

Merged
merged 1 commit into from
Jul 25, 2017
Merged

Conversation

janisz
Copy link
Contributor

@janisz janisz commented Jul 22, 2017

This PR extracts commonTags outside of loop.
They are not changed inside the loop so there is no
need to recalculate them every time.
Ta reduce allocations tags slice i create with
initial capacity equal to the lenght of application labels.

benchmark                              old ns/op     new ns/op     delta
BenchmarkApp_RegistrationIntents-8     438           415           -5.25%
BenchmarkApp_RegistrationIntents-8     436           419           -3.90%
BenchmarkApp_RegistrationIntents-8     432           415           -3.94%

benchmark                              old allocs     new allocs     delta
BenchmarkApp_RegistrationIntents-8     4              3              -25.00%
BenchmarkApp_RegistrationIntents-8     4              3              -25.00%
BenchmarkApp_RegistrationIntents-8     4              3              -25.00%

benchmark                              old bytes     new bytes     delta
BenchmarkApp_RegistrationIntents-8     128           208           +62.50%
BenchmarkApp_RegistrationIntents-8     128           208           +62.50%
BenchmarkApp_RegistrationIntents-8     128           208           +62.50%

This PR extracts commonTags outside of loop.
They are not changed inside the loop so there is no
need to recalculate them every time.
Ta reduce allocations tags slice i create with
initial capacity equal to the lenght of application labels.

```
benchmark                              old ns/op     new ns/op     delta
BenchmarkApp_RegistrationIntents-8     438           415           -5.25%
BenchmarkApp_RegistrationIntents-8     436           419           -3.90%
BenchmarkApp_RegistrationIntents-8     432           415           -3.94%

benchmark                              old allocs     new allocs     delta
BenchmarkApp_RegistrationIntents-8     4              3              -25.00%
BenchmarkApp_RegistrationIntents-8     4              3              -25.00%
BenchmarkApp_RegistrationIntents-8     4              3              -25.00%

benchmark                              old bytes     new bytes     delta
BenchmarkApp_RegistrationIntents-8     128           208           +62.50%
BenchmarkApp_RegistrationIntents-8     128           208           +62.50%
BenchmarkApp_RegistrationIntents-8     128           208           +62.50%
```
@janisz janisz requested review from pbetkier and a team July 22, 2017 14:49
@coveralls
Copy link

Coverage Status

Coverage increased (+0.006%) to 89.374% when pulling 1f7dc8e on improve_label_generation_perf into 1cda574 on master.

@janisz janisz merged commit 1c1b22f into master Jul 25, 2017
@janisz janisz deleted the improve_label_generation_perf branch July 25, 2017 07:46
@@ -135,7 +136,7 @@ func marathonAppNameToServiceName(name string, nameSeparator string) string {
}

func labelsToTags(labels map[string]string) []string {
tags := []string{}
tags := make([]string, 0, len(labels))
Copy link
Contributor

@kamilchm kamilchm Jul 28, 2017

Choose a reason for hiding this comment

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

len(labels) is ineffective here, the first thing that will happen to it is append, which will allocate new slice to fit tags and commonTags.

Copy link
Contributor Author

@janisz janisz Jul 28, 2017

Choose a reason for hiding this comment

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

No, this capacity is used to prepare a space for tags appended in for loop below. I'm not trying to optimize allocations of the caller of labelsToTags.

Copy link
Contributor

Choose a reason for hiding this comment

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

so why are you benchmarking BenchmarkApp_RegistrationIntents when you not trying to optimize it? ;)

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'm prepare the benchmark to see the whole change in performance of RegistrationIntents not just labelsToTags.
In this PR I removed one allocation in creation of tags from labels. Instead of starting with empty slice and grow it when needed I start with capacity that will be sufficient for tags. This reduces the allocation of the caller.

I do not agree this is ineffective but I agree it's a micro optimization. labelsToTags is called twice and this change improves the first call. There are cases when the second call will also be improved (port labels count equal size of common tags but none of this label will be a tag) but I'm only interested in most common case in our installation.

If you think you can improve this part of code feel free to use the benchmark and show your results. I'll be happy to accept a pull request.

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.

5 participants