pool protobuf marshal buffers to reduce allocations

Use sync.Pool to reuse 4KB byte slices for protobuf marshaling via
MarshalAppend instead of allocating fresh buffers per result message.
Reduces GC pressure under high-throughput polling.
This commit is contained in:
Graham McIntire 2026-02-12 09:55:38 -06:00
parent da8b644f40
commit 04d8349a6c
No known key found for this signature in database

View file

@ -118,13 +118,20 @@ func runSession(ctx context.Context, baseURL, token string) error {
}
}
bufPool := &sync.Pool{
New: func() any { return make([]byte, 0, 4096) },
}
sendBinaryResult := func(event string, msg proto.Message) {
bin, err := proto.Marshal(msg)
buf := bufPool.Get().([]byte)[:0]
bin, err := proto.MarshalOptions{}.MarshalAppend(buf, msg)
if err != nil {
slog.Error("marshal protobuf", "error", err)
return
}
payload, _ := json.Marshal(map[string]string{"binary": base64.StdEncoding.EncodeToString(bin)})
encoded := base64.StdEncoding.EncodeToString(bin)
bufPool.Put(bin[:0])
payload, _ := json.Marshal(map[string]string{"binary": encoded})
sendMsg(event, payload)
}