Skip to content

Commit

Permalink
cmd/sync: use an independent dynamic sync pool
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro committed Jan 17, 2025
1 parent 739620e commit 54dd1df
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 @@ -558,9 +557,8 @@ func doUploadPart(src, dst object.ObjectStorage, srckey string, off, size int64,
}
start := time.Now()
sz := size
p := chunk.NewOffPage(int(size))
defer p.Release()
data := p.Data
data := utils.DynamicAlloc(int(size))
defer utils.DynFree(data)
var part *object.Part
var chksum uint32
err := try(3, func() error {
Expand Down
51 changes: 51 additions & 0 deletions pkg/utils/dynamic_alloc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* JuiceFS, Copyright 2025 Juicedata, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"fmt"
"sync"
)

func DynamicAlloc(size int) []byte {
zeros := 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[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)
}
}

0 comments on commit 54dd1df

Please sign in to comment.