perf: reduce time.Now overhead

This commit is contained in:
Toby 2022-05-06 00:11:07 -07:00
parent 65fcdd51bc
commit 95d8ea49cc
2 changed files with 13 additions and 13 deletions

View File

@ -33,8 +33,8 @@ func NewBrutalSender(bps congestion.ByteCount) *BrutalSender {
bps: bps,
maxDatagramSize: initMaxDatagramSize,
}
bs.pacer = newPacer(func() congestion.ByteCount {
return congestion.ByteCount(float64(bs.bps) / bs.getAckRate())
bs.pacer = newPacer(func(now time.Time) congestion.ByteCount {
return congestion.ByteCount(float64(bs.bps) / bs.getAckRate(now))
})
return bs
}
@ -60,7 +60,7 @@ func (b *BrutalSender) GetCongestionWindow() congestion.ByteCount {
if rtt <= 0 {
return 10240
}
return congestion.ByteCount(float64(b.bps) * rtt.Seconds() * 1.5 / b.getAckRate())
return congestion.ByteCount(float64(b.bps) * rtt.Seconds() * 1.5 / b.getAckRate(time.Now()))
}
func (b *BrutalSender) OnPacketSent(sentTime time.Time, bytesInFlight congestion.ByteCount,
@ -101,8 +101,7 @@ func (b *BrutalSender) SetMaxDatagramSize(size congestion.ByteCount) {
b.pacer.SetMaxDatagramSize(size)
}
func (b *BrutalSender) getAckRate() float64 {
now := time.Now()
func (b *BrutalSender) getAckRate(now time.Time) float64 {
currentTimestamp := now.Unix()
minTimestamp := currentTimestamp - pktInfoSlotCount
var ackCount, lossCount uint64

View File

@ -16,10 +16,10 @@ type pacer struct {
budgetAtLastSent congestion.ByteCount
maxDatagramSize congestion.ByteCount
lastSentTime time.Time
getBandwidth func() congestion.ByteCount // in bytes/s
getBandwidth func(time.Time) congestion.ByteCount // in bytes/s
}
func newPacer(getBandwidth func() congestion.ByteCount) *pacer {
func newPacer(getBandwidth func(time.Time) congestion.ByteCount) *pacer {
p := &pacer{
budgetAtLastSent: maxBurstPackets * 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 {
if p.lastSentTime.IsZero() {
return p.maxBurstSize()
return p.maxBurstSize(now)
}
budget := p.budgetAtLastSent + (p.getBandwidth()*congestion.ByteCount(now.Sub(p.lastSentTime).Nanoseconds()))/1e9
return minByteCount(p.maxBurstSize(), budget)
budget := p.budgetAtLastSent + (p.getBandwidth(now)*congestion.ByteCount(now.Sub(p.lastSentTime).Nanoseconds()))/1e9
return minByteCount(p.maxBurstSize(now), budget)
}
func (p *pacer) maxBurstSize() congestion.ByteCount {
func (p *pacer) maxBurstSize(now time.Time) congestion.ByteCount {
return maxByteCount(
congestion.ByteCount((minPacingDelay+time.Millisecond).Nanoseconds())*p.getBandwidth()/1e9,
congestion.ByteCount((minPacingDelay+time.Millisecond).Nanoseconds())*p.getBandwidth(now)/1e9,
maxBurstPackets*p.maxDatagramSize,
)
}
@ -61,7 +61,8 @@ func (p *pacer) TimeUntilSend() time.Time {
}
return p.lastSentTime.Add(maxDuration(
minPacingDelay,
time.Duration(math.Ceil(float64(p.maxDatagramSize-p.budgetAtLastSent)*1e9/float64(p.getBandwidth())))*time.Nanosecond,
time.Duration(math.Ceil(float64(p.maxDatagramSize-p.budgetAtLastSent)*1e9/
float64(p.getBandwidth(time.Now()))))*time.Nanosecond,
))
}