chore: bitlist use uint16 as underlay

This commit is contained in:
mzz2017
2023-02-20 18:24:29 +08:00
parent 9fd52c4d38
commit f412f9eeac
3 changed files with 200 additions and 38 deletions

View File

@ -8,8 +8,7 @@ package bitlist
import (
"fmt"
"github.com/mzz2017/softwind/common"
"github.com/mzz2017/softwind/pkg/zeroalloc/buffer"
"github.com/mzz2017/softwind/pool"
"github.com/v2rayA/dae/pkg/anybuffer"
"math/bits"
)
@ -17,7 +16,7 @@ import (
type CompactBitList struct {
unitBitSize int
size int
b *buffer.Buffer
b *anybuffer.Buffer[uint16]
unitNum int
}
@ -25,7 +24,7 @@ func NewCompactBitList(unitBitSize int) *CompactBitList {
return &CompactBitList{
unitBitSize: unitBitSize,
size: 0,
b: buffer.NewBuffer(1),
b: anybuffer.NewBuffer[uint16](1),
}
}
@ -35,14 +34,14 @@ func (m *CompactBitList) Set(iUnit int, v uint64) {
panic(fmt.Sprintf("value %v exceeds unit bit size", v))
}
m.growByUnitIndex(iUnit)
b := m.b.Bytes()
i := iUnit * m.unitBitSize / 8
j := iUnit * m.unitBitSize % 8
for unitToTravel := m.unitBitSize; unitToTravel > 0; unitToTravel -= 8 {
b := m.b.Slice()
i := iUnit * m.unitBitSize / 16
j := iUnit * m.unitBitSize % 16
for unitToTravel := m.unitBitSize; unitToTravel > 0; unitToTravel -= 16 {
k := 0
for ; k < unitToTravel && j+k < 8; k++ {
for ; k < unitToTravel && j+k < 16; k++ {
b[i] &= ^(1 << (k + j)) // clear bit.
val := uint8((v & (1 << k)) << j)
val := uint16((v & (1 << k)) << j)
b[i] |= val // set bit.
}
// Now unitBitSize is traveled and we should break the loop,
@ -53,29 +52,29 @@ func (m *CompactBitList) Set(iUnit int, v uint64) {
i++
bakJ := j
j = k
for ; k < unitToTravel && k < 8; k++ {
for ; k < unitToTravel && k < 16; k++ {
b[i] &= ^(1 << (k - j)) // clear bit.
val := uint8((v & (1 << k)) >> j)
val := uint16((v & (1 << k)) >> j)
b[i] |= val // set bit.
}
v >>= 8
j = (bakJ + 8) % 8
v >>= 16
j = (bakJ + 16) % 16
}
m.unitNum = common.Max(m.unitNum, iUnit+1)
}
func (m *CompactBitList) Get(iUnit int) (v uint64) {
bitBoundary := (iUnit + 1) * m.unitBitSize
if m.b.Len()*8 < bitBoundary {
if m.b.Len()*16 < bitBoundary {
return 0
}
b := m.b.Bytes()
i := iUnit * m.unitBitSize / 8
j := iUnit * m.unitBitSize % 8
b := m.b.Slice()
i := iUnit * m.unitBitSize / 16
j := iUnit * m.unitBitSize % 16
var val uint8
byteSpace := 8 - j
var val uint16
byteSpace := 16 - j
// 11111111
// |
// j byteSpace = 6, unitBitSize = 2
@ -89,15 +88,15 @@ func (m *CompactBitList) Get(iUnit int) (v uint64) {
}
v |= uint64(val)
offset := 8 - j
offset := 16 - j
i++
// Now we have multiple of 8 bits spaces to move.
// Now we have multiple of 16 bits spaces to move.
unitToTravel := m.unitBitSize - offset
for ; unitToTravel >= 8; unitToTravel, i, offset = unitToTravel-8, i+1, offset+8 {
for ; unitToTravel >= 16; unitToTravel, i, offset = unitToTravel-16, i+1, offset+16 {
// 11111111
// |
// p
// 11111111 We copy whole 8 bits
// 11111111 We copy whole 16 bits
v |= uint64(b[i]) << offset
}
if unitToTravel == 0 {
@ -108,7 +107,7 @@ func (m *CompactBitList) Get(iUnit int) (v uint64) {
// |
// p unitToTravel = 3
// 111 We only copy those 3 bits, so we left shift 5 and right shift 5.
toTrimLeft := 8 - unitToTravel
toTrimLeft := 16 - unitToTravel
if offset > toTrimLeft {
v |= uint64(b[i]<<toTrimLeft) << (offset - toTrimLeft)
} else {
@ -122,9 +121,9 @@ func (m *CompactBitList) Append(v uint64) {
}
func (m *CompactBitList) growByUnitIndex(i int) {
if bitBoundary := (i + 1) * m.unitBitSize; m.b.Len()*8 < bitBoundary {
needBytes := bitBoundary / 8
if bitBoundary%8 != 0 {
if bitBoundary := (i + 1) * m.unitBitSize; m.b.Len()*16 < bitBoundary {
needBytes := bitBoundary / 16
if bitBoundary%16 != 0 {
needBytes++
}
m.b.Extend(needBytes - m.b.Len())
@ -132,12 +131,7 @@ func (m *CompactBitList) growByUnitIndex(i int) {
}
func (m *CompactBitList) Tighten() {
a := pool.B(make([]byte, m.b.Len()))
copy(a, m.b.Bytes())
m.b.Put()
m.b = buffer.NewBufferFrom(a)
}
func (m *CompactBitList) Put() {
m.b.Put()
a := make([]uint16, m.b.Len())
copy(a, m.b.Slice())
m.b = anybuffer.NewBufferFrom(a)
}