You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2026/06/21 15:44:31 UTC
[PR] Optimize gRPC request processing: fast negotiation path, Identity codec, and reduced per-request allocations [pekko-grpc]
He-Pin opened a new pull request, #739:
URL: https://github.com/apache/pekko-grpc/pull/739
## Motivation
Profiling the gRPC unary request hot path revealed several allocation and CPU overhead sources:
1. **Protocol negotiation**: `GrpcProtocol.negotiate()` performed redundant header scans and string comparisons per request
2. **Gzip overhead**: Identity codec was not preferred, causing unnecessary Gzip compression/decompression (~27% CPU overhead)
3. **Per-request allocations**: Lambda objects, `ByteString.slice` wrappers, `Option.map` closures, and `PartialFunction` allocations in the unary handler path
4. **URI path matching**: `methodName` used `toString` which triggered `renderPath` + `StringBuilder` + `String` allocation per request
## Modification
### Protocol negotiation & codec (5 commits)
- **Fast negotiation path**: Single-pass header scan for native gRPC (`application/grpc+proto`), avoiding repeated `detect()` calls
- **Identity codec preference**: Always prefer Identity for responses; per-frame compression flag tells clients whether each frame is compressed
- **String comparison fix**: Changed `eq` to `==` for proper string comparison in negotiate fast path
- **Pre-computed negotiation result**: Cache `NativeIdentityIdentity` tuple to avoid per-request allocation
- **`negotiated` lambda elimination**: Replace `Option.map` with direct `match` to avoid closure allocation
### Unary hot path (6 commits)
- **Pre-create unary method lambdas**: Generated handler pre-creates lambdas for unary methods at initialization, avoiding per-request lambda allocation
- **Native identity strict decoding**: Optimized `decodeIdentitySingleFrame` to parse gRPC frame header directly without `ByteReader` allocation
- **Offset-aware deserialization**: Added `ProtobufFrameSerializer.deserialize(data, offset, length)` to bypass `ByteString.slice` allocation (~2 wrapper objects per request)
- **`methodName` Uri.Path matching**: Use `Uri.Path` structure matching (`Slash(Segment(...))`) instead of `toString`, eliminating `renderPath` + `StringBuilder` + `String` allocations (3 objects per request)
- **Inline exception handling**: `handleUnaryException` inlines the error-to-response conversion, avoiding `PartialFunction` allocation via `GrpcExceptionHandler.from()`
- **`encodeFrameData` small message optimization**: For messages ≤4KB, combine frame header + data into single array allocation
## Result
Benchmarked with `ghz` (complex_proto, 1000 concurrency, 50 connections, 120s, SerialGC):
| Metric | Baseline (1.2.0) | Optimized | Improvement |
|--------|-----------------|-----------|-------------|
| Throughput (ForkJoinPool) | 62,899 req/s | 73,584 req/s | **+17.0%** |
| P99 latency (ForkJoinPool) | 32.46 ms | 29.20 ms | **-10.0%** |
| Avg latency (ForkJoinPool) | 8.34 ms | 6.77 ms | **-18.8%** |
| P99 latency (AffinityPool) | — | 24.17 ms | **-25.5%** |
JMH microbenchmarks confirm:
- `GrpcMarshallingBenchmark.unmarshallStrict`: ~50M ops/s
- `ScalaUnaryHandlerBenchmark.generatedUnary`: ~3.8M ops/s
## Tests
- `sbt runtime / Test / testOnly GrpcMarshallingSpec CodecsSpec GrpcResponseHeadersSpec`
- `sbt codegen / Test / test`
- All tests pass
## References
- Profiling with async-profiler (CPU + allocation)
- Benchmark suite: [grpc_bench](https://github.com/LesnyRumcajs/grpc_bench) scala_pekko_bench
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path and reduced per-request allocations [pekko-grpc]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#issuecomment-4781734457
@pjfanning I have removed the gzip change from this PR
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path, Identity codec, and reduced per-request allocations [pekko-grpc]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#issuecomment-4780406592
Let's do that on this PR. at $work, it's adaptive.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path, Identity codec, and reduced per-request allocations [pekko-grpc]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#issuecomment-4780377074
@He-Pin if you want to make the compression adaptive then that's ok by me.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path and reduced per-request allocations [pekko-grpc]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#discussion_r3462363596
##########
runtime/src/main/scala/org/apache/pekko/grpc/internal/Codecs.scala:
##########
@@ -23,8 +23,9 @@ import scala.collection.immutable
import scala.util.{ Failure, Success, Try }
object Codecs {
- // TODO should this list be made user-extensible?
- val supportedCodecs = immutable.Seq(Gzip, Identity)
+ // Codecs supported by this server. The selection order is determined by the
+ // client's grpc-accept-encoding header preference, not by this sequence order.
+ val supportedCodecs = immutable.Seq(Identity, Gzip)
Review Comment:
Will rebate, I think I will continues it this weekends
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path and reduced per-request allocations [pekko-grpc]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#discussion_r3462302835
##########
runtime/src/main/scala/org/apache/pekko/grpc/internal/Codecs.scala:
##########
@@ -23,8 +23,9 @@ import scala.collection.immutable
import scala.util.{ Failure, Success, Try }
object Codecs {
- // TODO should this list be made user-extensible?
- val supportedCodecs = immutable.Seq(Gzip, Identity)
+ // Codecs supported by this server. The selection order is determined by the
+ // client's grpc-accept-encoding header preference, not by this sequence order.
+ val supportedCodecs = immutable.Seq(Identity, Gzip)
Review Comment:
can this gzip change be handled only in the new separate PR?
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path, Identity codec, and reduced per-request allocations [pekko-grpc]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#issuecomment-4780170547
@He-Pin can the gzip change be moved to its own PR because it is a behaviour change - better for transparency?
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path, Identity codec, and reduced per-request allocations [pekko-grpc]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#issuecomment-4780347421
I agree, I think we can add an adaptive one for it,wdyt @pjfanning
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] Optimize gRPC request processing: fast negotiation path and reduced per-request allocations [pekko-grpc]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #739:
URL: https://github.com/apache/pekko-grpc/pull/739#discussion_r3462307450
##########
runtime/src/main/scala/org/apache/pekko/grpc/internal/GrpcProtocolNative.scala:
##########
@@ -44,11 +44,32 @@ object GrpcProtocolNative extends AbstractGrpcProtocol("grpc") {
AbstractGrpcProtocol.writer(this, codec, encodeFrame(codec, _), encodeDataToResponse(codec))
override protected def reader(codec: Codec): GrpcProtocolReader =
- AbstractGrpcProtocol.reader(codec, decodeFrame)
+ if (codec eq Identity)
Review Comment:
this identity codec change, can it be done in the gzip PR instead?
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org