He-Pin opened a new pull request, #747: URL: https://github.com/apache/pekko-grpc/pull/747
### Motivation The previous optimization (PR #739) always used Identity (no compression) for native gRPC responses to avoid CPU overhead on small messages. However, large messages benefit significantly from gzip compression. An adaptive approach that compresses only messages above a configurable threshold provides the best of both worlds. ### Modification - **AdaptiveGzip codec**: Only compresses messages above a configurable threshold (default 1024 bytes). Messages below the threshold pass through uncompressed while the `grpc-encoding` header still advertises gzip. - **Codec.compressWithFlag()**: Returns `(ByteString, Boolean)` to signal per-frame compression status accurately in the gRPC frame header (bit 0 of the 5-byte header). This fixes a correctness bug where streaming frames would incorrectly set the compression flag even for uncompressed data. - **Configuration**: `pekko.grpc.server.compression-threshold` (default 1024). Generated Scala handlers read config once at handler creation and pass the threshold as `Int` to avoid per-request config overhead. - **JIT/GC optimized**: Pre-computed `NativeIdentityAdaptiveGzip` negotiation result and cached default `AdaptiveGzip` instance for zero per-request allocation on the common path. - **Fast path**: `ProtobufFrameSerializer.serializedDataSize()` for efficient size checking. Adaptive fast path in `GrpcResponseHelpers` for small messages using `serializeDataFrame` single-allocation path. ### Result | Message Size | Behavior | Frame Header Flag | |---|---|---| | < threshold | Sent uncompressed, no gzip CPU overhead | 0 (not compressed) | | ≥ threshold | Gzip compressed | 1 (compressed) | Default threshold of 1024 bytes aligns with industry consensus (Netty, Ktor, OneUptime gRPC guide) as the break-even point where gzip CPU cost is justified by bandwidth savings on protobuf data. ### Tests - `runtime / Test / test` - 152 tests passed - `plugin-tester-scala / Test / testOnly ErrorReportingSpec` - passed - 30 new AdaptiveGzip tests covering: codec behavior, boundary conditions, `compressWithFlag`, streaming frame header correctness, round-trip encode/decode, instance caching, negotiate integration ### References Refs #739 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
