Avoid unnecessary copies in IdentityCompression and reuse protocol singletons#299
Merged
Merged
Conversation
…col singletons - IdentityCompression.compress/decompress now return bytes input as-is instead of always calling bytes(data), eliminating a redundant copy on the hot path when no compression is used. - negotiate_server_protocol returns module-level singletons instead of creating new stateless protocol instances on every request. - Add unit tests for both optimizations. Signed-off-by: dongjiang <dongjiang1989@126.com>
Collaborator
|
Thanks @dongjiang1989 - I have applied some cleanups to the tests, notably deleting the protocol singleton test case since it's a minor optimization that doesn't need a regression test, copying buffers in identity compression on the other hand is a bug |
anuraaga
approved these changes
Jul 13, 2026
Contributor
Author
|
Thanks @anuraaga |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compress/decompressnow returnbytesinput as-is (data if isinstance(data, bytes) else bytes(data)) instead of always callingbytes(data), eliminating a redundant allocation on the hot path when no compression is used.negotiate_server_protocolreturns module-level singleton instances (_CONNECT_PROTOCOL,_GRPC_PROTOCOL,_GRPC_WEB_PROTOCOL) instead of creating new stateless protocol instances on every request. The three server protocol classes (ConnectServerProtocol,GRPCServerProtocol,GRPCWebServerProtocol) are verified to be fully stateless (no__init__, no instance variables).Test plan
test/test_compression.py::TestIdentityCompression— 9 cases coveringbytesidentity,bytearray/memoryviewcopy semantics, and empty input.test/test_protocol_server.py::TestNegotiateServerProtocol— 11 cases covering all content-type routing, singleton identity (is), correct types, and distinct instances.Signed-off-by: dongjiang1989 dongjiang1989@126.com