-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rpc_util: Reuse memory buffer for receiving message with stat handlers #6660
Comments
If you have a proposal for how to do this, then please include it here. Otherwise, it's a bit tricky of a situation since the stats handler may keep a pointer to the data, meaning re-use would corrupt it. |
I believe we are talking about this field: https://github.com/grpc/grpc-go/blob/v1.58.2/stats/stats.go#L78-L79 I see two approaches:
The second option will not break anyone as it's an opt-in experience, but it's also not as nice because of the difference in API. Why would a stats handler retain the buffer? I haven't seen such handlers and I cannot come up with a good use case. Are there any examples? |
Unfortunately, yes, it would be, as the documentation doesn't currently forbid it. Yes,
I think this is the only situation where the contract would be violated anyway. This actually seems pretty reasonable, but note that it requires a full understanding from the user of the implications of setting
I believe there are use cases where the stats handler is used to populate a debug page, for example, where the payload is saved and used in a lazy fashion. Copying it early creates often-unnecessary garbage (when the page is never viewed), and allowing it to be overwritten would corrupt that data. |
But isn't this the current situation - each buffer is garbage after it had been used once. Such debug page is rarely used so this buffer is pure garbage and is discarded. What we are discussing would make it possible to make the much more common case, where the buffer can be reused, more efficient (resource-usage-wise). The debugging stats handler would only need to allocate a buffer and copy the data. The only overheard vs the current situation is copying the data, not the allocation. Presumably such stats handler can have a single buffer that it overwrites on each message so in that case the new approach is actually still more efficient than status quo. Current: new buffer for each message New: 1 buffer in gRPC + 1 buffer in stats handler + copy from first to the second. Copying memory should be cheaper than creating garbage+GCing it later. Also, if such a stats handler is for debugging, it should be ok to have some overhead (i.e. "price") for that. It's not cost for nothing, it's the cost of debugging functionality, not pure waste. But the common case becomes way more efficient. #6309 gave me a 33% RAM usage reduction (if I calculated it correctly...), so I'm sure doing the same for the read path would be a great improvement too. I don't understand why a common use case (receiving messages) needs to take such a huge efficiency hit because of an esoteric stats handler used by a very few, especially when there is a clear workaround for their use case. 🤷 ![]() |
Use case(s) - what problem will this feature solve?
Use shared buffer pool when stats handler(s) is used i.e. address the limitation documented in
grpc-go/server.go
Lines 572 to 591 in a758b62
Proposed Solution
?
Alternatives Considered
?
Additional Context
See https://github.com/grpc/grpc-go/pull/5862/files#r1171991389.
The text was updated successfully, but these errors were encountered: