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

ReadMsgHeader use bytes pool #1386

Closed
wants to merge 2 commits into from

Conversation

IIvyPy
Copy link

@IIvyPy IIvyPy commented Jun 28, 2022

I find the makeslice of ReadMsgHeader will cost much GC time. So I suggest to use bytes pool to avoid the GC cost.

// use sync.Pool to avoid gc overhead.
var udpPool sync.Pool

func init() {
Copy link
Owner

Choose a reason for hiding this comment

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

please, no init functions

@@ -260,6 +261,13 @@ func (c *Client) exchangeContext(ctx context.Context, m *Msg, co *Conn) (r *Msg,
return r, rtt, err
}

// use sync.Pool to avoid gc overhead.
Copy link
Owner

Choose a reason for hiding this comment

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

comment doesn't add a lot

var udpPool sync.Pool

func init() {
udpPool.New = makeUDPBuffer(DefaultMsgSize)
Copy link
Owner

Choose a reason for hiding this comment

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

DefaultMsgSize is 512, a header is max 12 bytes, is this intentional?

@miekg
Copy link
Owner

miekg commented Jul 7, 2022

your commit message also don't explain anything about this change, it's just a one-liner

So it's on the client you want to use a buffer just like we do on the server side? Any data available (that should be in the ocmmit msg) on why and how this came about??

return nil, err
}

p = make([]byte, length)
n, err = io.ReadFull(co.Conn, p)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is wrong. You're ignoring the length which can be up to 64KiB. This would limit the max message size under TCP to whatever the pool returns.

p = make([]byte, co.UDPSize)
} else {
p = make([]byte, MinMsgSize)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code was important. UDPSize can be set larger than MinMsgSize. You're not handling that at all.

return nil, ErrShortRead
}

p = p[:n]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This slice call is important, we only read n-bytes not the full length of p.

@tmthrgd
Copy link
Collaborator

tmthrgd commented Apr 18, 2023

I'm sympathetic to the performance concerns here, but this isn't quite the way to go about it. We'd need a pool on the Client struct itself. But considering there's been no movement here in almost a year, I'm going to go ahead and close this PR. Feel free to re-open and address the issues above.

@tmthrgd tmthrgd closed this Apr 18, 2023
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