Skip to content

Commit 28775a6

Browse files
committed
Use Go 1.21 built-in min function
Signed-off-by: Xiaobo Liu <[email protected]>
1 parent ff2807d commit 28775a6

File tree

9 files changed

+12
-89
lines changed

9 files changed

+12
-89
lines changed

pkg/flexfec/flexfec_decoder_03.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (d *fecDecoder) recoverPacket(fec *fecPacketState) (rtp.Packet, error) {
283283
if err != nil {
284284
return rtp.Packet{}, fmt.Errorf("marshal protected packet: %w", err)
285285
}
286-
for i := 0; i < minInt(int(payloadLength), len(packet)-12); i++ {
286+
for i := 0; i < min(int(payloadLength), len(packet)-12); i++ {
287287
payloadRecovery[i] ^= packet[12+i]
288288
}
289289
}
@@ -407,23 +407,7 @@ func parseFlexFEC03Header(data []byte) (flexFec, error) {
407407
}
408408

409409
func seqDiff(a, b uint16) uint16 {
410-
return minUInt16(a-b, b-a)
411-
}
412-
413-
func minInt(a, b int) int {
414-
if a < b {
415-
return a
416-
}
417-
418-
return b
419-
}
420-
421-
func minUInt16(a, b uint16) uint16 {
422-
if a < b {
423-
return a
424-
}
425-
426-
return b
410+
return min(a-b, b-a)
427411
}
428412

429413
func abs(x int) int {

pkg/gcc/adaptive_threshold.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (a *adaptiveThreshold) compare(estimate, _ time.Duration) (usage, time.Dura
6666
if a.numDeltas < 2 {
6767
return usageNormal, estimate, a.max
6868
}
69-
t := time.Duration(minInt(a.numDeltas, maxDeltas)) * estimate
69+
t := time.Duration(min(a.numDeltas, maxDeltas)) * estimate
7070
use := usageNormal
7171
if t > a.thresh {
7272
use = usageOver
@@ -96,7 +96,7 @@ func (a *adaptiveThreshold) update(estimate time.Duration) {
9696
}
9797
maxTimeDelta := 100 * time.Millisecond
9898
timeDelta := time.Duration(
99-
minInt(int(now.Sub(a.lastUpdate).Milliseconds()), int(maxTimeDelta.Milliseconds())),
99+
min(int(now.Sub(a.lastUpdate).Milliseconds()), int(maxTimeDelta.Milliseconds())),
100100
) * time.Millisecond
101101
d := absEstimate - a.thresh
102102
add := k * float64(d.Milliseconds()) * float64(timeDelta.Milliseconds())

pkg/gcc/gcc.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ package gcc
66

77
import "time"
88

9-
func minInt(a, b int) int {
10-
if a < b {
11-
return a
12-
}
13-
14-
return b
15-
}
16-
179
func maxInt(a, b int) int {
1810
if a > b {
1911
return a
@@ -23,7 +15,7 @@ func maxInt(a, b int) int {
2315
}
2416

2517
func clampInt(b, minVal, maxVal int) int {
26-
return maxInt(minVal, minInt(maxVal, b))
18+
return maxInt(minVal, min(maxVal, b))
2719
}
2820

2921
func clampDuration(d, minVal, maxVal time.Duration) time.Duration {

pkg/gcc/gcc_test.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,6 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14-
func TestMinInt(t *testing.T) {
15-
tests := []struct {
16-
expected int
17-
a, b int
18-
}{
19-
{
20-
expected: 0,
21-
a: 0,
22-
b: 100,
23-
},
24-
{
25-
expected: 10,
26-
a: 10,
27-
b: 10,
28-
},
29-
{
30-
expected: 1,
31-
a: 10,
32-
b: 1,
33-
},
34-
}
35-
for i, tt := range tests {
36-
tt := tt
37-
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
38-
assert.Equal(t, tt.expected, minInt(tt.a, tt.b))
39-
})
40-
}
41-
}
42-
4314
func TestMaxInt(t *testing.T) {
4415
tests := []struct {
4516
expected int

pkg/gcc/loss_based_bwe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (e *lossBasedBandwidthEstimator) getEstimate(wantedRate int) LossStats {
6363
if e.bitrate <= 0 {
6464
e.bitrate = clampInt(wantedRate, e.minBitrate, e.maxBitrate)
6565
}
66-
e.bitrate = minInt(wantedRate, e.bitrate)
66+
e.bitrate = min(wantedRate, e.bitrate)
6767

6868
return LossStats{
6969
TargetBitrate: e.bitrate,

pkg/gcc/send_side_bwe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (e *SendSideBWE) WriteRTCP(pkts []rtcp.Packet, _ interceptor.Attributes) er
210210
}
211211
pendingTime := feedbackSentTime.Sub(ack.Arrival)
212212
rtt := now.Sub(ack.Departure) - pendingTime
213-
feedbackMinRTT = time.Duration(minInt(int(rtt), int(feedbackMinRTT)))
213+
feedbackMinRTT = time.Duration(min(int(rtt), int(feedbackMinRTT)))
214214
}
215215
if feedbackMinRTT < math.MaxInt {
216216
e.delayController.updateRTT(feedbackMinRTT)
@@ -283,7 +283,7 @@ func (e *SendSideBWE) onDelayUpdate(delayStats DelayStats) {
283283

284284
lossStats := e.lossController.getEstimate(delayStats.TargetBitrate)
285285
bitrateChanged := false
286-
bitrate := minInt(delayStats.TargetBitrate, lossStats.TargetBitrate)
286+
bitrate := min(delayStats.TargetBitrate, lossStats.TargetBitrate)
287287
if bitrate != e.latestBitrate {
288288
bitrateChanged = true
289289
e.latestBitrate = bitrate

pkg/stats/stats_recorder.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (r *recorder) recordIncomingRR(latestStats internalStats, pkt *rtcp.Receive
249249
latestStats.RemoteInboundRTPStreamStats.Jitter = float64(report.Jitter) / r.clockRate
250250

251251
if report.Delay != 0 && report.LastSenderReport != 0 {
252-
for i := minInt(r.maxLastSenderReports, len(latestStats.lastSenderReports)) - 1; i >= 0; i-- {
252+
for i := min(r.maxLastSenderReports, len(latestStats.lastSenderReports)) - 1; i >= 0; i-- {
253253
lastReport := latestStats.lastSenderReports[i]
254254
if (lastReport&0x0000FFFFFFFF0000)>>16 == uint64(report.LastSenderReport) {
255255
dlsr := time.Duration(float64(report.Delay) / 65536.0 * float64(time.Second))
@@ -272,7 +272,7 @@ func (r *recorder) recordIncomingXR(latestStats internalStats, pkt *rtcp.Extende
272272
if xr, ok := report.(*rtcp.DLRRReportBlock); ok {
273273
for _, xrReport := range xr.Reports {
274274
if xrReport.LastRR != 0 && xrReport.DLRR != 0 {
275-
for i := minInt(r.maxLastReceiverReferenceTimes, len(latestStats.lastReceiverReferenceTimes)) - 1; i >= 0; i-- {
275+
for i := min(r.maxLastReceiverReferenceTimes, len(latestStats.lastReceiverReferenceTimes)) - 1; i >= 0; i-- {
276276
lastRR := latestStats.lastReceiverReferenceTimes[i]
277277
if (lastRR&0x0000FFFFFFFF0000)>>16 == uint64(xrReport.LastRR) {
278278
dlrr := time.Duration(float64(xrReport.DLRR) / 65536.0 * float64(time.Second))
@@ -407,11 +407,3 @@ func (r *recorder) QueueOutgoingRTCP(ts time.Time, pkts []rtcp.Packet, attr inte
407407
})
408408
r.ms.Unlock()
409409
}
410-
411-
func minInt(a, b int) int {
412-
if a < b {
413-
return a
414-
}
415-
416-
return b
417-
}

pkg/twcc/arrival_time_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (m *packetArrivalTimeMap) EraseTo(sequenceNumber int64) {
136136
// RemoveOldPackets removes packets from the beginning of the map as long as they are before
137137
// sequenceNumber and with an age older than arrivalTimeLimit.
138138
func (m *packetArrivalTimeMap) RemoveOldPackets(sequenceNumber int64, arrivalTimeLimit int64) {
139-
checkTo := min64(sequenceNumber, m.endSequenceNumber)
139+
checkTo := min(sequenceNumber, m.endSequenceNumber)
140140
for m.beginSequenceNumber < checkTo && m.get(m.beginSequenceNumber) <= arrivalTimeLimit {
141141
m.beginSequenceNumber++
142142
}

pkg/twcc/twcc.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (c *chunk) encode() rtcp.PacketStatusChunk {
318318
}
319319
}
320320

321-
minCap := minInt(maxTwoBitCap, len(c.deltas))
321+
minCap := min(maxTwoBitCap, len(c.deltas))
322322
svc := &rtcp.StatusVectorChunk{
323323
SymbolSize: rtcp.TypeTCCSymbolSizeTwoBit,
324324
SymbolList: c.deltas[:minCap],
@@ -356,26 +356,10 @@ func maxInt(a, b int) int {
356356
return b
357357
}
358358

359-
func minInt(a, b int) int {
360-
if a < b {
361-
return a
362-
}
363-
364-
return b
365-
}
366-
367359
func max64(a, b int64) int64 {
368360
if a > b {
369361
return a
370362
}
371363

372364
return b
373365
}
374-
375-
func min64(a, b int64) int64 {
376-
if a < b {
377-
return a
378-
}
379-
380-
return b
381-
}

0 commit comments

Comments
 (0)