mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-04 12:47:36 +03:00
use pointer to byte slices in the buffer pool
https://staticcheck.io/docs/staticcheck#SA6002 suggests to use pointers to objects in the sync.Pool.
This commit is contained in:
parent
d16dea09cc
commit
07b8821ef7
8 changed files with 21 additions and 30 deletions
|
@ -8,19 +8,20 @@ import (
|
||||||
|
|
||||||
var bufferPool sync.Pool
|
var bufferPool sync.Pool
|
||||||
|
|
||||||
func getPacketBuffer() []byte {
|
func getPacketBuffer() *[]byte {
|
||||||
return bufferPool.Get().([]byte)
|
return bufferPool.Get().(*[]byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
func putPacketBuffer(buf []byte) {
|
func putPacketBuffer(buf *[]byte) {
|
||||||
if cap(buf) != int(protocol.MaxReceivePacketSize) {
|
if cap(*buf) != int(protocol.MaxReceivePacketSize) {
|
||||||
panic("putPacketBuffer called with packet of wrong size!")
|
panic("putPacketBuffer called with packet of wrong size!")
|
||||||
}
|
}
|
||||||
bufferPool.Put(buf[:0])
|
bufferPool.Put(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
bufferPool.New = func() interface{} {
|
bufferPool.New = func() interface{} {
|
||||||
return make([]byte, 0, protocol.MaxReceivePacketSize)
|
b := make([]byte, 0, protocol.MaxReceivePacketSize)
|
||||||
|
return &b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,25 +8,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Buffer Pool", func() {
|
var _ = Describe("Buffer Pool", func() {
|
||||||
It("returns buffers of correct len and cap", func() {
|
It("returns buffers of cap", func() {
|
||||||
buf := getPacketBuffer()
|
buf := *getPacketBuffer()
|
||||||
Expect(buf).To(HaveLen(0))
|
|
||||||
Expect(buf).To(HaveCap(int(protocol.MaxReceivePacketSize)))
|
Expect(buf).To(HaveCap(int(protocol.MaxReceivePacketSize)))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("zeroes put buffers' length", func() {
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
buf := getPacketBuffer()
|
|
||||||
putPacketBuffer(buf[0:10])
|
|
||||||
buf = getPacketBuffer()
|
|
||||||
Expect(buf).To(HaveLen(0))
|
|
||||||
Expect(buf).To(HaveCap(int(protocol.MaxReceivePacketSize)))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
It("panics if wrong-sized buffers are passed", func() {
|
It("panics if wrong-sized buffers are passed", func() {
|
||||||
Expect(func() {
|
Expect(func() {
|
||||||
putPacketBuffer([]byte{0})
|
putPacketBuffer(&[]byte{0})
|
||||||
}).To(Panic())
|
}).To(Panic())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -245,7 +245,7 @@ func (c *client) listen() {
|
||||||
for {
|
for {
|
||||||
var n int
|
var n int
|
||||||
var addr net.Addr
|
var addr net.Addr
|
||||||
data := getPacketBuffer()
|
data := *getPacketBuffer()
|
||||||
data = data[:protocol.MaxReceivePacketSize]
|
data = data[:protocol.MaxReceivePacketSize]
|
||||||
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
|
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
|
||||||
// If it does, we only read a truncated packet, which will then end up undecryptable
|
// If it does, we only read a truncated packet, which will then end up undecryptable
|
||||||
|
|
|
@ -139,8 +139,8 @@ func unpackInitialPacket(aead crypto.AEAD, hdr *wire.Header, data []byte, versio
|
||||||
// packUnencryptedPacket provides a low-overhead way to pack a packet.
|
// packUnencryptedPacket provides a low-overhead way to pack a packet.
|
||||||
// It is supposed to be used in the early stages of the handshake, before a session (which owns a packetPacker) is available.
|
// It is supposed to be used in the early stages of the handshake, before a session (which owns a packetPacker) is available.
|
||||||
func packUnencryptedPacket(aead crypto.AEAD, hdr *wire.Header, f wire.Frame, pers protocol.Perspective) ([]byte, error) {
|
func packUnencryptedPacket(aead crypto.AEAD, hdr *wire.Header, f wire.Frame, pers protocol.Perspective) ([]byte, error) {
|
||||||
raw := getPacketBuffer()
|
raw := *getPacketBuffer()
|
||||||
buffer := bytes.NewBuffer(raw)
|
buffer := bytes.NewBuffer(raw[:0])
|
||||||
if err := hdr.Write(buffer, pers, hdr.Version); err != nil {
|
if err := hdr.Write(buffer, pers, hdr.Version); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -342,8 +342,8 @@ func (p *packetPacker) writeAndSealPacket(
|
||||||
payloadFrames []wire.Frame,
|
payloadFrames []wire.Frame,
|
||||||
sealer handshake.Sealer,
|
sealer handshake.Sealer,
|
||||||
) ([]byte, error) {
|
) ([]byte, error) {
|
||||||
raw := getPacketBuffer()
|
raw := *getPacketBuffer()
|
||||||
buffer := bytes.NewBuffer(raw)
|
buffer := bytes.NewBuffer(raw[:0])
|
||||||
|
|
||||||
if err := header.Write(buffer, p.perspective, p.version); err != nil {
|
if err := header.Write(buffer, p.perspective, p.version); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -24,8 +24,9 @@ type packetUnpacker struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.Header, data []byte) (*unpackedPacket, error) {
|
func (u *packetUnpacker) Unpack(headerBinary []byte, hdr *wire.Header, data []byte) (*unpackedPacket, error) {
|
||||||
buf := getPacketBuffer()
|
buf := *getPacketBuffer()
|
||||||
defer putPacketBuffer(buf)
|
buf = buf[:0]
|
||||||
|
defer putPacketBuffer(&buf)
|
||||||
decrypted, encryptionLevel, err := u.aead.Open(buf, data, hdr.PacketNumber, headerBinary)
|
decrypted, encryptionLevel, err := u.aead.Open(buf, data, hdr.PacketNumber, headerBinary)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Wrap err in quicError so that public reset is sent by session
|
// Wrap err in quicError so that public reset is sent by session
|
||||||
|
|
|
@ -213,7 +213,7 @@ func populateServerConfig(config *Config) *Config {
|
||||||
// serve listens on an existing PacketConn
|
// serve listens on an existing PacketConn
|
||||||
func (s *server) serve() {
|
func (s *server) serve() {
|
||||||
for {
|
for {
|
||||||
data := getPacketBuffer()
|
data := *getPacketBuffer()
|
||||||
data = data[:protocol.MaxReceivePacketSize]
|
data = data[:protocol.MaxReceivePacketSize]
|
||||||
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
|
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
|
||||||
// If it does, we only read a truncated packet, which will then end up undecryptable
|
// If it does, we only read a truncated packet, which will then end up undecryptable
|
||||||
|
|
|
@ -393,7 +393,7 @@ runLoop:
|
||||||
}
|
}
|
||||||
// This is a bit unclean, but works properly, since the packet always
|
// This is a bit unclean, but works properly, since the packet always
|
||||||
// begins with the public header and we never copy it.
|
// begins with the public header and we never copy it.
|
||||||
putPacketBuffer(p.header.Raw)
|
putPacketBuffer(&p.header.Raw)
|
||||||
case p := <-s.paramsChan:
|
case p := <-s.paramsChan:
|
||||||
s.processTransportParameters(&p)
|
s.processTransportParameters(&p)
|
||||||
case _, ok := <-handshakeEvent:
|
case _, ok := <-handshakeEvent:
|
||||||
|
@ -867,7 +867,7 @@ func (s *session) sendPacket() (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) sendPackedPacket(packet *packedPacket) error {
|
func (s *session) sendPackedPacket(packet *packedPacket) error {
|
||||||
defer putPacketBuffer(packet.raw)
|
defer putPacketBuffer(&packet.raw)
|
||||||
err := s.sentPacketHandler.SentPacket(&ackhandler.Packet{
|
err := s.sentPacketHandler.SentPacket(&ackhandler.Packet{
|
||||||
PacketNumber: packet.header.PacketNumber,
|
PacketNumber: packet.header.PacketNumber,
|
||||||
Frames: packet.frames,
|
Frames: packet.frames,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue