Skip to content

Commit

Permalink
avoid allocations when adding packets to the sent packet history
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 27, 2022
1 parent 07412be commit fd38fe4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/ackhandler/sent_packet_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 17 additions & 17 deletions internal/ackhandler/sent_packet_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit fd38fe4

Please sign in to comment.