perf: reduce time.Now overhead 2

This commit is contained in:
Toby 2022-05-06 19:11:35 -07:00
parent 6e42cb4586
commit 28b6210ce5
2 changed files with 19 additions and 16 deletions

View File

@ -20,6 +20,7 @@ type BrutalSender struct {
pacer *pacer pacer *pacer
pktInfoSlots [pktInfoSlotCount]pktInfo pktInfoSlots [pktInfoSlotCount]pktInfo
ackRate float64
} }
type pktInfo struct { type pktInfo struct {
@ -32,9 +33,10 @@ func NewBrutalSender(bps congestion.ByteCount) *BrutalSender {
bs := &BrutalSender{ bs := &BrutalSender{
bps: bps, bps: bps,
maxDatagramSize: initMaxDatagramSize, maxDatagramSize: initMaxDatagramSize,
ackRate: 1,
} }
bs.pacer = newPacer(func(now time.Time) congestion.ByteCount { bs.pacer = newPacer(func() congestion.ByteCount {
return congestion.ByteCount(float64(bs.bps) / bs.getAckRate(now)) return congestion.ByteCount(float64(bs.bps) / bs.ackRate)
}) })
return bs return bs
} }
@ -60,7 +62,7 @@ func (b *BrutalSender) GetCongestionWindow() congestion.ByteCount {
if rtt <= 0 { if rtt <= 0 {
return 10240 return 10240
} }
return congestion.ByteCount(float64(b.bps) * rtt.Seconds() * 1.5 / b.getAckRate(time.Now())) return congestion.ByteCount(float64(b.bps) * rtt.Seconds() * 1.5 / b.ackRate)
} }
func (b *BrutalSender) OnPacketSent(sentTime time.Time, bytesInFlight congestion.ByteCount, func (b *BrutalSender) OnPacketSent(sentTime time.Time, bytesInFlight congestion.ByteCount,
@ -80,6 +82,7 @@ func (b *BrutalSender) OnPacketAcked(number congestion.PacketNumber, ackedBytes
b.pktInfoSlots[slot].AckCount = 1 b.pktInfoSlots[slot].AckCount = 1
b.pktInfoSlots[slot].LossCount = 0 b.pktInfoSlots[slot].LossCount = 0
} }
b.updateAckRate(currentTimestamp)
} }
func (b *BrutalSender) OnPacketLost(number congestion.PacketNumber, lostBytes congestion.ByteCount, func (b *BrutalSender) OnPacketLost(number congestion.PacketNumber, lostBytes congestion.ByteCount,
@ -94,6 +97,7 @@ func (b *BrutalSender) OnPacketLost(number congestion.PacketNumber, lostBytes co
b.pktInfoSlots[slot].AckCount = 0 b.pktInfoSlots[slot].AckCount = 0
b.pktInfoSlots[slot].LossCount = 1 b.pktInfoSlots[slot].LossCount = 1
} }
b.updateAckRate(currentTimestamp)
} }
func (b *BrutalSender) SetMaxDatagramSize(size congestion.ByteCount) { func (b *BrutalSender) SetMaxDatagramSize(size congestion.ByteCount) {
@ -101,8 +105,7 @@ func (b *BrutalSender) SetMaxDatagramSize(size congestion.ByteCount) {
b.pacer.SetMaxDatagramSize(size) b.pacer.SetMaxDatagramSize(size)
} }
func (b *BrutalSender) getAckRate(now time.Time) float64 { func (b *BrutalSender) updateAckRate(currentTimestamp int64) {
currentTimestamp := now.Unix()
minTimestamp := currentTimestamp - pktInfoSlotCount minTimestamp := currentTimestamp - pktInfoSlotCount
var ackCount, lossCount uint64 var ackCount, lossCount uint64
for _, info := range b.pktInfoSlots { for _, info := range b.pktInfoSlots {
@ -113,13 +116,13 @@ func (b *BrutalSender) getAckRate(now time.Time) float64 {
lossCount += info.LossCount lossCount += info.LossCount
} }
if ackCount+lossCount < minSampleCount { if ackCount+lossCount < minSampleCount {
return 1 b.ackRate = 1
} }
rate := float64(ackCount) / float64(ackCount+lossCount) rate := float64(ackCount) / float64(ackCount+lossCount)
if rate < minAckRate { if rate < minAckRate {
return minAckRate b.ackRate = minAckRate
} }
return rate b.ackRate = rate
} }
func (b *BrutalSender) InSlowStart() bool { func (b *BrutalSender) InSlowStart() bool {

View File

@ -16,10 +16,10 @@ type pacer struct {
budgetAtLastSent congestion.ByteCount budgetAtLastSent congestion.ByteCount
maxDatagramSize congestion.ByteCount maxDatagramSize congestion.ByteCount
lastSentTime time.Time lastSentTime time.Time
getBandwidth func(time.Time) congestion.ByteCount // in bytes/s getBandwidth func() congestion.ByteCount // in bytes/s
} }
func newPacer(getBandwidth func(time.Time) congestion.ByteCount) *pacer { func newPacer(getBandwidth func() congestion.ByteCount) *pacer {
p := &pacer{ p := &pacer{
budgetAtLastSent: maxBurstPackets * initMaxDatagramSize, budgetAtLastSent: maxBurstPackets * initMaxDatagramSize,
maxDatagramSize: initMaxDatagramSize, maxDatagramSize: initMaxDatagramSize,
@ -40,15 +40,15 @@ func (p *pacer) SentPacket(sendTime time.Time, size congestion.ByteCount) {
func (p *pacer) Budget(now time.Time) congestion.ByteCount { func (p *pacer) Budget(now time.Time) congestion.ByteCount {
if p.lastSentTime.IsZero() { if p.lastSentTime.IsZero() {
return p.maxBurstSize(now) return p.maxBurstSize()
} }
budget := p.budgetAtLastSent + (p.getBandwidth(now)*congestion.ByteCount(now.Sub(p.lastSentTime).Nanoseconds()))/1e9 budget := p.budgetAtLastSent + (p.getBandwidth()*congestion.ByteCount(now.Sub(p.lastSentTime).Nanoseconds()))/1e9
return minByteCount(p.maxBurstSize(now), budget) return minByteCount(p.maxBurstSize(), budget)
} }
func (p *pacer) maxBurstSize(now time.Time) congestion.ByteCount { func (p *pacer) maxBurstSize() congestion.ByteCount {
return maxByteCount( return maxByteCount(
congestion.ByteCount((minPacingDelay+time.Millisecond).Nanoseconds())*p.getBandwidth(now)/1e9, congestion.ByteCount((minPacingDelay+time.Millisecond).Nanoseconds())*p.getBandwidth()/1e9,
maxBurstPackets*p.maxDatagramSize, maxBurstPackets*p.maxDatagramSize,
) )
} }
@ -62,7 +62,7 @@ func (p *pacer) TimeUntilSend() time.Time {
return p.lastSentTime.Add(maxDuration( return p.lastSentTime.Add(maxDuration(
minPacingDelay, minPacingDelay,
time.Duration(math.Ceil(float64(p.maxDatagramSize-p.budgetAtLastSent)*1e9/ time.Duration(math.Ceil(float64(p.maxDatagramSize-p.budgetAtLastSent)*1e9/
float64(p.getBandwidth(time.Now()))))*time.Nanosecond, float64(p.getBandwidth())))*time.Nanosecond,
)) ))
} }