Skip to content

Commit bb2073d

Browse files
vanja-pclaude
andauthored
mem: Allow overriding the default buffer pool. (#8806)
The default buffer pool only has a only a few tiers, which hurts performance in some applications. In #8770, it was decided that instead of adding more tiers to the default pool, we should allow changing the default pool. RELEASE NOTES: * mem: Allow changing the default buffer pool --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bd4444a commit bb2073d

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

benchmark/benchmain/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (p swappableBufferPool) Put(i *[]byte) {
177177
}
178178

179179
func init() {
180-
internal.SetDefaultBufferPoolForTesting.(func(mem.BufferPool))(swappableBufferPool{mem.DefaultBufferPool()})
180+
internal.SetDefaultBufferPool.(func(mem.BufferPool))(swappableBufferPool{mem.DefaultBufferPool()})
181181
}
182182

183183
var (

experimental/experimental.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ import (
3131
"google.golang.org/grpc/mem"
3232
)
3333

34+
// SetDefaultBufferPool sets the default buffer pool used by all grpc clients
35+
// and servers that do not have a buffer pool configured via WithBufferPool or
36+
// BufferPool. It also changes the buffer pool used by the proto codec, which
37+
// can't be changed otherwise. The provided buffer pool must be non-nil. The
38+
// default value is mem.DefaultBufferPool.
39+
//
40+
// NOTE: this function must only be called during initialization time (i.e. in
41+
// an init() function), and is not thread-safe. The last caller wins.
42+
func SetDefaultBufferPool(bufferPool mem.BufferPool) {
43+
internal.SetDefaultBufferPool.(func(mem.BufferPool))(bufferPool)
44+
}
45+
3446
// WithBufferPool returns a grpc.DialOption that configures the use of bufferPool
3547
// for parsing incoming messages on a grpc.ClientConn, and for temporary buffers
3648
// when marshaling outgoing messages. By default, mem.DefaultBufferPool is used,

internal/experimental.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ var (
2626
// option to configure a shared buffer pool for a grpc.Server.
2727
BufferPool any // func (grpc.SharedBufferPool) grpc.ServerOption
2828

29+
// SetDefaultBufferPool updates the default buffer pool.
30+
SetDefaultBufferPool any // func(mem.BufferPool)
31+
2932
// AcceptCompressors is implemented by the grpc package and returns
3033
// a call option that restricts the grpc-accept-encoding header for a call.
3134
AcceptCompressors any // func(...string) grpc.CallOption

internal/internal.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ var (
216216
// original state. Only called in testing functions.
217217
SnapshotMetricRegistryForTesting func() func()
218218

219-
// SetDefaultBufferPoolForTesting updates the default buffer pool, for
220-
// testing purposes.
221-
SetDefaultBufferPoolForTesting any // func(mem.BufferPool)
222-
223219
// SetBufferPoolingThresholdForTesting updates the buffer pooling threshold, for
224220
// testing purposes.
225221
SetBufferPoolingThresholdForTesting any // func(int)

internal/leakcheck/leakcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var failTestsOnLeakedBuffers = false
5151
func init() {
5252
defaultPool := mem.DefaultBufferPool()
5353
globalPool.Store(&defaultPool)
54-
(internal.SetDefaultBufferPoolForTesting.(func(mem.BufferPool)))(&globalPool)
54+
internal.SetDefaultBufferPool.(func(mem.BufferPool))(&globalPool)
5555
}
5656

5757
var globalPool swappableBufferPool

mem/buffer_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var defaultBufferPool BufferPool
5353
func init() {
5454
defaultBufferPool = NewTieredBufferPool(defaultBufferPoolSizes...)
5555

56-
internal.SetDefaultBufferPoolForTesting = func(pool BufferPool) {
56+
internal.SetDefaultBufferPool = func(pool BufferPool) {
5757
defaultBufferPool = pool
5858
}
5959

0 commit comments

Comments
 (0)