From ec2d96888d4a3dcf68428bcb461b24dfe1b59ee8 Mon Sep 17 00:00:00 2001 From: Javier de la Torre Date: Mon, 30 Nov 2026 19:50:31 +0100 Subject: [PATCH] pb: use the slot allocator as gRPC's buffer pool (#13136) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * pb: power-of-two gRPC buffer tiers up to 1 MB ## Problem gRPC's default buffer pool has tiers of 256 B, 4 KB, 16 KB, 32 KB and 1 MB, and zeroes a pooled buffer's whole capacity on every `Get`. Every message between 32 KB and 1 MB therefore zeroes 1 MB, in the proto codec's marshal and in every receive. A filer entry that holds a table's Iceberg metadata (inline content, extended attributes) is such a message, several times per commit. Profiled on `weed mini` under an Iceberg commit load (8 DuckDB writers, one table each, 30 s CPU profile): `runtime.memclrNoHeapPointers` under `sizedBufferPool.Get` was 2.39 s of 30.0 s sampled, 8% of the process's CPU. With the tiers below it is 0.74 s of 22.2 s. ## Fix `weed/pb` sets the process-wide default pool at init (`experimental.SetDefaultBufferPool`, which gRPC documents as init-time only) to a binary tiered pool with every power of two from 4 KB to 1 MB (plus 256 B), so zeroing is bounded by twice the message size. Buffers are still zeroed; only the tier sizes change. The proto codec reads only the default pool; `NewGrpcServer` and `GrpcDial` also pass the pool as an option (`experimental.BufferPool`, `experimental.WithBufferPool`), because gRPC's own default server and dial options captured the old pool before this package's init ran. Messages up to 32 KB and above 1 MB use the same tiers as before. Each extra tier is a `sync.Pool`, emptied by the GC like the others. ## Tests `weed/pb/grpc_buffer_pool_test.go`: - `TestGrpcBufferPoolIsTheDefault`: the default pool is the tiered one. - `BenchmarkGrpcBufferPoolGet`: a `Get`+`Put` with gRPC's default tiers against these (Apple M-series, `go test -bench GrpcBufferPool -count 3 ./weed/pb/`): | message | default tiers | power-of-two tiers | |---|---|---| | 40 KB | 9.8–10.1 µs | 0.59–0.71 µs | | 100 KB | 10.1–10.8 µs | 1.24–1.29 µs | | 300 KB | 11.1–14.7 µs | 5.6–5.7 µs | `go vet ./weed/pb/` reports nothing in the touched files; `go test ./weed/pb/`: ok. `go build ./weed`: ok. * pb: test the buffer capacity each gRPC pool tier hands out Co-Authored-By: Claude Opus 5.5 * pb: rely on the default pool for dials, which read it per connection grpc's defaultDialOptions() reads mem.DefaultBufferPool() at dial time, so only the server, whose defaults were captured at grpc's init, needs the pool passed as an option. Co-Authored-By: Claude Opus 5.5 * mem: return only slot-sized buffers to the slot pools Free put any buffer into the slot its capacity rounds up to, so a nil or odd-capacity buffer made a later Allocate of that slot panic. Co-Authored-By: Claude Opus 5.5 * mem: serve the slots through a Get/Put pool type SlotPool satisfies gRPC's mem.BufferPool, and Allocate and Free now go through it. Co-Authored-By: Claude Opus 5.5 * pb: use the slot pool as gRPC's buffer pool The slots round up to the next power of two and do not zero, so gRPC shares buffers with Allocate and no longer clears them on every Get. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Chris Lu Co-authored-by: Claude Opus 5.5 --- weed/pb/grpc_buffer_pool_test.go | 38 ++++++++++++++++++++++++++++++ weed/pb/grpc_client_server.go | 12 ++++++++++ weed/util/log_buffer/log_buffer.go | 4 +--- weed/util/mem/slot_pool.go | 27 +++++++++++++++------ weed/util/mem/slot_pool_test.go | 28 ++++++++++++++++++++++ 5 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 weed/pb/grpc_buffer_pool_test.go diff --git a/weed/pb/grpc_buffer_pool_test.go b/weed/pb/grpc_buffer_pool_test.go new file mode 100644 index 0000000000..230feb2cd7 --- /dev/null +++ b/weed/pb/grpc_buffer_pool_test.go @@ -0,0 +1,38 @@ +package pb + +import ( + "strconv" + "testing" + + util_mem "github.com/seaweedfs/seaweedfs/weed/util/mem" + "google.golang.org/grpc/mem" +) + +// The proto codec takes its buffers from the process-wide default pool. +func TestGrpcBufferPoolIsTheDefault(t *testing.T) { + if mem.DefaultBufferPool() != (util_mem.SlotPool{}) { + t.Fatal("gRPC's default buffer pool is not the slot pool") + } +} + +// gRPC's default pool zeroes the whole buffer on every Get, and between 32 KB and 1 MB that buffer +// is 1 MB. Compares it with the slot pool. +func BenchmarkGrpcBufferPoolGet(b *testing.B) { + defaultTiers, err := mem.NewBinaryTieredBufferPool(8, 12, 14, 15, 20) + if err != nil { + b.Fatal(err) + } + for _, size := range []int{40 << 10, 100 << 10, 300 << 10, 3 << 20} { + for _, p := range []struct { + name string + pool mem.BufferPool + }{{"grpc-default", defaultTiers}, {"slots", util_mem.SlotPool{}}} { + b.Run(p.name+"/"+strconv.Itoa(size>>10)+"KB", func(b *testing.B) { + b.SetBytes(int64(size)) + for i := 0; i < b.N; i++ { + p.pool.Put(p.pool.Get(size)) + } + }) + } + } +} diff --git a/weed/pb/grpc_client_server.go b/weed/pb/grpc_client_server.go index 2e25cc5589..001a71cf48 100644 --- a/weed/pb/grpc_client_server.go +++ b/weed/pb/grpc_client_server.go @@ -23,9 +23,11 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" "github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/util" + util_mem "github.com/seaweedfs/seaweedfs/weed/util/mem" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/experimental" "google.golang.org/grpc/keepalive" "google.golang.org/grpc/status" @@ -264,6 +266,15 @@ func (l *localSocketListener) Accept() (net.Conn, error) { return c, nil } +// gRPC's default tiers jump from 32 KB to 1 MB, and a pooled buffer is zeroed whole on every Get, so +// a 40 KB message zeroed 1 MB. The slot pool rounds up to the next power of two and skips the +// zeroing, since gRPC writes a pooled buffer before reading it. Dials read the default pool per +// connection; servers copy gRPC's defaults, which captured the old pool before this init ran, so +// NewGrpcServer passes it explicitly. +func init() { + experimental.SetDefaultBufferPool(util_mem.SlotPool{}) +} + func NewGrpcServer(opts ...grpc.ServerOption) *grpc.Server { var options []grpc.ServerOption options = append(options, @@ -282,6 +293,7 @@ func NewGrpcServer(opts ...grpc.ServerOption) *grpc.Server { grpc.InitialConnWindowSize(16*1024*1024), // 16MB connection window grpc.MaxHeaderListSize(8*1024*1024), // 8MB header list limit grpc.UnaryInterceptor(requestIDUnaryInterceptor()), + experimental.BufferPool(util_mem.SlotPool{}), ) for _, opt := range opts { if opt != nil { diff --git a/weed/util/log_buffer/log_buffer.go b/weed/util/log_buffer/log_buffer.go index e0a6c0e649..a1bca39f24 100644 --- a/weed/util/log_buffer/log_buffer.go +++ b/weed/util/log_buffer/log_buffer.go @@ -1077,9 +1077,7 @@ func (logBuffer *LogBuffer) SetLastFlushTsNs(ts int64) { } func (d *dataToFlush) releaseMemory() { - // Guard nil: mem.Free(nil) would put a zero-cap slice into the smallest slot - // pool, so a later Allocate could hand back nil and panic. Also makes a double - // release harmless. + // Clearing data makes a double release harmless. if d.data != nil { mem.Free(d.data) d.data = nil diff --git a/weed/util/mem/slot_pool.go b/weed/util/mem/slot_pool.go index 897034e649..cabed2ffeb 100644 --- a/weed/util/mem/slot_pool.go +++ b/weed/util/mem/slot_pool.go @@ -46,16 +46,29 @@ func AllocationSize(size int) int { return size } -func Allocate(size int) []byte { +// SlotPool serves the slots through gRPC's mem.BufferPool interface. Buffers are not zeroed. +type SlotPool struct{} + +func (SlotPool) Get(size int) *[]byte { if pool, found := getSlotPool(size); found { - slab := *pool.Get().(*[]byte) - return slab[:size] + buf := pool.Get().(*[]byte) + *buf = (*buf)[:size] + return buf } - return make([]byte, size) + buf := make([]byte, size) + return &buf +} + +func (SlotPool) Put(buf *[]byte) { + if pool, found := getSlotPool(cap(*buf)); found && cap(*buf) == AllocationSize(cap(*buf)) { + pool.Put(buf) + } +} + +func Allocate(size int) []byte { + return *SlotPool{}.Get(size) } func Free(buf []byte) { - if pool, found := getSlotPool(cap(buf)); found { - pool.Put(&buf) - } + SlotPool{}.Put(&buf) } diff --git a/weed/util/mem/slot_pool_test.go b/weed/util/mem/slot_pool_test.go index 620f2da8b0..b2c1841d1a 100644 --- a/weed/util/mem/slot_pool_test.go +++ b/weed/util/mem/slot_pool_test.go @@ -56,3 +56,31 @@ func TestAllocationSize(t *testing.T) { } assert.Equal(t, 257<<20, AllocationSize(257<<20)) } + +func TestFreeKeepsOnlySlotSizedBuffers(t *testing.T) { + for _, buf := range [][]byte{nil, make([]byte, 1500), make([]byte, 3000)} { + Free(buf) + for _, size := range []int{1, 2048, 4096} { + got := Allocate(size) + assert.Equal(t, AllocationSize(size), cap(got)) + Free(got) + } + } +} + +func TestSlotPoolGetPut(t *testing.T) { + for _, tc := range []struct{ size, wantCap int }{ + {0, 1 << 10}, + {4 << 10, 4 << 10}, + {(32 << 10) + 1, 64 << 10}, + {40 << 10, 64 << 10}, + {(1 << 20) + 1, 2 << 20}, + {3 << 20, 4 << 20}, + } { + buf := SlotPool{}.Get(tc.size) + if len(*buf) != tc.size || cap(*buf) != tc.wantCap { + t.Errorf("Get(%d): len %d cap %d, want len %d cap %d", tc.size, len(*buf), cap(*buf), tc.size, tc.wantCap) + } + SlotPool{}.Put(buf) + } +}