avoid allocations when adding packets to the sent packet history

This commit is contained in:
Marten Seemann 2022-08-27 18:22:46 +03:00
parent 07412be8a0
commit fd38fe4a9a
2 changed files with 18 additions and 18 deletions

View file

@ -38,7 +38,7 @@ var _ = Describe("SentPacketHandler", func() {
getPacket := func(pn protocol.PacketNumber, encLevel protocol.EncryptionLevel) *Packet {
if el, ok := handler.getPacketNumberSpace(encLevel).history.packetMap[pn]; ok {
return &el.Value
return el.Value
}
return nil
}

View file

@ -11,18 +11,18 @@ import (
type sentPacketHistory struct {
rttStats *utils.RTTStats
outstandingPacketList *list.List[Packet]
etcPacketList *list.List[Packet]
packetMap map[protocol.PacketNumber]*list.Element[Packet]
outstandingPacketList *list.List[*Packet]
etcPacketList *list.List[*Packet]
packetMap map[protocol.PacketNumber]*list.Element[*Packet]
highestSent protocol.PacketNumber
}
func newSentPacketHistory(rttStats *utils.RTTStats) *sentPacketHistory {
return &sentPacketHistory{
rttStats: rttStats,
outstandingPacketList: list.New[Packet](),
etcPacketList: list.New[Packet](),
packetMap: make(map[protocol.PacketNumber]*list.Element[Packet]),
outstandingPacketList: list.New[*Packet](),
etcPacketList: list.New[*Packet](),
packetMap: make(map[protocol.PacketNumber]*list.Element[*Packet]),
highestSent: protocol.InvalidPacketNumber,
}
}
@ -33,7 +33,7 @@ func (h *sentPacketHistory) SentPacket(p *Packet, isAckEliciting bool) {
}
// Skipped packet numbers.
for pn := h.highestSent + 1; pn < p.PacketNumber; pn++ {
el := h.etcPacketList.PushBack(Packet{
el := h.etcPacketList.PushBack(&Packet{
PacketNumber: pn,
EncryptionLevel: p.EncryptionLevel,
SendTime: p.SendTime,
@ -44,11 +44,11 @@ func (h *sentPacketHistory) SentPacket(p *Packet, isAckEliciting bool) {
h.highestSent = p.PacketNumber
if isAckEliciting {
var el *list.Element[Packet]
var el *list.Element[*Packet]
if p.outstanding() {
el = h.outstandingPacketList.PushBack(*p)
el = h.outstandingPacketList.PushBack(p)
} else {
el = h.etcPacketList.PushBack(*p)
el = h.etcPacketList.PushBack(p)
}
h.packetMap[p.PacketNumber] = el
}
@ -59,7 +59,7 @@ func (h *sentPacketHistory) Iterate(cb func(*Packet) (cont bool, err error)) err
cont := true
outstandingEl := h.outstandingPacketList.Front()
etcEl := h.etcPacketList.Front()
var el *list.Element[Packet]
var el *list.Element[*Packet]
// whichever has the next packet number is returned first
for cont {
if outstandingEl == nil || (etcEl != nil && etcEl.Value.PacketNumber < outstandingEl.Value.PacketNumber) {
@ -76,7 +76,7 @@ func (h *sentPacketHistory) Iterate(cb func(*Packet) (cont bool, err error)) err
etcEl = etcEl.Next()
}
var err error
cont, err = cb(&el.Value)
cont, err = cb(el.Value)
if err != nil {
return err
}
@ -90,7 +90,7 @@ func (h *sentPacketHistory) FirstOutstanding() *Packet {
if el == nil {
return nil
}
return &el.Value
return el.Value
}
func (h *sentPacketHistory) Len() int {
@ -114,7 +114,7 @@ func (h *sentPacketHistory) HasOutstandingPackets() bool {
func (h *sentPacketHistory) DeleteOldPackets(now time.Time) {
maxAge := 3 * h.rttStats.PTO(false)
var nextEl *list.Element[Packet]
var nextEl *list.Element[*Packet]
// we don't iterate outstandingPacketList, as we should not delete outstanding packets.
// being outstanding for more than 3*PTO should only happen in the case of drastic RTT changes.
for el := h.etcPacketList.Front(); el != nil; el = nextEl {
@ -145,10 +145,10 @@ func (h *sentPacketHistory) DeclareLost(p *Packet) *Packet {
}
}
if el == nil {
el = h.etcPacketList.PushFront(*p)
el = h.etcPacketList.PushFront(p)
} else {
el = h.etcPacketList.InsertAfter(*p, el)
el = h.etcPacketList.InsertAfter(p, el)
}
h.packetMap[p.PacketNumber] = el
return &el.Value
return el.Value
}