Skip to content

Avoid unnecessary copies in IdentityCompression and reuse protocol singletons#299

Merged
anuraaga merged 2 commits into
connectrpc:mainfrom
dongjiang1989:optimization
Jul 13, 2026
Merged

Avoid unnecessary copies in IdentityCompression and reuse protocol singletons#299
anuraaga merged 2 commits into
connectrpc:mainfrom
dongjiang1989:optimization

Conversation

@dongjiang1989

@dongjiang1989 dongjiang1989 commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • IdentityCompression: compress/decompress now return bytes input as-is (data if isinstance(data, bytes) else bytes(data)) instead of always calling bytes(data), eliminating a redundant allocation on the hot path when no compression is used.
  • Protocol singletons: negotiate_server_protocol returns 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).
  • Added unit tests for both optimizations (20 new test cases).

Test plan

  • test/test_compression.py::TestIdentityCompression — 9 cases covering bytes identity, bytearray/memoryview copy 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.
  • Full test suite: 240 passed.

Signed-off-by: dongjiang1989 dongjiang1989@126.com

dongjiang1989 and others added 2 commits July 11, 2026 13:44
…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>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
@anuraaga anuraaga changed the title perf: avoid unnecessary copies in IdentityCompression and reuse protocol singletons Avoid unnecessary copies in IdentityCompression and reuse protocol singletons Jul 13, 2026
@anuraaga

anuraaga commented Jul 13, 2026

Copy link
Copy Markdown
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
anuraaga merged commit ac0c946 into connectrpc:main Jul 13, 2026
22 of 23 checks passed
@dongjiang1989

Copy link
Copy Markdown
Contributor Author

Thanks @anuraaga

@dongjiang1989
dongjiang1989 deleted the optimization branch July 13, 2026 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants