Merge pull request #1309 from lucas-clemente/ack-binary-search

use binary search to check if an ACK acks a packet
This commit is contained in:
Marten Seemann
2018-04-19 18:01:33 +09:00
committed by GitHub
2 changed files with 9 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ package wire
import (
"bytes"
"errors"
"sort"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
@@ -202,17 +203,15 @@ func (f *AckFrame) LowestAcked() protocol.PacketNumber {
// AcksPacket determines if this ACK frame acks a certain packet number
func (f *AckFrame) AcksPacket(p protocol.PacketNumber) bool {
if p < f.LowestAcked() || p > f.LargestAcked() { // this is just a performance optimization
if p < f.LowestAcked() || p > f.LargestAcked() {
return false
}
// TODO: this could be implemented as a binary search
for _, ackRange := range f.AckRanges {
if p >= ackRange.Smallest && p <= ackRange.Largest {
return true
}
}
return false
i := sort.Search(len(f.AckRanges), func(i int) bool {
return p >= f.AckRanges[i].Smallest
})
// i will always be < len(f.AckRanges), since we checked above that p is not bigger than the largest acked
return p <= f.AckRanges[i].Largest
}
func encodeAckDelay(delay time.Duration) uint64 {

View File

@@ -314,12 +314,14 @@ var _ = Describe("ACK Frame (for IETF QUIC)", func() {
}
Expect(f.AcksPacket(4)).To(BeFalse())
Expect(f.AcksPacket(5)).To(BeTrue())
Expect(f.AcksPacket(6)).To(BeTrue())
Expect(f.AcksPacket(7)).To(BeTrue())
Expect(f.AcksPacket(8)).To(BeTrue())
Expect(f.AcksPacket(9)).To(BeFalse())
Expect(f.AcksPacket(14)).To(BeFalse())
Expect(f.AcksPacket(15)).To(BeTrue())
Expect(f.AcksPacket(18)).To(BeTrue())
Expect(f.AcksPacket(19)).To(BeTrue())
Expect(f.AcksPacket(20)).To(BeTrue())
Expect(f.AcksPacket(21)).To(BeFalse())
})