[ CHORE]: goridge: let Frame borrow its payload from the buffer pool instead of copying through it
No duplicates .
- I have searched for a similar issue.
What should be improved or cleaned up?
Draft PR: roadrunner-server/goridge#222. Builds on goridge#221, follow-up to #2401.
Module is github.com/roadrunner-server/goridge/v4, master after goridge#220. Design question before merging, since it touches the Frame contract.
The buffer pool in internal/bpool.go is a staging area. On receive, the payload is read into a pooled buffer, copied into frame.payload, and the pooled buffer goes back:
pb := get(pl)
_, err2 := io.ReadFull(relay, (*pb)[:pl])
...
fr.WritePayload((*pb)[:pl]) // full copy of the body
put(pb)On send, header and payload are copied into a pooled buffer to make one Write:
pb := get(n)
buf := append((*pb)[:0], h...)
buf = append(buf, p...) // full copy of the body
_, err := w.Write(buf)
put(pb)Each side copies the body once more than it has to. With the clone in worker.receiveFrame over in pool, a 1 MB response is memcpy'd three times between the socket and the http plugin.
grpc-go went through the same thing and ended up with mem.BufferSlice: the pooled buffer is the payload, it travels through the layers and the last owner returns it. x/net/http2 keeps a copy because one connection multiplexes many streams and the consumer is unknown when the frame is read. goridge has neither problem: one relay per worker, one frame at a time, and the consumer is the *Frame passed to Receive.
What the PR does. Frame borrows its payload from the tiered pool and returns it in Reset.
AllocPayload(n)resizes the payload in place for the caller to fill. The pooled buffer is reused when it fits, a payload over caller memory (From,ReadFrame) is written in place when it fits as today, otherwise a buffer of the right tier is taken.WritePayloadcopies through it and keeps its semantics.- The pooled pointer is the ownership token: only a buffer taken from the pool ever goes back, memory from
FromorReadFramecannot enter it. No flag. Resetreturns a buffer above 4 KB to its tier and keeps a 4 KB one on the frame, so small frames do not pay a pool round trip per cycle and a frame never retains more than 4 KB.- A pooled payload starts
Headroom(52 bytes, header with 10 options) into its buffer.Wire()writes the header into that room and returns the contiguous frame, soSendFrameis oneWriteat any size with no copy and no scratch. Same trick as headroom insk_buff. ReceiveFramereads the options into the header's spare capacity and the body straight intoAllocPayload(pl).- Frames over caller memory keep the assembled path up to 64 KB and go through
net.Buffersabove it. The pool moves tointernal/bpoolsopkg/framecan import it.
The visible contract does not move: Payload() is valid until the next write or Reset, as documented since #220.
Numbers. Against goridge#221, interleaved runs, benchstat over 10 samples each, M3:
| Path | #221 | this PR |
|---|---|---|
| ReceivePath 1 KB / 64 KB / 1 MB | 68.9 ns / 1.69 µs / 37.3 µs | 43.1 ns / 0.92 µs / 18.9 µs |
| ReceivePath + clone as in pool, 200 B / 4 KB / 64 KB / 1 MB | 94.5 ns / 425 ns / 6.66 µs / 72.2 µs | 76.7 ns / 377 ns / 4.70 µs / 52.5 µs |
| SendPath 1 KB / 64 KB / 1 MB | 89.9 ns / 4.13 µs / 55.9 µs | 75.1 ns / 1.76 µs / 37.5 µs |
allocs/op identical everywhere. On 64 KB the framing part is now about 0.9 µs, one memcpy of the body; the remaining 3.8 µs in the clone row are the two bytes.Clone in pool.
Memory. Since #220 a pooled frame keeps the largest payload it ever carried, one frame pool per worker. One 100 MB response leaves a 100 MB frame on that worker until sync.Pool drops it. With a borrowed payload the buffer goes back to its tier and the frame keeps at most 4 KB. 64 concurrent 200-byte receives hold 0.39 MB, same as #221, against 67 MB before it.
Second step, separate PR in pool. sendFrame writes context and body into AllocPayload instead of a bytes.Buffer, receiveFrame passes the borrowed buffer through payload.Payload instead of cloning, and the http plugin releases it after writing the response. That is where the remaining 80 % on 64 KB is.
Questions:
- Are you fine with
Resetreturning the payload to the pool andFramegainingAllocPayload,WireandHeadroom? - Keeping a 4 KB buffer on the frame across
Reset, or return everything? - Should the pool step be planned together with this, or is goridge alone the target for now?
Wire format stays untouched.
Source: roadrunner-server/roadrunner