-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
161df86
to
6727d84
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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>
6727d84
to
0c60614
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice stuff!
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.
Checklist