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/mysql: improve GTID encoding for OK packet #16361

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

mattrobenolt
Copy link
Contributor

@mattrobenolt mattrobenolt commented Jul 9, 2024

This isn't a super common path, but if we are tracking GTIDs, this will happen for every single query.

This enhancement effectively calculates the full size of the GTID data to be encoded up front to do a single allocation rather than naively appending to a slice.

I also added documentation and extracted the GTID encoding to it's own function with unit tests.

I also cleaned up how it's utilized to avoid some unnecessary byte -> string allocations within the packet writer.

$ benchstat {old,new}.txt
goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/mysql
               │   old.txt    │               new.txt               │
               │    sec/op    │   sec/op     vs base                │
EncGtidData-10   104.65n ± 1%   15.55n ± 0%  -85.14% (p=0.000 n=10)

               │   old.txt   │              new.txt               │
               │    B/op     │    B/op     vs base                │
EncGtidData-10   56.000 ± 0%   8.000 ± 0%  -85.71% (p=0.000 n=10)

               │  old.txt   │              new.txt               │
               │ allocs/op  │ allocs/op   vs base                │
EncGtidData-10   7.000 ± 0%   1.000 ± 0%  -85.71% (p=0.000 n=10)

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

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
@mattrobenolt mattrobenolt force-pushed the enc-gtid-performance branch from 161df86 to 6727d84 Compare July 9, 2024 22:55
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.70%. Comparing base (eb29999) to head (0c60614).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main   #16361    +/-   ##
========================================
  Coverage   68.69%   68.70%            
========================================
  Files        1547     1548     +1     
  Lines      198297   198445   +148     
========================================
+ Hits       136228   136345   +117     
- Misses      62069    62100    +31     

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

@mattlord mattlord added Type: Internal Cleanup Type: Performance Component: Performance and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jul 10, 2024
Copy link
Contributor

@mattlord mattlord left a comment

Choose a reason for hiding this comment

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

I only had a couple of minor comments/suggestions/nits. The only thing that we really should change is the use of screaming snake case. The others are very minor nits.

Thanks, @mattrobenolt !

@@ -787,15 +787,15 @@ func (c *Conn) writeOKPacketWithHeader(packetOk *PacketOK, headerType byte) erro
// assuming CapabilityClientProtocol41
length += 4 // status_flags + warnings

hasSessionTrack := c.Capabilities&CapabilityClientSessionTrack == CapabilityClientSessionTrack
Copy link
Contributor

@mattlord mattlord Jul 10, 2024

Choose a reason for hiding this comment

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

Nit, but using != 0 is more obvious and standard — at least within Vitess — although there's no practical change.

if packetOk.statusFlags&ServerSessionStateChanged == ServerSessionStateChanged {
data.writeEOFString(string(gtidData))
if hasGtidData {
data.writeEOFBytes(gtidData)
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 guess I can further specialize this explicitly into a data.writeGtidData(packetOk.sessionStateData) and directly write it into the buffer with the extra allocation, then change above to a like, length += encGtidDataSize(packetOk.sessionStateData)

Then we avoid the intermediary gtidData []byte being allocated.

This isn't a super common path, but if we are tracking GTIDs, this will
happen for every single query.

This enhancement effectively calculates the full size of the GTID data
to be encoded up front to do a single allocation rather than naively
appending to a slice.

I also added documentation and extracted the GTID encoding to it's own
function with unit tests.

I also cleaned up how it's utilized to avoid some unnecessary byte ->
string allocations within the packet writer.

```
$ benchstat {old,new}.txt
goos: darwin
goarch: arm64
pkg: vitess.io/vitess/go/mysql
               │   old.txt    │               new.txt               │
               │    sec/op    │   sec/op     vs base                │
EncGtidData-10   104.65n ± 1%   15.55n ± 0%  -85.14% (p=0.000 n=10)

               │   old.txt   │              new.txt               │
               │    B/op     │    B/op     vs base                │
EncGtidData-10   56.000 ± 0%   8.000 ± 0%  -85.71% (p=0.000 n=10)

               │  old.txt   │              new.txt               │
               │ allocs/op  │ allocs/op   vs base                │
EncGtidData-10   7.000 ± 0%   1.000 ± 0%  -85.71% (p=0.000 n=10)
```

Signed-off-by: Matt Robenolt <matt@ydekproductions.com>
@mattrobenolt mattrobenolt force-pushed the enc-gtid-performance branch from 6727d84 to 0c60614 Compare July 10, 2024 04:26
Copy link
Collaborator

@systay systay left a comment

Choose a reason for hiding this comment

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

nice stuff!

@mattlord mattlord merged commit bc32d84 into vitessio:main Jul 10, 2024
126 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants