refactor header writing to append to a byte slice (#3646)

This avoids having to allocate a bytes.Buffer.
This commit is contained in:
Marten Seemann 2023-01-17 01:56:06 -08:00 committed by GitHub
parent 3d4bbc28ba
commit c24fbb094c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 282 additions and 279 deletions

View file

@ -1,7 +1,6 @@
package self_test
import (
"bytes"
"context"
"errors"
"fmt"
@ -116,12 +115,12 @@ var _ = Describe("MITM test", func() {
for i := 0; i < numPackets; i++ {
payloadLen := mrand.Int31n(100)
replyHdr.Length = protocol.ByteCount(mrand.Int31n(payloadLen + 1))
buf := &bytes.Buffer{}
Expect(replyHdr.Write(buf, version)).To(Succeed())
b := make([]byte, payloadLen)
mrand.Read(b)
buf.Write(b)
if _, err := conn.WriteTo(buf.Bytes(), remoteAddr); err != nil {
b, err := replyHdr.Append(nil, version)
Expect(err).ToNot(HaveOccurred())
r := make([]byte, payloadLen)
mrand.Read(r)
b = append(b, r...)
if _, err := conn.WriteTo(b, remoteAddr); err != nil {
return
}
<-ticker.C
@ -134,13 +133,13 @@ var _ = Describe("MITM test", func() {
Expect(err).To(MatchError(wire.ErrInvalidReservedBits))
}
for i := 0; i < numPackets; i++ {
buf := &bytes.Buffer{}
Expect(wire.WriteShortHeader(buf, connID, pn, pnLen, protocol.KeyPhaseBit(mrand.Intn(2)))).To(Succeed())
b, err := wire.AppendShortHeader(nil, connID, pn, pnLen, protocol.KeyPhaseBit(mrand.Intn(2)))
Expect(err).ToNot(HaveOccurred())
payloadLen := mrand.Int31n(100)
b := make([]byte, payloadLen)
mrand.Read(b)
buf.Write(b)
if _, err := conn.WriteTo(buf.Bytes(), remoteAddr); err != nil {
r := make([]byte, payloadLen)
mrand.Read(r)
b = append(b, r...)
if _, err := conn.WriteTo(b, remoteAddr); err != nil {
return
}
<-ticker.C

View file

@ -28,7 +28,6 @@ func isProxyRunning() bool {
var _ = Describe("QUIC Proxy", func() {
makePacket := func(p protocol.PacketNumber, payload []byte) []byte {
b := &bytes.Buffer{}
hdr := wire.ExtendedHeader{
Header: wire.Header{
Type: protocol.PacketTypeInitial,
@ -40,10 +39,10 @@ var _ = Describe("QUIC Proxy", func() {
PacketNumber: p,
PacketNumberLen: protocol.PacketNumberLen4,
}
Expect(hdr.Write(b, protocol.VersionWhatever)).To(Succeed())
raw := b.Bytes()
raw = append(raw, payload...)
return raw
b, err := hdr.Append(nil, protocol.Version1)
Expect(err).ToNot(HaveOccurred())
b = append(b, payload...)
return b
}
readPacketNumber := func(b []byte) protocol.PacketNumber {