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

filters/tracing: improve stateBagToTag #2775

Merged

Conversation

AlexanderYastrebov
Copy link
Member

  • reduce allocations when tag value is string
  • check statebag value first to bail out early if it is absent
  • restrict max number of arguments to two
goos: linux
goarch: amd64
pkg: github.com/zalando/skipper/filters/tracing
cpu: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
                            │   HEAD~1    │                HEAD                 │
                            │   sec/op    │   sec/op     vs base                │
StateBagToTag_StringValue-8   313.1n ± 2%   124.3n ± 1%  -60.29% (p=0.000 n=10)

                            │   HEAD~1   │                HEAD                │
                            │    B/op    │   B/op     vs base                 │
StateBagToTag_StringValue-8   19.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)

                            │   HEAD~1   │                HEAD                 │
                            │ allocs/op  │ allocs/op   vs base                 │
StateBagToTag_StringValue-8   2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)

For zalando-incubator/kubernetes-on-aws#6604

Comment on lines +67 to +68
if _, ok := value.(string); ok {
span.SetTag(f.tagName, value)
Copy link
Member Author

Choose a reason for hiding this comment

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

See golang/go#64541 why not

if s, ok := value.(string); ok {
    span.SetTag(f.tagName, s)
}

Copy link
Member

Choose a reason for hiding this comment

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

I guess we have similar cases all over the place.

Copy link
Member Author

Choose a reason for hiding this comment

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

I checked all span.SetTag callsites and its fine. There may be other places that receive interface{} like SetTag of course but I think this is a very specific pattern here - optimizing for string case.

We could simply use span.SetTag(f.tagName, value) without type assertion but that is not guaranteed to work for types other than string, bool and ints:

// Adds a tag to the span.
	//
	// If there is a pre-existing tag set for `key`, it is overwritten.
	//
	// Tag values can be numeric types, strings, or bools. The behavior of
	// other tag value types is undefined at the OpenTracing level. If a
	// tracing system does not know how to handle a particular value type, it
	// may ignore the tag, but shall not panic.
	//
	// Returns a reference to this Span for chaining.
	SetTag(key string, value interface{}) Span

so I decided to keep fmt.Sprint()

* reduce allocations when tag value is string
* check statebag value first to bail out early if it is absent
* restrict max number of arguments to two

```
goos: linux
goarch: amd64
pkg: github.com/zalando/skipper/filters/tracing
cpu: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
                            │   HEAD~1    │                HEAD                 │
                            │   sec/op    │   sec/op     vs base                │
StateBagToTag_StringValue-8   313.1n ± 2%   124.3n ± 1%  -60.29% (p=0.000 n=10)

                            │   HEAD~1   │                HEAD                │
                            │    B/op    │   B/op     vs base                 │
StateBagToTag_StringValue-8   19.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)

                            │   HEAD~1   │                HEAD                 │
                            │ allocs/op  │ allocs/op   vs base                 │
StateBagToTag_StringValue-8   2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)
```

Signed-off-by: Alexander Yastrebov <[email protected]>
@AlexanderYastrebov AlexanderYastrebov force-pushed the filters/tracing/stateBagToTag-reduce-allocs branch from 8deb1eb to 8deb30e Compare December 5, 2023 09:29
@MustafaSaber
Copy link
Member

👍

1 similar comment
@AlexanderYastrebov
Copy link
Member Author

👍

@AlexanderYastrebov AlexanderYastrebov merged commit c6deeb8 into master Dec 5, 2023
12 checks passed
@AlexanderYastrebov AlexanderYastrebov deleted the filters/tracing/stateBagToTag-reduce-allocs branch December 5, 2023 15:45
AlexanderYastrebov added a commit that referenced this pull request Dec 6, 2023
AlexanderYastrebov added a commit that referenced this pull request Dec 6, 2023
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.

3 participants