1
Fork 0

Make packet cache cache-friendly.

This commit is contained in:
Juliusz Chroboczek 2020-05-20 19:36:33 +02:00
parent 464bad0781
commit a6ff98a313
2 changed files with 16 additions and 4 deletions

View File

@ -8,8 +8,9 @@ const BufSize = 1500
type entry struct {
seqno uint16
length int
length uint16
buf [BufSize]byte
pad [32 - (BufSize+4)%32]byte // avoid false sharing
}
type Cache struct {
@ -99,7 +100,7 @@ func (cache *Cache) Store(seqno uint16, buf []byte) uint16 {
cache.entries[cache.tail].seqno = seqno
copy(cache.entries[cache.tail].buf[:], buf)
cache.entries[cache.tail].length = len(buf)
cache.entries[cache.tail].length = uint16(len(buf))
cache.tail = (cache.tail + 1) % len(cache.entries)
return cache.first

View File

@ -4,6 +4,7 @@ import (
"bytes"
"math/rand"
"testing"
"unsafe"
"github.com/pion/rtcp"
)
@ -54,6 +55,16 @@ func TestCacheOverflow(t *testing.T) {
}
}
func TestCacheAlignment(t *testing.T) {
cache := New(16)
for i := range cache.entries {
p := unsafe.Pointer(&cache.entries[i])
if uintptr(p) % 32 != 0 {
t.Errorf("%v: alignment %v", i, uintptr(p) % 32)
}
}
}
func TestBitmap(t *testing.T) {
value := uint64(0xcdd58f1e035379c0)
packet := make([]byte, 1)