-
Notifications
You must be signed in to change notification settings - Fork 2.3k
zero-copy buffer #55
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
zero-copy buffer #55
Changes from 3 commits
96a4f13
c2dde2d
de021f9
a602069
7ac8bc9
f3fa1f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,15 +26,14 @@ import ( | |
// Read packet to buffer 'data' | ||
func (mc *mysqlConn) readPacket() (data []byte, err error) { | ||
// Read packet header | ||
data = make([]byte, 4) | ||
err = mc.buf.read(data) | ||
data, err = mc.buf.readNext(4) | ||
if err != nil { | ||
errLog.Print(err.Error()) | ||
return nil, driver.ErrBadConn | ||
} | ||
|
||
// Packet Length [24 bit] | ||
pktLen := uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16 | ||
pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leave as is, cast to int in L54 readNext? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the benefit of that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. honestly - none. Just one change less (and I dislike parens around long expressions) |
||
if pktLen < 1 { | ||
errLog.Print(errMalformPkt.Error()) | ||
|
@@ -52,8 +51,7 @@ func (mc *mysqlConn) readPacket() (data []byte, err error) { | |
mc.sequence++ | ||
|
||
// Read packet body [pktLen bytes] | ||
data = make([]byte, pktLen) | ||
err = mc.buf.read(data) | ||
data, err = mc.buf.readNext(pktLen) | ||
if err == nil { | ||
if pktLen < maxPacketSize { | ||
return data, nil | ||
|
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.
replace function body with:
I don't know if the append-code is idiomatic, but it's the best I can offer. Go's compiler could optimize it and get rid of the non-required allocation. I'm not sure about the performance as is, though. Maybe a make and copy is faster, maybe it'll even stay faster with later go compilers.
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 asked about growing a slice on go-nuts. Hopefully someone knows the best way to do it.
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.
Taking the results from go-nuts into consideration (esp. Maxim Khitrov's performance comparisons) I'd opt for moving
append
intofill
and use its capacity doubling behavior.Get rid of the first
if
in my proposal and handle resizing byappend
infill
.It should be safe as long as nobody low on RAM stores DVD-ISOs in MySQL.
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.
Even in this case the buffer grows just to
maxPacketSize
bytes (16 MiB)