Skip to content

Commit

Permalink
Remove third-party dequeue
Browse files Browse the repository at this point in the history
Remove third-party dequeue
  • Loading branch information
cnderrauber committed Jul 1, 2024
1 parent 7be9ead commit 7bb55be
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 7 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module github.com/pion/sctp

require (
github.com/gammazero/deque v0.2.1
github.com/pion/logging v0.2.2
github.com/pion/randutil v0.1.0
github.com/pion/transport/v3 v3.0.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0=
github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
6 changes: 2 additions & 4 deletions payload_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@

package sctp

import "github.com/gammazero/deque"

type payloadQueue struct {
chunks *deque.Deque[*chunkPayloadData]
chunks *queue[*chunkPayloadData]
nBytes int
}

func newPayloadQueue() *payloadQueue {
return &payloadQueue{chunks: deque.New[*chunkPayloadData](128)}
return &payloadQueue{chunks: newQueue[*chunkPayloadData](128)}
}

func (q *payloadQueue) pushNoCheck(p *chunkPayloadData) {
Expand Down
78 changes: 78 additions & 0 deletions queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package sctp

import "fmt"

type queue[T any] struct {
buf []T
head int
tail int
count int
}

const minCap = 16

func newQueue[T any](capacity int) *queue[T] {
queueCap := minCap
for queueCap < capacity {
queueCap <<= 1
}

return &queue[T]{
buf: make([]T, queueCap),
}
}

func (q *queue[T]) Len() int {
return q.count
}

func (q *queue[T]) PushBack(ele T) {
q.growIfFull()
q.buf[q.tail] = ele
q.tail = (q.tail + 1) % len(q.buf)
q.count++
}

func (q *queue[T]) PopFront() T {
if q.count <= 0 {
panic("PopFront() called on empty queue")

Check failure on line 38 in queue.go

View workflow job for this annotation

GitHub Actions / lint / Go

use of `panic` forbidden by pattern `^panic$` (forbidigo)
}
ele := q.buf[q.head]
var zeroVal T
q.buf[q.head] = zeroVal
q.head = (q.head + 1) % len(q.buf)
q.count--
return ele
}

func (q *queue[T]) Front() T {
if q.count <= 0 {
panic("Front() called on empty queue")

Check failure on line 50 in queue.go

View workflow job for this annotation

GitHub Actions / lint / Go

use of `panic` forbidden by pattern `^panic$` (forbidigo)
}
return q.buf[q.head]
}

func (q *queue[T]) At(i int) T {
if i < 0 || i >= q.count {
panic(fmt.Sprintf("index %d out of range %d", i, q.count))

Check failure on line 57 in queue.go

View workflow job for this annotation

GitHub Actions / lint / Go

use of `panic` forbidden by pattern `^panic$` (forbidigo)
}
return q.buf[(q.head+i)%(len(q.buf))]
}

func (q *queue[T]) growIfFull() {
if q.count < len(q.buf) {
return
}

newBuf := make([]T, q.count<<1)
if q.tail > q.head {
copy(newBuf, q.buf[q.head:q.tail])

Check warning on line 69 in queue.go

View check run for this annotation

Codecov / codecov/patch

queue.go#L69

Added line #L69 was not covered by tests
} else {
n := copy(newBuf, q.buf[q.head:])
copy(newBuf[n:], q.buf[:q.tail])
}

q.head = 0
q.tail = q.count
q.buf = newBuf
}
48 changes: 48 additions & 0 deletions queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sctp

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestQueue(t *testing.T) {
q := newQueue[int](32)
assert.Panics(t, func() { q.PopFront() })
assert.Panics(t, func() { q.Front() })
assert.Zero(t, q.Len())

// test push & pop
for i := 1; i < 33; i++ {
q.PushBack(i)
}
assert.Equal(t, 32, q.Len())
assert.Equal(t, 5, q.At(4))
for i := 1; i < 33; i++ {
assert.Equal(t, i, q.Front())
assert.Equal(t, i, q.PopFront())
}
assert.Zero(t, q.Len())

q.PushBack(10)
q.PushBack(11)
assert.Equal(t, 2, q.Len())
assert.Equal(t, 11, q.At(1))
assert.Equal(t, 10, q.Front())
assert.Panics(t, func() {
q.At(33)
})
assert.Equal(t, 10, q.PopFront())
assert.Equal(t, 11, q.PopFront())

// test grow capacity
for i := 0; i < 64; i++ {
q.PushBack(i)
}
assert.Equal(t, 64, q.Len())
assert.Equal(t, 2, q.At(2))
for i := 0; i < 64; i++ {
assert.Equal(t, i, q.Front())
assert.Equal(t, i, q.PopFront())
}
}

0 comments on commit 7bb55be

Please sign in to comment.