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/trace/agent: improve NormalizeTag #2951

Merged
merged 2 commits into from
Jan 28, 2019
Merged

pkg/trace/agent: improve NormalizeTag #2951

merged 2 commits into from
Jan 28, 2019

Conversation

gbbr
Copy link
Contributor

@gbbr gbbr commented Jan 24, 2019

Improves performance of NormalizeTag

name                      old time/op    new time/op    delta
NormalizeTag/ok-4            129ns ± 0%      69ns ± 0%  -46.74%
NormalizeTag/trim-4          167ns ± 0%      92ns ± 0%  -44.97%
NormalizeTag/trim-both-4     197ns ± 0%     160ns ± 0%  -18.78%
NormalizeTag/plenty-4        193ns ± 0%     147ns ± 0%  -23.83%
NormalizeTag/more-4          261ns ± 0%     264ns ± 0%   +1.15%

name                      old alloc/op   new alloc/op   delta
NormalizeTag/ok-4             136B ± 0%       24B ± 0%  -82.35%
NormalizeTag/trim-4           160B ± 0%       32B ± 0%  -80.00%
NormalizeTag/trim-both-4      176B ± 0%       64B ± 0%  -63.64%
NormalizeTag/plenty-4         160B ± 0%       64B ± 0%  -60.00%
NormalizeTag/more-4           176B ± 0%      128B ± 0%  -27.27%

name                      old allocs/op  new allocs/op  delta
NormalizeTag/ok-4             3.00 ± 0%      2.00 ± 0%  -33.33%
NormalizeTag/trim-4           3.00 ± 0%      2.00 ± 0%  -33.33%
NormalizeTag/trim-both-4      3.00 ± 0%      3.00 ± 0%    0.00%
NormalizeTag/plenty-4         3.00 ± 0%      3.00 ± 0%    0.00%
NormalizeTag/more-4           3.00 ± 0%      4.00 ± 0%  +33.33%

Instead of creating a buffer, create a series of tuples representing cuts that need to happen inside the tag string, and apply them later. If no changes need to happen, the same string is returned without any extra allocations.

As can be seen in the benchmarks, the algorithm becomes slower the more cuts need to be made. For common scenarios with no changes or few changes, the improvement is around 400% for memory.

@gbbr gbbr added this to the 6.10.0 milestone Jan 24, 2019
@gbbr gbbr requested a review from a team as a code owner January 24, 2019 15:41
@gbbr gbbr force-pushed the gbbr/normalize-tag branch from 3fffb4a to c10955b Compare January 24, 2019 15:53
@codecov-io
Copy link

codecov-io commented Jan 24, 2019

Codecov Report

Merging #2951 into master will increase coverage by 0.06%.
The diff coverage is 96.77%.

@@            Coverage Diff             @@
##           master    #2951      +/-   ##
==========================================
+ Coverage   56.45%   56.51%   +0.06%     
==========================================
  Files         482      482              
  Lines       34185    34223      +38     
==========================================
+ Hits        19298    19340      +42     
+ Misses      13728    13724       -4     
  Partials     1159     1159
Impacted Files Coverage Δ
pkg/trace/agent/tags.go 72.27% <96.77%> (+7.98%) ⬆️
pkg/trace/writer/stats.go 89.69% <0%> (-1.04%) ⬇️
pkg/trace/writer/trace.go 91.66% <0%> (+0.49%) ⬆️
pkg/logs/auditor/auditor.go 71.83% <0%> (+0.7%) ⬆️

continue
}

c = unicode.ToLower(c)
switch {
// handle always valid cases
case unicode.IsLetter(c) || c == ':':
Copy link
Contributor

Choose a reason for hiding this comment

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

We can merge the 1st and 3rd case together?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually there is a catch here: switch case statements are evaluated top-to-bottom and the idea here is that you can not go past the second case (length = 0) if the character isn't a letter or a colon. In other words only letters or colons can start the tag name.

If we merge the two, it will alter this rule.

pkg/trace/agent/tags.go Outdated Show resolved Hide resolved
buf.WriteRune(c)
lastWasUnderscore = false
// lower-case
norm[i] += 'a' - 'A'
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: we can put +32 directly, saying that it moves a ascii upper-case to ascii lower-case equivalent.

Choose a reason for hiding this comment

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

The compiler should auto compute this expression so we can leave this which I think gives better readability

Copy link
Contributor Author

@gbbr gbbr Jan 28, 2019

Choose a reason for hiding this comment

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

I agree with @erichgess here. I liked this way and I even checked how the standard library does it, so I'd rather keep it if that's ok: https://github.com/golang/go/blob/05f8b44d5edc2960eff106e5e780cf83535d0533/src/unicode/letter.go#L267

@gbbr
Copy link
Contributor Author

gbbr commented Jan 28, 2019

I'd say this is good to review if you'd like to take another look @LotharSee

@gbbr gbbr merged commit c6012c3 into master Jan 28, 2019
@gbbr gbbr deleted the gbbr/normalize-tag branch January 28, 2019 10:24
@gbbr
Copy link
Contributor Author

gbbr commented Jan 29, 2019

Has a bug that was fixed in #2957

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team/agent-apm trace-agent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants