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

go/vt/callerid: speedup callerid context access #16352

Closed
wants to merge 1 commit into from

Conversation

mattrobenolt
Copy link
Contributor

Relatively minor, but this is called and used relatively frequently, plus a very easy win.

callerid.NewContext combines both CallerID and VTGateCallerID into a singular Context.WithValue.

This is slightly more performant because each WithValue wraps a new layer of Context, which also means when doing Context.Value, this gets searched recursively through each layer.

Reducing this to 1 "tuple" means 1 less layer of Context wrapping, and these are always both set together through NewContext.

As you can see, this ultimately makes EffectiveCallerIDFromContext slightly faster due to it being 1 less layer to unwrap.

$ benchstat {old,new}.txt
goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/vt/callerid
                                                │   old.txt   │               new.txt               │
                                                │   sec/op    │   sec/op     vs base                │
CallerIDContext/New-10                            52.92n ± 0%   44.20n ± 3%  -16.46% (p=0.000 n=10)
CallerIDContext/EffectiveCallerIDFromContext-10   7.031n ± 0%   3.435n ± 0%  -51.14% (p=0.000 n=10)
CallerIDContext/ImmediateCallerIDFromContext-10   6.880n ± 2%   3.527n ± 1%  -48.73% (p=0.000 n=10)
geomean                                           13.68n        8.121n       -40.63%

                                                │   old.txt    │               new.txt                │
                                                │     B/op     │    B/op     vs base                  │
CallerIDContext/New-10                            96.00 ± 0%     64.00 ± 0%  -33.33% (p=0.000 n=10)
CallerIDContext/EffectiveCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
CallerIDContext/ImmediateCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                                      ²               -12.64%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                │   old.txt    │               new.txt               │
                                                │  allocs/op   │ allocs/op   vs base                 │
CallerIDContext/New-10                            2.000 ± 0%     2.000 ± 0%       ~ (p=1.000 n=10) ¹
CallerIDContext/EffectiveCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CallerIDContext/ImmediateCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                      ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Relatively minor, but this is called and used relatively frequently,
plus a very easy win.

`callerid.NewContext` combines both CallerID and VTGateCallerID into a
singular `Context.WithValue`.

This is slightly more performant because each `WithValue` wraps a new
layer of `Context`, which also means when doing `Context.Value`, this
gets searched recursively through each layer.

Reducing this to 1 "tuple" means 1 less layer of Context wrapping, and
these are always both set together through `NewContext`.

As you can see, this ultimately makes `EffectiveCallerIDFromContext`
slightly faster due to it being 1 less layer to unwrap.

```
$ benchstat {old,new}.txt
goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/vt/callerid
                                                │   old.txt   │               new.txt               │
                                                │   sec/op    │   sec/op     vs base                │
CallerIDContext/New-10                            52.92n ± 0%   44.20n ± 3%  -16.46% (p=0.000 n=10)
CallerIDContext/EffectiveCallerIDFromContext-10   7.031n ± 0%   3.435n ± 0%  -51.14% (p=0.000 n=10)
CallerIDContext/ImmediateCallerIDFromContext-10   6.880n ± 2%   3.527n ± 1%  -48.73% (p=0.000 n=10)
geomean                                           13.68n        8.121n       -40.63%

                                                │   old.txt    │               new.txt                │
                                                │     B/op     │    B/op     vs base                  │
CallerIDContext/New-10                            96.00 ± 0%     64.00 ± 0%  -33.33% (p=0.000 n=10)
CallerIDContext/EffectiveCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
CallerIDContext/ImmediateCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                                      ²               -12.64%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                │   old.txt    │               new.txt               │
                                                │  allocs/op   │ allocs/op   vs base                 │
CallerIDContext/New-10                            2.000 ± 0%     2.000 ± 0%       ~ (p=1.000 n=10) ¹
CallerIDContext/EffectiveCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
CallerIDContext/ImmediateCallerIDFromContext-10   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                      ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean
```

Signed-off-by: Matt Robenolt <matt@ydekproductions.com>
@mattrobenolt mattrobenolt requested a review from deepthi as a code owner July 9, 2024 06:18
Copy link
Contributor

vitess-bot bot commented Jul 9, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jul 9, 2024
@github-actions github-actions bot added this to the v21.0.0 milestone Jul 9, 2024
}
return nil
}

// ImmediateCallerIDFromContext returns the ImmediateCallerID(querypb.VTGateCallerID)
// stored in the Context, if any
func ImmediateCallerIDFromContext(ctx context.Context) *querypb.VTGateCallerID {
im, ok := ctx.Value(immediateCallerIDKey).(*querypb.VTGateCallerID)
if ok && im != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also removed the extraneous != nil check, which wasn't actually free. But is entirely unnecessary to check. If it's nil, can simply return nil.

Copy link

codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.69%. Comparing base (079ef28) to head (6dc6dfd).
Report is 94 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16352      +/-   ##
==========================================
- Coverage   68.70%   68.69%   -0.02%     
==========================================
  Files        1547     1547              
  Lines      198287   198284       -3     
==========================================
- Hits       136239   136212      -27     
- Misses      62048    62072      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +35 to +36
ef *vtrpcpb.CallerID
im *querypb.VTGateCallerID
Copy link
Member

Choose a reason for hiding this comment

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

I'd call these effective and immediate for (a) readability and (b) so that people can correlate them to concepts they are already familiar with.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I can do that. I yanked ef and im since they were used throughout this file already for function args, etc.

// internal Context key for effective CallerID
effectiveCallerIDKey callerIDKey = 1
)
type callerIDContextKey struct{}
Copy link
Member

Choose a reason for hiding this comment

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

Previously the keys were integers. Now we have only one key and it is an empty struct. Is it actually more efficient to use an empty struct as the key versus an integer value of 0?

Copy link
Contributor

Choose a reason for hiding this comment

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

An int is 4 or 8 bytes of storage whereas an empty struct is 0 bytes AFAIUI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, a zero struct occupies no extra space, it's relatively insignificant here since we're talking about a single integer, but a zero sized struct has a constant pointer in the Go runtime and is effectively a pointer to one address no matter the type. Specifically this runtime.zerobase.

The comparison has to be done with an interface because of the any in Context, so an interface just holds a reference to what type + the value, in which case, it's effectively only a ref to the type without a value, since the value is nothing, and the comparison is just on the type, rather than an interface of {type: int, val: 1} or whatever.

The docs also suggest using empty structs for keys: https://pkg.go.dev/context#WithValue

To avoid allocating when assigning to an interface{}, context keys often have concrete type struct{}. Alternatively, exported context key variables' static type should be a pointer or interface.

Examples of this can be seen through the stdlib codebase.

https://github.com/golang/go/blob/a71bb570d064f6d0c1cf59cef4b7a044be7a39d3/src/runtime/trace/annotation.go#L14
https://github.com/golang/go/blob/a71bb570d064f6d0c1cf59cef4b7a044be7a39d3/src/net/http/transport.go#L2809
https://github.com/golang/go/blob/a71bb570d064f6d0c1cf59cef4b7a044be7a39d3/src/runtime/pprof/label.go#L25

etc

I doubt in practice this makes a tangible difference, but it is the established pattern in stdlib for context keys. If they were to make compiler optimizations for this, I imagine they'd target this pattern specifically.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. TiL

Copy link
Contributor

This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:

  • Push additional commits to the associated branch.
  • Remove the stale label.
  • Add a comment indicating why it is not stale.

If no action is taken within 7 days, this PR will be closed.

@github-actions github-actions bot added the Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. label Aug 11, 2024
Copy link
Contributor

This PR was closed because it has been stale for 7 days with no activity.

@github-actions github-actions bot closed this Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants