Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: new SharedBufferPool to replace the old #1152

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (a *connArray) Init(addr string, security config.Security, idleNotify *uint
}),
}, opts...)
if cfg.TiKVClient.GrpcSharedBufferPool {
opts = append(opts, experimental.WithRecvBufferPool(grpc.NewSharedBufferPool()))
opts = append(opts, experimental.WithRecvBufferPool(NewSharedBufferPool()))
}
conn, err := a.monitoredDial(
ctx,
Expand Down
128 changes: 128 additions & 0 deletions internal/client/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2024 TiKV Authors
//
// 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 client

import (
"runtime"
"sync"

"google.golang.org/grpc"
)

func NewSharedBufferPool() grpc.SharedBufferPool {
return &simpleSharedBufferPool{
pools: [poolArraySize]*bufferPool{
newBytesPool(level0PoolMaxSize),
newBytesPool(level1PoolMaxSize),
newBytesPool(level2PoolMaxSize),
newBytesPool(level3PoolMaxSize),
newBytesPool(level4PoolMaxSize),
newBytesPool(level4PoolMaxSize),
},
}
}

// simpleSharedBufferPool is a simple implementation of SharedBufferPool.
type simpleSharedBufferPool struct {
pools [poolArraySize]*bufferPool
}

func (p *simpleSharedBufferPool) Get(size int) []byte {
idx := p.poolIdx(size)
if idx == levelMaxPoolIdx {
return make([]byte, size)
}
return p.pools[idx].Get(size)
}

func (p *simpleSharedBufferPool) Put(bs *[]byte) {
idx := p.poolIdx(cap(*bs))
if idx == levelMaxPoolIdx {
return
}
p.pools[idx].Put(bs)
}

func (p *simpleSharedBufferPool) poolIdx(size int) int {
switch {
case size <= level0PoolMaxSize:
return level0PoolIdx
case size <= level1PoolMaxSize:
return level1PoolIdx
case size <= level2PoolMaxSize:
return level2PoolIdx
case size <= level3PoolMaxSize:
return level3PoolIdx
case size <= level4PoolMaxSize:
return level4PoolIdx
default:
return levelMaxPoolIdx
}
}

const (
level0PoolMaxSize = 16 // 16 B
level1PoolMaxSize = level0PoolMaxSize * 16 // 256 B
level2PoolMaxSize = level1PoolMaxSize * 16 // 4 KB
level3PoolMaxSize = level2PoolMaxSize * 16 // 64 KB
level4PoolMaxSize = level3PoolMaxSize * 2 // 128 MB
)

const (
level0PoolIdx = iota
level1PoolIdx
level2PoolIdx
level3PoolIdx
level4PoolIdx
levelMaxPoolIdx
poolArraySize
)

type bufferPool struct {
sync.Pool

defaultSize int
}

func (p *bufferPool) Get(size int) []byte {
bs := p.Pool.Get().(*[]byte)

if cap(*bs) < size {
p.Pool.Put(bs)

return make([]byte, size)
}

return (*bs)[:size]
}

func (p *bufferPool) Put(i *[]byte) {
runtime.SetFinalizer(i, func(i *[]byte) {
clear(*i)
p.Pool.Put(i)
})
}

func newBytesPool(size int) *bufferPool {
return &bufferPool{
Pool: sync.Pool{
New: func() any {
bs := make([]byte, size)
return &bs
},
},
defaultSize: size,
}
}
37 changes: 37 additions & 0 deletions internal/client/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 TiKV Authors
//
// 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 client

import "testing"

func TestSharedBufferPool(t *testing.T) {
lengths := []int{
level4PoolMaxSize + 1,
level4PoolMaxSize,
level3PoolMaxSize,
level2PoolMaxSize,
level1PoolMaxSize,
level0PoolMaxSize,
}
p := NewSharedBufferPool()
for _, l := range lengths {
bs := p.Get(l)
if len(bs) != l {
t.Fatalf("Expected buffer of length %d, got %d", l, len(bs))
}

p.Put(&bs)
}
}
Loading