Skip to content

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

Merged
merged 3 commits into from
Nov 13, 2013

Conversation

cxmcc
Copy link
Contributor

@cxmcc cxmcc commented Nov 11, 2013

Possible benefits:

  • Less expensive function calls and stack space.
  • Probably make it easier for compression implementation since the loop gonna work on the same buffer (uncompressed payload).

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)
Copy link
Member

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

Copy link
Contributor Author

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.

@julienschmidt
Copy link
Member

This makes the common case (non splitted packets) a lot more expensive and destroys our current "zero allocations" approach.

data, err = mc.readPacket()
if err == nil {
return append(buf, data...), nil
if pktLen < maxPacketSize {
Copy link
Member

Choose a reason for hiding this comment

The 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 becomes

if pktLen >= maxPacketSize {
  payload = append(payload, data...)
  continue
}
return data, nil

Copy link
Member

Choose a reason for hiding this comment

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

and (handwaving) the correct variable is returned every time... (not data if it should be payload)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@cxmcc
Copy link
Contributor Author

cxmcc commented Nov 13, 2013

@julienschmidt the logic after the new patch, for the common cases (non-splitting), will not invoke any allocation (like old approach); for large packets, avoids recursive calls and copying twice.

// Read packet body [pktLen bytes]
if data, err = mc.buf.readNext(pktLen); err == nil {
if pktLen < maxPacketSize {
// Zero-allocation for non-splitting packets
Copy link
Member

Choose a reason for hiding this comment

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

Maybe // zero allocations for non-split packets ?

I'm not a native speaker, but to me Zero-allocation sound like allocating a zero

@julienschmidt
Copy link
Member

The new approach looks fine to me 👍

@cxmcc
Copy link
Contributor Author

cxmcc commented Nov 13, 2013

thanks @julienschmidt, amended suggestions to previous commits.

@julienschmidt
Copy link
Member

LGTM now, final review by @arnehormann

return nil, driver.ErrBadConn
}

isLastPacket := (pktLen < maxPacketSize)
Copy link
Member

Choose a reason for hiding this comment

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

what happens when the packet length of the last packet is exactly maxPacketSize? Is that possible?
I'll look it up now, but if you know, please chime in...

Copy link
Member

Choose a reason for hiding this comment

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

what happens when the packet length of the last packet is exactly maxPacketSize? Is that possible?

No, a packet length of maxPacketSize marks a split packet

Copy link
Member

Choose a reason for hiding this comment

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

Thanks

@arnehormann
Copy link
Member

LGTM before and still does...

arnehormann added a commit that referenced this pull request Nov 13, 2013
Replace recursive approach with iterative approach for readPacket()
@arnehormann arnehormann merged commit 4c8905e into go-sql-driver:master Nov 13, 2013
@julienschmidt julienschmidt added this to the v1.2 milestone Apr 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants