From e59ea9d5f51bf31b575b6380d6c2036c3c9f12bf Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 20 Jan 2021 17:06:05 +0800 Subject: [PATCH] preallocate a slice of one frame when packing a packet The majority of packets will contain only one STREAM frame. We should make sure that we don't need to grow the slice in the common case. --- packet_packer.go | 2 +- packet_packer_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packet_packer.go b/packet_packer.go index ab8462b6..231508c8 100644 --- a/packet_packer.go +++ b/packet_packer.go @@ -579,7 +579,7 @@ func (p *packetPacker) maybeGetAppDataPacketWithEncLevel(maxPayloadSize protocol } func (p *packetPacker) composeNextPacket(maxFrameSize protocol.ByteCount, ackAllowed bool) *payload { - payload := &payload{} + payload := &payload{frames: make([]ackhandler.Frame, 0, 1)} var hasDatagram bool if p.datagramQueue != nil { diff --git a/packet_packer_test.go b/packet_packer_test.go index 63313825..8eac0145 100644 --- a/packet_packer_test.go +++ b/packet_packer_test.go @@ -292,7 +292,8 @@ var _ = Describe("Packet packer", func() { pnManager.EXPECT().PopPacketNumber(protocol.Encryption0RTT).Return(protocol.PacketNumber(0x42)) cf := ackhandler.Frame{Frame: &wire.MaxDataFrame{MaximumData: 0x1337}} framer.EXPECT().HasData().Return(true) - framer.EXPECT().AppendControlFrames(nil, gomock.Any()).DoAndReturn(func(frames []ackhandler.Frame, _ protocol.ByteCount) ([]ackhandler.Frame, protocol.ByteCount) { + framer.EXPECT().AppendControlFrames(gomock.Any(), gomock.Any()).DoAndReturn(func(frames []ackhandler.Frame, _ protocol.ByteCount) ([]ackhandler.Frame, protocol.ByteCount) { + Expect(frames).To(BeEmpty()) return append(frames, cf), cf.Length(packer.version) }) // TODO: check sizes