Improve: use one bytes.Buffer pool

This commit is contained in:
Dreamacro
2021-09-20 21:02:18 +08:00
parent 5b1a0a523f
commit 70c8605cca
10 changed files with 53 additions and 60 deletions

View File

@ -2,17 +2,10 @@ package tools
import (
"bytes"
"math/rand"
"sync"
"github.com/Dreamacro/clash/common/pool"
"crypto/rand"
"io"
)
var BufPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
func AppendRandBytes(b *bytes.Buffer, length int) {
randBytes := pool.Get(length)
defer pool.Put(randBytes)
rand.Read(randBytes)
b.Write(randBytes)
b.ReadFrom(io.LimitReader(rand.Reader, int64(length)))
}