mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
39 lines
1.0 KiB
Go
39 lines
1.0 KiB
Go
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))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|