Skip to content

Commit 01f8f2d

Browse files
committed
chore: cleanup allocator code
1 parent 255ff5e commit 01f8f2d

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

common/pool/alloc.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ import (
88
"sync"
99
)
1010

11-
var defaultAllocator = NewAllocator()
11+
var DefaultAllocator = NewAllocator()
1212

13-
// Allocator for incoming frames, optimized to prevent overwriting after zeroing
14-
type Allocator struct {
13+
type Allocator interface {
14+
Get(size int) []byte
15+
Put(buf []byte) error
16+
}
17+
18+
// defaultAllocator for incoming frames, optimized to prevent overwriting after zeroing
19+
type defaultAllocator struct {
1520
buffers [11]sync.Pool
1621
}
1722

1823
// NewAllocator initiates a []byte allocator for frames less than 65536 bytes,
1924
// the waste(memory fragmentation) of space allocation is guaranteed to be
2025
// no more than 50%.
21-
func NewAllocator() *Allocator {
22-
return &Allocator{
26+
func NewAllocator() Allocator {
27+
return &defaultAllocator{
2328
buffers: [...]sync.Pool{ // 64B -> 64K
2429
{New: func() any { return new([1 << 6]byte) }},
2530
{New: func() any { return new([1 << 7]byte) }},
@@ -37,7 +42,7 @@ func NewAllocator() *Allocator {
3742
}
3843

3944
// Get a []byte from pool with most appropriate cap
40-
func (alloc *Allocator) Get(size int) []byte {
45+
func (alloc *defaultAllocator) Get(size int) []byte {
4146
switch {
4247
case size < 0:
4348
panic("alloc.Get: len out of range")
@@ -87,7 +92,7 @@ func (alloc *Allocator) Get(size int) []byte {
8792

8893
// Put returns a []byte to pool for future use,
8994
// which the cap must be exactly 2^n
90-
func (alloc *Allocator) Put(buf []byte) error {
95+
func (alloc *defaultAllocator) Put(buf []byte) error {
9196
if cap(buf) == 0 || cap(buf) > 65536 {
9297
return nil
9398
}

common/pool/buffer_low_memory.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
package pool
44

55
const (
6+
// RelayBufferSize using for tcp
67
// io.Copy default buffer size is 32 KiB
7-
// but the maximum packet size of vmess/shadowsocks is about 16 KiB
8-
// so define a buffer of 20 KiB to reduce the memory of each TCP relay
98
RelayBufferSize = 16 * 1024
109

11-
// RelayBufferSize uses 20KiB, but due to the allocator it will actually
12-
// request 32Kib. Most UDPs are smaller than the MTU, and the TUN's MTU
10+
// UDPBufferSize using for udp
11+
// Most UDPs are smaller than the MTU, and the TUN's MTU
1312
// set to 9000, so the UDP Buffer size set to 16Kib
1413
UDPBufferSize = 8 * 1024
1514
)

common/pool/buffer_standard.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
package pool
44

55
const (
6+
// RelayBufferSize using for tcp
67
// io.Copy default buffer size is 32 KiB
7-
// but the maximum packet size of vmess/shadowsocks is about 16 KiB
8-
// so define a buffer of 20 KiB to reduce the memory of each TCP relay
9-
RelayBufferSize = 20 * 1024
8+
RelayBufferSize = 32 * 1024
109

11-
// RelayBufferSize uses 20KiB, but due to the allocator it will actually
12-
// request 32Kib. Most UDPs are smaller than the MTU, and the TUN's MTU
10+
// UDPBufferSize using for udp
11+
// Most UDPs are smaller than the MTU, and the TUN's MTU
1312
// set to 9000, so the UDP Buffer size set to 16Kib
1413
UDPBufferSize = 16 * 1024
1514
)

common/pool/pool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package pool
22

33
func Get(size int) []byte {
4-
return defaultAllocator.Get(size)
4+
return DefaultAllocator.Get(size)
55
}
66

77
func Put(buf []byte) error {
8-
return defaultAllocator.Put(buf)
8+
return DefaultAllocator.Put(buf)
99
}

common/pool/sing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package pool
33
import "github.com/metacubex/sing/common/buf"
44

55
func init() {
6-
buf.DefaultAllocator = defaultAllocator
6+
buf.DefaultAllocator = DefaultAllocator
77
}

0 commit comments

Comments
 (0)