mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-11 00:09:18 +07:00
chore: bitlist use uint16 as underlay
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ func (b *RoutingMatcherBuilder) OutboundToId(outbound string) uint8 {
|
||||
return uint8(h.Sum64() & 0xFF)
|
||||
}
|
||||
|
||||
func (b *RoutingMatcherBuilder) AddDomain(f *config_parser.Function, key string, values []string, outbound string) {
|
||||
func (b *RoutingMatcherBuilder) AddDomain(f *config_parser.Function, key string, values []string, outbound *routing.Outbound) {
|
||||
if b.err != nil {
|
||||
return
|
||||
}
|
||||
@ -141,7 +141,7 @@ routing {
|
||||
ip(geoip:private) -> direct
|
||||
ip(geoip:cn) -> direct
|
||||
domain(geosite:cn) -> direct
|
||||
final: my_group
|
||||
fallback: my_group
|
||||
}`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
168
pkg/anybuffer/anybuffer.go
Normal file
168
pkg/anybuffer/anybuffer.go
Normal file
@ -0,0 +1,168 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) 2023, v2rayA Organization <team@v2raya.org>
|
||||
*/
|
||||
|
||||
package anybuffer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
var smallBufferSize = 16
|
||||
|
||||
// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
|
||||
// The zero value for Buffer is an empty buffer ready to use.
|
||||
type Buffer[T constraints.Unsigned] struct {
|
||||
buf []T // contents are the bytes buf[off : len(buf)]
|
||||
}
|
||||
|
||||
// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
|
||||
var ErrTooLarge = errors.New("bytes.Buffer: too large")
|
||||
|
||||
const maxInt = int(^uint(0) >> 1)
|
||||
|
||||
// Slice returns a slice of length b.Len() holding the unread portion of the buffer.
|
||||
// The slice is valid for use only until the next buffer modification (that is,
|
||||
// only until the next call to a method like Read, Write, Reset, or Truncate).
|
||||
// The slice aliases the buffer content at least until the next buffer modification,
|
||||
// so immediate changes to the slice will affect the result of future reads.
|
||||
func (b *Buffer[T]) Slice() []T { return b.buf }
|
||||
|
||||
// empty reports whether the unread portion of the buffer is empty.
|
||||
func (b *Buffer[T]) empty() bool { return len(b.buf) <= 0 }
|
||||
|
||||
// Len returns the number of bytes of the unread portion of the buffer;
|
||||
// b.Len() == len(b.Slice()).
|
||||
func (b *Buffer[T]) Len() int { return len(b.buf) - 0 }
|
||||
|
||||
// Cap returns the capacity of the buffer's underlying byte slice, that is, the
|
||||
// total space allocated for the buffer's data.
|
||||
func (b *Buffer[T]) Cap() int { return cap(b.buf) }
|
||||
|
||||
// Truncate discards all but the first n unread bytes from the buffer
|
||||
// but continues to use the same allocated storage.
|
||||
// It panics if n is negative or greater than the length of the buffer.
|
||||
func (b *Buffer[T]) Truncate(n int) {
|
||||
if n == 0 {
|
||||
b.Reset()
|
||||
return
|
||||
}
|
||||
if n < 0 || n > b.Len() {
|
||||
panic("bytes.Buffer: truncation out of range")
|
||||
}
|
||||
b.buf = b.buf[:0+n]
|
||||
}
|
||||
|
||||
// Reset resets the buffer to be empty,
|
||||
// but it retains the underlying storage for use by future writes.
|
||||
// Reset is the same as Truncate(0).
|
||||
func (b *Buffer[T]) Reset() {
|
||||
b.buf = b.buf[:0]
|
||||
}
|
||||
|
||||
// tryGrowByReslice is a inlineable version of grow for the fast-case where the
|
||||
// internal buffer only needs to be resliced.
|
||||
// It returns the index where bytes should be written and whether it succeeded.
|
||||
func (b *Buffer[T]) tryGrowByReslice(n int) (int, bool) {
|
||||
if l := len(b.buf); n <= cap(b.buf)-l {
|
||||
b.buf = b.buf[:l+n]
|
||||
return l, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// grow grows the buffer to guarantee space for n more bytes.
|
||||
// It returns the index where bytes should be written.
|
||||
// If the buffer can't grow it will panic with ErrTooLarge.
|
||||
func (b *Buffer[T]) grow(n int) int {
|
||||
m := b.Len()
|
||||
// Try to grow by means of a reslice.
|
||||
if i, ok := b.tryGrowByReslice(n); ok {
|
||||
return i
|
||||
}
|
||||
if b.buf == nil && n <= smallBufferSize {
|
||||
b.buf = make([]T, n, smallBufferSize)
|
||||
return 0
|
||||
}
|
||||
c := cap(b.buf)
|
||||
if n <= c/2-m {
|
||||
// We can slide things down instead of allocating a new
|
||||
// slice. We only need m+n <= c to slide, but
|
||||
// we instead let capacity get twice as large so we
|
||||
// don't spend all our time copying.
|
||||
} else if c > maxInt-c-n {
|
||||
panic(ErrTooLarge)
|
||||
} else {
|
||||
// Not enough space anywhere, we need to allocate.
|
||||
buf := makeSlice[T](2*c + n)
|
||||
copy(buf, b.buf)
|
||||
b.buf = buf
|
||||
}
|
||||
// Restore len(b.buf).
|
||||
b.buf = b.buf[:m+n]
|
||||
return m
|
||||
}
|
||||
|
||||
// Grow grows the buffer's capacity, if necessary, to guarantee space for
|
||||
// another n bytes. After Grow(n), at least n bytes can be written to the
|
||||
// buffer without another allocation.
|
||||
// If n is negative, Grow will panic.
|
||||
// If the buffer can't grow it will panic with ErrTooLarge.
|
||||
func (b *Buffer[T]) Grow(n int) {
|
||||
if n < 0 {
|
||||
panic("bytes.Buffer.Grow: negative count")
|
||||
}
|
||||
m := b.grow(n)
|
||||
b.buf = b.buf[:m]
|
||||
}
|
||||
|
||||
// Extend extends the Buffer.Len() by n.
|
||||
func (b *Buffer[T]) Extend(n int) {
|
||||
b.extend(n)
|
||||
}
|
||||
|
||||
func (b *Buffer[T]) extend(n int) int {
|
||||
m, ok := b.tryGrowByReslice(n)
|
||||
if !ok {
|
||||
m = b.grow(n)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// makeSlice allocates a slice of size n. If the allocation fails, it panics
|
||||
// with ErrTooLarge.
|
||||
func makeSlice[T constraints.Unsigned](n int) []T {
|
||||
// If the make fails, give a known error.
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
panic(ErrTooLarge)
|
||||
}
|
||||
}()
|
||||
return make([]T, n)
|
||||
}
|
||||
|
||||
// NewBuffer creates and initializes a new Buffer using buf as its
|
||||
// initial contents. The new Buffer takes ownership of buf, and the
|
||||
// caller should not use buf after this call. NewBuffer is intended to
|
||||
// prepare a Buffer to read existing data. It can also be used to set
|
||||
// the initial size of the internal buffer for writing. To do that,
|
||||
// buf should have the desired capacity but a length of zero.
|
||||
//
|
||||
// In most cases, new(Buffer) (or just declaring a Buffer variable) is
|
||||
// sufficient to initialize a Buffer.
|
||||
func NewBuffer[T constraints.Unsigned](size int) *Buffer[T] {
|
||||
if size == 0 {
|
||||
size = 512
|
||||
}
|
||||
return &Buffer[T]{buf: make([]T, 0, size)}
|
||||
}
|
||||
|
||||
func NewBufferFrom[T constraints.Unsigned](b []T) *Buffer[T] {
|
||||
return &Buffer[T]{buf: b}
|
||||
}
|
Reference in New Issue
Block a user