Skip to content

Commit

Permalink
cmd/sync: use an independent dynamic sync pool (#5568)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro authored Jan 17, 2025
1 parent fac5515 commit 6fb68d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
35 changes: 31 additions & 4 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"sync"
"time"

"github.com/juicedata/juicefs/pkg/chunk"
"github.com/juicedata/juicefs/pkg/object"
"github.com/juicedata/juicefs/pkg/utils"
"github.com/juju/ratelimit"
Expand Down Expand Up @@ -553,15 +552,43 @@ func (w *withProgress) Read(b []byte) (int, error) {
return n, err
}

func dynAlloc(size int) []byte {
zeros := utils.PowerOf2(size)
b := *dynPools[zeros].Get().(*[]byte)
if cap(b) < size {
panic(fmt.Sprintf("%d < %d", cap(b), size))
}
return b[:size]
}

func dynFree(b []byte) {
dynPools[utils.PowerOf2(cap(b))].Put(&b)
}

var dynPools []*sync.Pool

func init() {
dynPools = make([]*sync.Pool, 33) // 1 - 8G
for i := 0; i < 33; i++ {
func(bits int) {
dynPools[i] = &sync.Pool{
New: func() interface{} {
b := make([]byte, 1<<bits)
return &b
},
}
}(i)
}
}

func doUploadPart(src, dst object.ObjectStorage, srckey string, off, size int64, key, uploadID string, num int, calChksum bool) (*object.Part, uint32, error) {
if limiter != nil {
limiter.Wait(size)
}
start := time.Now()
sz := size
p := chunk.NewOffPage(int(size))
defer p.Release()
data := p.Data
data := dynAlloc(int(size))
defer dynFree(data)
var part *object.Part
var chksum uint32
err := try(3, func() error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var used int64

// Alloc returns size bytes memory from Go heap.
func Alloc(size int) []byte {
zeros := powerOf2(size)
zeros := PowerOf2(size)
b := *pools[zeros].Get().(*[]byte)
if cap(b) < size {
panic(fmt.Sprintf("%d < %d", cap(b), size))
Expand All @@ -41,7 +41,7 @@ func Alloc(size int) []byte {
func Free(b []byte) {
// buf could be zero length
atomic.AddInt64(&used, -int64(cap(b)))
pools[powerOf2(cap(b))].Put(&b)
pools[PowerOf2(cap(b))].Put(&b)
}

// AllocMemory returns the allocated memory
Expand All @@ -51,7 +51,7 @@ func AllocMemory() int64 {

var pools []*sync.Pool

func powerOf2(s int) int {
func PowerOf2(s int) int {
var bits int
var p int = 1
for p < s {
Expand Down

0 comments on commit 6fb68d9

Please sign in to comment.