Sequencer small optimisations (#2049)

* Sequencer small optimisations

1. Use range map to exclude padding only packets. Should take lesser
   space as we are not using slice to hold pointer to actual data.
2. Avoid `time.Now()` when adding each packet. Just use the arrival time
   as it should be close enough. `time.Now()` was showing up in
   profile.

* remove debug

* correct comment
This commit is contained in:
Raja Subramanian
2023-09-08 23:48:54 +05:30
committed by GitHub
parent 83353d821c
commit 492eb3bf18
7 changed files with 391 additions and 244 deletions
+133 -23
View File
@@ -25,15 +25,15 @@ import (
)
func Test_sequencer(t *testing.T) {
seq := newSequencer(500, 0, logger.GetLogger())
seq := newSequencer(500, false, logger.GetLogger())
off := uint16(15)
for i := uint16(1); i < 518; i++ {
seq.push(i, i+off, 123, true, 2, nil, nil)
for i := uint64(1); i < 518; i++ {
seq.push(time.Now(), i, i+uint64(off), 123, true, 2, nil, nil)
}
// send the last two out-of-order
seq.push(519, 519+off, 123, false, 2, nil, nil)
seq.push(518, 518+off, 123, true, 2, nil, nil)
seq.push(time.Now(), 519, 519+uint64(off), 123, false, 2, nil, nil)
seq.push(time.Now(), 518, 518+uint64(off), 123, true, 2, nil, nil)
req := []uint16{57, 58, 62, 63, 513, 514, 515, 516, 517}
res := seq.getPacketsMeta(req)
@@ -59,14 +59,14 @@ func Test_sequencer(t *testing.T) {
require.Equal(t, val.layer, int8(2))
}
seq.push(521, 521+off, 123, true, 1, nil, nil)
seq.push(time.Now(), 521, 521+uint64(off), 123, true, 1, nil, nil)
m := seq.getPacketsMeta([]uint16{521 + off})
require.Equal(t, 0, len(m))
time.Sleep((ignoreRetransmission + 10) * time.Millisecond)
m = seq.getPacketsMeta([]uint16{521 + off})
require.Equal(t, 1, len(m))
seq.push(505, 505+off, 123, false, 1, nil, nil)
seq.push(time.Now(), 505, 505+uint64(off), 123, false, 1, nil, nil)
m = seq.getPacketsMeta([]uint16{505 + off})
require.Equal(t, 0, len(m))
time.Sleep((ignoreRetransmission + 10) * time.Millisecond)
@@ -74,14 +74,17 @@ func Test_sequencer(t *testing.T) {
require.Equal(t, 1, len(m))
}
func Test_sequencer_getNACKSeqNo(t *testing.T) {
func Test_sequencer_getNACKSeqNo_exclusion(t *testing.T) {
type args struct {
seqNo []uint16
}
type input struct {
seqNo uint64
isPadding bool
}
type fields struct {
input []uint16
padding []uint16
offset uint16
inputs []input
offset uint64
markerOdd bool
markerEven bool
codecBytesOdd []byte
@@ -99,8 +102,17 @@ func Test_sequencer_getNACKSeqNo(t *testing.T) {
{
name: "Should get correct seq numbers",
fields: fields{
input: []uint16{2, 3, 4, 7, 8, 11},
padding: []uint16{9, 10},
inputs: []input{
{65526, false},
{65524, false},
{65525, false},
{65529, false},
{65530, false},
{65531, true},
{65533, false},
{65532, true},
{65534, false},
},
offset: 5,
markerOdd: true,
markerEven: false,
@@ -110,26 +122,124 @@ func Test_sequencer_getNACKSeqNo(t *testing.T) {
ddBytesEven: []byte{11, 12},
},
args: args{
seqNo: []uint16{4 + 5, 5 + 5, 8 + 5, 9 + 5, 10 + 5, 11 + 5},
seqNo: []uint16{65526 + 5, 65527 + 5, 65530 + 5, 0 /* 65531 input */, 1 /* 65532 input */, 2 /* 65533 input */, 3 /* 65534 input */},
},
want: []uint16{4, 8, 11},
// although 65526 is originally pushed, that would have been reset by 65532 (padding only packet)
// because of trying to add an exclusion range before highest sequence number which will fail
// and the resulting fix up of the exclusion range slots
want: []uint16{65530, 65533, 65534},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
n := newSequencer(5, 10, logger.GetLogger())
n := newSequencer(5, true, logger.GetLogger())
for _, i := range tt.fields.input {
if i%2 == 0 {
n.push(i, i+tt.fields.offset, 123, tt.fields.markerEven, 3, tt.fields.codecBytesEven, tt.fields.ddBytesEven)
for _, i := range tt.fields.inputs {
if i.isPadding {
n.pushPadding(i.seqNo+tt.fields.offset, i.seqNo+tt.fields.offset)
} else {
n.push(i, i+tt.fields.offset, 123, tt.fields.markerOdd, 3, tt.fields.codecBytesOdd, tt.fields.ddBytesOdd)
if i.seqNo%2 == 0 {
n.push(time.Now(), i.seqNo, i.seqNo+tt.fields.offset, 123, tt.fields.markerEven, 3, tt.fields.codecBytesEven, tt.fields.ddBytesEven)
} else {
n.push(time.Now(), i.seqNo, i.seqNo+tt.fields.offset, 123, tt.fields.markerOdd, 3, tt.fields.codecBytesOdd, tt.fields.ddBytesOdd)
}
}
}
time.Sleep((ignoreRetransmission + 10) * time.Millisecond)
g := n.getPacketsMeta(tt.args.seqNo)
var got []uint16
for _, sn := range g {
got = append(got, sn.sourceSeqNo)
if sn.sourceSeqNo%2 == 0 {
require.Equal(t, tt.fields.markerEven, sn.marker)
require.Equal(t, tt.fields.codecBytesEven, sn.codecBytes)
require.Equal(t, tt.fields.ddBytesEven, sn.ddBytes)
} else {
require.Equal(t, tt.fields.markerOdd, sn.marker)
require.Equal(t, tt.fields.codecBytesOdd, sn.codecBytes)
require.Equal(t, tt.fields.ddBytesOdd, sn.ddBytes)
}
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getPacketsMeta() = %v, want %v", got, tt.want)
}
})
}
}
func Test_sequencer_getNACKSeqNo_no_exclusion(t *testing.T) {
type args struct {
seqNo []uint16
}
type input struct {
seqNo uint64
isPadding bool
}
type fields struct {
inputs []input
offset uint64
markerOdd bool
markerEven bool
codecBytesOdd []byte
codecBytesEven []byte
ddBytesOdd []byte
ddBytesEven []byte
}
tests := []struct {
name string
fields fields
args args
want []uint16
}{
{
name: "Should get correct seq numbers",
fields: fields{
inputs: []input{
{2, false},
{3, false},
{4, false},
{7, false},
{8, false},
{9, true},
{11, false},
{10, true},
{12, false},
{13, false},
},
offset: 5,
markerOdd: true,
markerEven: false,
codecBytesOdd: []byte{1, 2, 3, 4},
codecBytesEven: []byte{5, 6, 7},
ddBytesOdd: []byte{8, 9, 10},
ddBytesEven: []byte{11, 12},
},
args: args{
seqNo: []uint16{4 + 5, 5 + 5, 8 + 5, 9 + 5, 10 + 5, 11 + 5, 12 + 5},
},
// although 4 and 8 were originally added, they would be too old after a cycle of sequencer buffer
want: []uint16{11, 12},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
n := newSequencer(5, false, logger.GetLogger())
for _, i := range tt.fields.inputs {
if i.isPadding {
n.pushPadding(i.seqNo+tt.fields.offset, i.seqNo+tt.fields.offset)
} else {
if i.seqNo%2 == 0 {
n.push(time.Now(), i.seqNo, i.seqNo+tt.fields.offset, 123, tt.fields.markerEven, 3, tt.fields.codecBytesEven, tt.fields.ddBytesEven)
} else {
n.push(time.Now(), i.seqNo, i.seqNo+tt.fields.offset, 123, tt.fields.markerOdd, 3, tt.fields.codecBytesOdd, tt.fields.ddBytesOdd)
}
}
}
for _, i := range tt.fields.padding {
n.pushPadding(i + tt.fields.offset)
}
time.Sleep((ignoreRetransmission + 10) * time.Millisecond)
g := n.getPacketsMeta(tt.args.seqNo)