Files
seaweedfs/weed/util/mem
ec2d96888d pb: use the slot allocator as gRPC's buffer pool (#13136)
* 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>
2026-11-30 10:50:31 -08:00
..