get bytes from sync pool
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
@@ -21,12 +22,6 @@ type ContinuousDirtyPages struct {
|
|||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var bufPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return new(bytes.Buffer)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func newDirtyPages(file *File) *ContinuousDirtyPages {
|
func newDirtyPages(file *File) *ContinuousDirtyPages {
|
||||||
return &ContinuousDirtyPages{
|
return &ContinuousDirtyPages{
|
||||||
Data: nil,
|
Data: nil,
|
||||||
@@ -37,9 +32,14 @@ func newDirtyPages(file *File) *ContinuousDirtyPages {
|
|||||||
func (pages *ContinuousDirtyPages) releaseResource() {
|
func (pages *ContinuousDirtyPages) releaseResource() {
|
||||||
if pages.Data != nil {
|
if pages.Data != nil {
|
||||||
pages.f.wfs.bufPool.Put(pages.Data)
|
pages.f.wfs.bufPool.Put(pages.Data)
|
||||||
|
pages.Data = nil
|
||||||
|
atomic.AddInt32(&counter, -1)
|
||||||
|
glog.V(3).Infof("%s/%s releasing resource", pages.f.dir.Path, pages.f.Name, counter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var counter = int32(0)
|
||||||
|
|
||||||
func (pages *ContinuousDirtyPages) AddPage(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
|
func (pages *ContinuousDirtyPages) AddPage(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
|
||||||
|
|
||||||
pages.lock.Lock()
|
pages.lock.Lock()
|
||||||
@@ -47,15 +47,17 @@ func (pages *ContinuousDirtyPages) AddPage(ctx context.Context, offset int64, da
|
|||||||
|
|
||||||
var chunk *filer_pb.FileChunk
|
var chunk *filer_pb.FileChunk
|
||||||
|
|
||||||
if pages.Data == nil {
|
if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
|
||||||
pages.Data = pages.f.wfs.bufPool.Get().([]byte)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data) > len(pages.Data) {
|
|
||||||
// this is more than what buffer can hold.
|
// this is more than what buffer can hold.
|
||||||
return pages.flushAndSave(ctx, offset, data)
|
return pages.flushAndSave(ctx, offset, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pages.Data == nil {
|
||||||
|
pages.Data = pages.f.wfs.bufPool.Get().([]byte)
|
||||||
|
atomic.AddInt32(&counter, 1)
|
||||||
|
glog.V(3).Infof("%s/%s acquire resource", pages.f.dir.Path, pages.f.Name, counter)
|
||||||
|
}
|
||||||
|
|
||||||
if offset < pages.Offset || offset >= pages.Offset+int64(len(pages.Data)) ||
|
if offset < pages.Offset || offset >= pages.Offset+int64(len(pages.Data)) ||
|
||||||
pages.Offset+int64(len(pages.Data)) < offset+int64(len(data)) {
|
pages.Offset+int64(len(pages.Data)) < offset+int64(len(data)) {
|
||||||
// if the data is out of range,
|
// if the data is out of range,
|
||||||
|
|||||||
Reference in New Issue
Block a user