mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-04-08 06:35:19 +08:00
进一步提升文件缓存写入速度
This commit is contained in:
@@ -2,47 +2,39 @@
|
||||
|
||||
package utils
|
||||
|
||||
import "bytes"
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var SharedBufferPool = NewBufferPool()
|
||||
|
||||
// BufferPool pool for get byte slice
|
||||
type BufferPool struct {
|
||||
c chan *bytes.Buffer
|
||||
rawPool *sync.Pool
|
||||
}
|
||||
|
||||
// NewBufferPool 创建新对象
|
||||
func NewBufferPool(maxSize int) *BufferPool {
|
||||
if maxSize <= 0 {
|
||||
maxSize = 1024
|
||||
}
|
||||
pool := &BufferPool{
|
||||
c: make(chan *bytes.Buffer, maxSize),
|
||||
func NewBufferPool() *BufferPool {
|
||||
var pool = &BufferPool{}
|
||||
pool.rawPool = &sync.Pool{
|
||||
New: func() any {
|
||||
return &bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
return pool
|
||||
}
|
||||
|
||||
// Get 获取一个新的Buffer
|
||||
func (this *BufferPool) Get() (b *bytes.Buffer) {
|
||||
select {
|
||||
case b = <-this.c:
|
||||
b.Reset()
|
||||
default:
|
||||
b = &bytes.Buffer{}
|
||||
var buffer = this.rawPool.Get().(*bytes.Buffer)
|
||||
if buffer.Len() > 0 {
|
||||
buffer.Reset()
|
||||
}
|
||||
return
|
||||
return buffer
|
||||
}
|
||||
|
||||
// Put 放回一个使用过的byte slice
|
||||
func (this *BufferPool) Put(b *bytes.Buffer) {
|
||||
b.Reset()
|
||||
|
||||
select {
|
||||
case this.c <- b:
|
||||
default:
|
||||
// 已达最大容量,则抛弃
|
||||
}
|
||||
}
|
||||
|
||||
// Size 当前的数量
|
||||
func (this *BufferPool) Size() int {
|
||||
return len(this.c)
|
||||
this.rawPool.Put(b)
|
||||
}
|
||||
|
||||
47
internal/utils/buffer_pool_test.go
Normal file
47
internal/utils/buffer_pool_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewBufferPool(t *testing.T) {
|
||||
var pool = utils.NewBufferPool()
|
||||
var b = pool.Get()
|
||||
b.WriteString("Hello, World")
|
||||
t.Log(b.String())
|
||||
|
||||
pool.Put(b)
|
||||
t.Log(b.String())
|
||||
|
||||
b = pool.Get()
|
||||
t.Log(b.String())
|
||||
}
|
||||
|
||||
func BenchmarkNewBufferPool1(b *testing.B) {
|
||||
var data = []byte(strings.Repeat("Hello", 1024))
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var buffer = &bytes.Buffer{}
|
||||
buffer.Write(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkNewBufferPool2(b *testing.B) {
|
||||
var pool = utils.NewBufferPool()
|
||||
var data = []byte(strings.Repeat("Hello", 1024))
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var buffer = pool.Get()
|
||||
buffer.Write(data)
|
||||
pool.Put(buffer)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user