Don't do send buffer allocation in header.Marshal, and don't allocate minMTU for outgoing non-data packets, throw away sendBufferPool
sendBufferPool will be easier to work with in the future with allocation no longer occuring in header.Marshal. I'm not even sure it's necessary.
This commit is contained in:
@@ -129,9 +129,17 @@ func (c *Conn) send(_type st, connID uint16, payload []byte, seqNr uint16) (err
|
||||
Bytes: selAck,
|
||||
}},
|
||||
}
|
||||
p := h.Marshal()
|
||||
var p []byte
|
||||
if len(payload) == 0 {
|
||||
p = make([]byte, 0, maxHeaderSize)
|
||||
} else {
|
||||
p = make([]byte, 0, minMTU)
|
||||
// p = sendBufferPool.Get().([]byte)[:0:minMTU]
|
||||
}
|
||||
n := h.Marshal(p)
|
||||
p = p[:n]
|
||||
// Extension headers are currently fixed in size.
|
||||
if len(p) != maxHeaderSize {
|
||||
if n != maxHeaderSize {
|
||||
panic("header has unexpected size")
|
||||
}
|
||||
p = append(p, payload...)
|
||||
@@ -202,7 +210,7 @@ func (c *Conn) write(_type st, connID uint16, payload []byte, seqNr uint16) (n i
|
||||
n = len(payload)
|
||||
// Copy payload so caller to write can continue to use the buffer.
|
||||
if payload != nil {
|
||||
payload = append(sendBufferPool.Get().([]byte)[:0:minMTU], payload...)
|
||||
payload = append([]byte(nil), payload...)
|
||||
}
|
||||
send := &send{
|
||||
payloadSize: uint32(len(payload)),
|
||||
|
||||
@@ -74,16 +74,14 @@ func (h *header) Unmarshal(b []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (h *header) Marshal() (ret []byte) {
|
||||
hLen := 20 + func() (ret int) {
|
||||
func (h *header) Marshal(p []byte) (n int) {
|
||||
n = 20 + func() (ret int) {
|
||||
for _, ext := range h.Extensions {
|
||||
ret += 2 + len(ext.Bytes)
|
||||
}
|
||||
return
|
||||
}()
|
||||
ret = sendBufferPool.Get().([]byte)[:hLen:minMTU]
|
||||
// ret = make([]byte, hLen, minMTU)
|
||||
p := ret // Used for manipulating ret.
|
||||
p = p[:n]
|
||||
p[0] = byte(h.Type<<4 | 1)
|
||||
binary.BigEndian.PutUint16(p[2:4], h.ConnID)
|
||||
binary.BigEndian.PutUint32(p[4:8], h.Timestamp)
|
||||
|
||||
@@ -212,12 +212,15 @@ func (s *Socket) dispatch(read read) {
|
||||
|
||||
// Send a reset in response to a packet with the given header.
|
||||
func (s *Socket) reset(addr net.Addr, ackNr, connId uint16) {
|
||||
go s.writeTo((&header{
|
||||
b := make([]byte, 0, maxHeaderSize)
|
||||
h := header{
|
||||
Type: stReset,
|
||||
Version: 1,
|
||||
ConnID: connId,
|
||||
AckNr: ackNr,
|
||||
}).Marshal(), addr)
|
||||
}
|
||||
b = b[:h.Marshal(b)]
|
||||
go s.writeTo(b, addr)
|
||||
}
|
||||
|
||||
// Return a recv_id that should be free. Handling the case where it isn't is
|
||||
|
||||
@@ -60,9 +60,9 @@ var (
|
||||
unsentStatePackets = expvar.NewInt("utpUnsentStatePackets")
|
||||
unusedReads = expvar.NewInt("utpUnusedReads")
|
||||
unusedReadsDropped = expvar.NewInt("utpUnusedReadsDropped")
|
||||
sendBufferPool = sync.Pool{
|
||||
New: func() interface{} { return make([]byte, minMTU) },
|
||||
}
|
||||
// sendBufferPool = sync.Pool{
|
||||
// New: func() interface{} { return make([]byte, minMTU) },
|
||||
// }
|
||||
// This is the latency we assume on new connections. It should be higher
|
||||
// than the latency we expect on most connections to prevent excessive
|
||||
// resending to peers that take a long time to respond, before we've got a
|
||||
|
||||
Reference in New Issue
Block a user