From 7f2dd28f55c5968e7bd1bf2935cef4bc949320d3 Mon Sep 17 00:00:00 2001 From: Vanja Pejovic Date: Tue, 6 Jan 2026 16:52:00 -0500 Subject: [PATCH 1/6] Allow overriding the default buffer pool. The default buffer pool only has a only a few tiers, which hurts performance in some applications. In, it was decided that instead of adding more tiers to the default pool, we should allow changing the default pool. --- benchmark/benchmain/main.go | 2 +- experimental/experimental.go | 10 ++++++++++ internal/internal.go | 5 ++--- internal/leakcheck/leakcheck.go | 2 +- mem/buffer_pool.go | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/benchmark/benchmain/main.go b/benchmark/benchmain/main.go index 443799f57871..297ba4d2722c 100644 --- a/benchmark/benchmain/main.go +++ b/benchmark/benchmain/main.go @@ -177,7 +177,7 @@ func (p swappableBufferPool) Put(i *[]byte) { } func init() { - internal.SetDefaultBufferPoolForTesting.(func(mem.BufferPool))(swappableBufferPool{mem.DefaultBufferPool()}) + internal.SetDefaultBufferPool.(func(mem.BufferPool))(swappableBufferPool{mem.DefaultBufferPool()}) } var ( diff --git a/experimental/experimental.go b/experimental/experimental.go index 3ba948bab316..e6dcefb7d23e 100644 --- a/experimental/experimental.go +++ b/experimental/experimental.go @@ -31,6 +31,16 @@ import ( "google.golang.org/grpc/mem" ) +// SetDefaultBufferPool sets the default buffer pool used by all grpc clients +// and servers that do not have a buffer pool configured via WithBufferPool or +// BufferPool. It also changes the buffer pool used by the proto codec, which +// can't be changed otherwise. The provided buffer pool must be non-nil. This +// must be called before creating any grpc clients or servers. The default value +// is mem.DefaultBufferPool. +func SetDefaultBufferPool(bufferPool mem.BufferPool) { + internal.SetDefaultBufferPool.(func(mem.BufferPool))(bufferPool) +} + // WithBufferPool returns a grpc.DialOption that configures the use of bufferPool // for parsing incoming messages on a grpc.ClientConn, and for temporary buffers // when marshaling outgoing messages. By default, mem.DefaultBufferPool is used, diff --git a/internal/internal.go b/internal/internal.go index 144b2803090e..b71e9ee51eb7 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -223,9 +223,8 @@ var ( // original state. Only called in testing functions. SnapshotMetricRegistryForTesting func() func() - // SetDefaultBufferPoolForTesting updates the default buffer pool, for - // testing purposes. - SetDefaultBufferPoolForTesting any // func(mem.BufferPool) + // SetDefaultBufferPool updates the default buffer pool. + SetDefaultBufferPool any // func(mem.BufferPool) // SetBufferPoolingThresholdForTesting updates the buffer pooling threshold, for // testing purposes. diff --git a/internal/leakcheck/leakcheck.go b/internal/leakcheck/leakcheck.go index 3c5d905751be..ae00db4bdd41 100644 --- a/internal/leakcheck/leakcheck.go +++ b/internal/leakcheck/leakcheck.go @@ -51,7 +51,7 @@ var failTestsOnLeakedBuffers = false func init() { defaultPool := mem.DefaultBufferPool() globalPool.Store(&defaultPool) - (internal.SetDefaultBufferPoolForTesting.(func(mem.BufferPool)))(&globalPool) + (internal.SetDefaultBufferPool.(func(mem.BufferPool)))(&globalPool) } var globalPool swappableBufferPool diff --git a/mem/buffer_pool.go b/mem/buffer_pool.go index e37afdd1981d..2ea763a49a2f 100644 --- a/mem/buffer_pool.go +++ b/mem/buffer_pool.go @@ -53,7 +53,7 @@ var defaultBufferPool BufferPool func init() { defaultBufferPool = NewTieredBufferPool(defaultBufferPoolSizes...) - internal.SetDefaultBufferPoolForTesting = func(pool BufferPool) { + internal.SetDefaultBufferPool = func(pool BufferPool) { defaultBufferPool = pool } From 12958c9c88b6ba278b3399bcd0a6d51c4a2b635a Mon Sep 17 00:00:00 2001 From: Vanja Pejovic Date: Tue, 6 Jan 2026 17:11:01 -0500 Subject: [PATCH 2/6] Empty commit to trigger CI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 From faa17328f29034b2c5093365374045e0a6ace20c Mon Sep 17 00:00:00 2001 From: Vanja Pejovic Date: Wed, 7 Jan 2026 14:22:44 -0500 Subject: [PATCH 3/6] Empty commit to trigger CI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 From 53078ae945b95317f1b3fe00073fcc27872f7028 Mon Sep 17 00:00:00 2001 From: Vanja Pejovic Date: Thu, 15 Jan 2026 15:22:58 -0500 Subject: [PATCH 4/6] move SetDefaultBufferPool --- internal/experimental.go | 3 +++ internal/internal.go | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/experimental.go b/internal/experimental.go index c90cc51bdd2b..8a999917d978 100644 --- a/internal/experimental.go +++ b/internal/experimental.go @@ -26,6 +26,9 @@ var ( // option to configure a shared buffer pool for a grpc.Server. BufferPool any // func (grpc.SharedBufferPool) grpc.ServerOption + // SetDefaultBufferPool updates the default buffer pool. + SetDefaultBufferPool any // func(mem.BufferPool) + // AcceptCompressors is implemented by the grpc package and returns // a call option that restricts the grpc-accept-encoding header for a call. AcceptCompressors any // func(...string) grpc.CallOption diff --git a/internal/internal.go b/internal/internal.go index b71e9ee51eb7..47d69638540a 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -223,9 +223,6 @@ var ( // original state. Only called in testing functions. SnapshotMetricRegistryForTesting func() func() - // SetDefaultBufferPool updates the default buffer pool. - SetDefaultBufferPool any // func(mem.BufferPool) - // SetBufferPoolingThresholdForTesting updates the buffer pooling threshold, for // testing purposes. SetBufferPoolingThresholdForTesting any // func(int) From cf603f9ba1735f07f8f989541f6776efa99deb8f Mon Sep 17 00:00:00 2001 From: Vanja Pejovic Date: Fri, 16 Jan 2026 12:46:12 -0500 Subject: [PATCH 5/6] update doc --- experimental/experimental.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/experimental/experimental.go b/experimental/experimental.go index e6dcefb7d23e..a7c008a3022f 100644 --- a/experimental/experimental.go +++ b/experimental/experimental.go @@ -34,9 +34,11 @@ import ( // SetDefaultBufferPool sets the default buffer pool used by all grpc clients // and servers that do not have a buffer pool configured via WithBufferPool or // BufferPool. It also changes the buffer pool used by the proto codec, which -// can't be changed otherwise. The provided buffer pool must be non-nil. This -// must be called before creating any grpc clients or servers. The default value -// is mem.DefaultBufferPool. +// can't be changed otherwise. The provided buffer pool must be non-nil. The +// default value is mem.DefaultBufferPool. +// +// NOTE: this function must only be called during initialization time (i.e. in +// an init() function), and is not thread-safe. The last caller wins. func SetDefaultBufferPool(bufferPool mem.BufferPool) { internal.SetDefaultBufferPool.(func(mem.BufferPool))(bufferPool) } From 03954f1d9227d56e4207f328f17278320ebe4ac3 Mon Sep 17 00:00:00 2001 From: Vanja Pejovic Date: Tue, 20 Jan 2026 14:27:14 -0500 Subject: [PATCH 6/6] remove extra parents in leakcheck --- internal/leakcheck/leakcheck.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/leakcheck/leakcheck.go b/internal/leakcheck/leakcheck.go index ae00db4bdd41..fe4efd5d3dd2 100644 --- a/internal/leakcheck/leakcheck.go +++ b/internal/leakcheck/leakcheck.go @@ -51,7 +51,7 @@ var failTestsOnLeakedBuffers = false func init() { defaultPool := mem.DefaultBufferPool() globalPool.Store(&defaultPool) - (internal.SetDefaultBufferPool.(func(mem.BufferPool)))(&globalPool) + internal.SetDefaultBufferPool.(func(mem.BufferPool))(&globalPool) } var globalPool swappableBufferPool