-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Replace recursive approach with iterative approach for readPacket() #170
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -24,54 +24,53 @@ import ( | |
|
|
||
| // Read packet to buffer 'data' | ||
| func (mc *mysqlConn) readPacket() ([]byte, error) { | ||
| // Read packet header | ||
| data, err := mc.buf.readNext(4) | ||
| if err != nil { | ||
| errLog.Print(err.Error()) | ||
| mc.Close() | ||
| return nil, driver.ErrBadConn | ||
| } | ||
| var payload []byte | ||
| for { | ||
| // Read packet header | ||
| data, err := mc.buf.readNext(4) | ||
| if err != nil { | ||
| errLog.Print(err.Error()) | ||
| mc.Close() | ||
| return nil, driver.ErrBadConn | ||
| } | ||
|
|
||
| // Packet Length [24 bit] | ||
| pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16) | ||
| // Packet Length [24 bit] | ||
| pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16) | ||
|
|
||
| if pktLen < 1 { | ||
| errLog.Print(errMalformPkt.Error()) | ||
| mc.Close() | ||
| return nil, driver.ErrBadConn | ||
| } | ||
| if pktLen < 1 { | ||
| errLog.Print(errMalformPkt.Error()) | ||
| mc.Close() | ||
| return nil, driver.ErrBadConn | ||
| } | ||
|
|
||
| // Check Packet Sync [8 bit] | ||
| if data[3] != mc.sequence { | ||
| if data[3] > mc.sequence { | ||
| return nil, errPktSyncMul | ||
| } else { | ||
| return nil, errPktSync | ||
| // Check Packet Sync [8 bit] | ||
| if data[3] != mc.sequence { | ||
| if data[3] > mc.sequence { | ||
| return nil, errPktSyncMul | ||
| } else { | ||
| return nil, errPktSync | ||
| } | ||
| } | ||
| } | ||
| mc.sequence++ | ||
| mc.sequence++ | ||
|
|
||
| // Read packet body [pktLen bytes] | ||
| if data, err = mc.buf.readNext(pktLen); err == nil { | ||
| if pktLen < maxPacketSize { | ||
| return data, nil | ||
| // Read packet body [pktLen bytes] | ||
| data, err = mc.buf.readNext(pktLen) | ||
| if err != nil { | ||
| errLog.Print(err.Error()) | ||
| mc.Close() | ||
| return nil, driver.ErrBadConn | ||
| } | ||
|
|
||
| // Make a copy since data becomes invalid with the next read | ||
| buf := make([]byte, len(data)) | ||
| copy(buf, data) | ||
|
|
||
| // More data | ||
| data, err = mc.readPacket() | ||
| if err == nil { | ||
| return append(buf, data...), nil | ||
| payload = append(payload, buf...) | ||
|
|
||
| if pktLen < maxPacketSize { | ||
|
Member
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. @julienschmidt is you critique still valid if the append above is removed and this if pktLen >= maxPacketSize {
payload = append(payload, data...)
continue
}
return data, nil
Member
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. and (handwaving) the correct variable is returned every time... (not
Contributor
Author
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. @julienschmidt I understood what you mean. It's doing payload = append(nil, data) for common cases. (I thought golang will do payload=data for append(nil...) but obviously not.) We can still disable the buffer copying for common case by doing what @arnehormann suggested. |
||
| return payload, nil | ||
| } | ||
| } | ||
|
|
||
| // err case | ||
| mc.Close() | ||
| errLog.Print(err.Error()) | ||
| return nil, driver.ErrBadConn | ||
| } | ||
|
|
||
| // Write packet buffer 'data' | ||
|
|
||
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.
we don't need the copy without the recursion - data stays valid and appending copies it anyway
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.
good point! just added a commit to remove that.