Skip to content

transport: writeStatus: Skip Status.Proto() without details #8282

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

Merged
merged 4 commits into from
May 7, 2025

Conversation

evanj
Copy link
Contributor

@evanj evanj commented May 1, 2025

Add Status.HasDetails() to check for details. Change http2server.writeStatus to check to avoid unnecessary clones in the common case when there are no details. This seems to use about 0.3% of CPU for a high QPS gRPC service, according to the Go profiler.

While looking at profiles for a high QPS gRPC service at Datadog, I noticed that about 0.3% of CPU time is being spent calling Status.Proto(). I believe this is unnecessary: this clones the protobuf status message, only to check if there are details. If there are not (the common case, e.g. for status code OK), it throws the protobuf message away.

I tried quickly but was not able to reliably measure the impact of this change from any end-to-end benchmarks, but it does seem to make this function go away according to the profiler. This should be a small win for every gRPC server.

Screenshot 2025-05-01 at 9 49 01 AM

RELEASE NOTES:

  • server: improve performance of server RPC handling by avoid a status proto copy

Add Status.HasDetails() to check for details. Change
http2server.writeStatus to check to avoid unnecessary clones in the
common case when there are no details. This seems to be worth about
0.1% of CPU for a high QPS gRPC service, according to the Go
profiler.

While looking at profiles for a high QPS gRPC service at Datadog, I
noticed that about 0.3% of CPU time is being spent calling
Status.Proto(). I believe this is unnecessary: this clones the
protobuf status message, only to check if there are details. If there
are not (the common case, e.g. for status code OK), it throws the
protobuf message away.

I tried quickly but was not able to reliably measure the impact of
this change from any end-to-end benchmarks, but it does seem to make
this function go away according to the profiler. This should be a
small win for every gRPC server.
@evanj evanj force-pushed the evan.jones/writestatus-avoid-proto branch from 1cfbc0c to 7e10d74 Compare May 1, 2025 13:51
Copy link

codecov bot commented May 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.10%. Comparing base (080f956) to head (792ed4c).
Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8282      +/-   ##
==========================================
- Coverage   82.13%   82.10%   -0.03%     
==========================================
  Files         417      419       +2     
  Lines       41385    41946     +561     
==========================================
+ Hits        33991    34440     +449     
- Misses       5961     6030      +69     
- Partials     1433     1476      +43     
Files with missing lines Coverage Δ
internal/status/status.go 91.00% <100.00%> (+0.47%) ⬆️
internal/transport/http2_server.go 90.36% <100.00%> (-0.73%) ⬇️

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dfawley
Copy link
Member

dfawley commented May 1, 2025

This changes our external API since grpc/status/Status is a type alias of grpc/internal/Status/Status. It may be preferable to implement a grpc/internal/status/RawStatusProto(s *Status) *sbp.Proto that just returns the internals, and then operate on them directly instead. The reason we split status in two places is because we want to be able to do things like this that would be unsafe for users to directly access, but improves efficiency of our internal code.

Removes status.HasDetails() in favor of a gRPC internal function.
@evanj
Copy link
Contributor Author

evanj commented May 5, 2025

I have updated the PR to add the suggested RawStatusProto function instead with a test (although the test is pretty silly and could be removed).

I'm happy to make any other suggested improvements. Thanks for the suggestion and the code review!

One unimportant ignorable thought: Another option would be to make this internal function more restricted: HasDetails(...) bool instead. This would avoid revealing the entire internals of the Status type. This seems fairly unimportant. I went with RawStatusProto because I have WAY less context on this project.

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dfawley dfawley requested a review from arjan-bal May 5, 2025 16:44
@dfawley
Copy link
Member

dfawley commented May 5, 2025

@arjan-bal can you take a second pass please?

@dfawley dfawley added the Type: Performance Performance improvements (CPU, network, memory, etc) label May 5, 2025
@dfawley dfawley added this to the 1.73 Release milestone May 5, 2025
@evanj
Copy link
Contributor Author

evanj commented May 5, 2025

I have added a copyright to the new test file by copy/pasting and editing the header from status.go in an attempt to fix vet.sh.

Copy link
Contributor

@arjan-bal arjan-bal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, otherwise LGTM.

@arjan-bal arjan-bal assigned evanj and unassigned arjan-bal May 7, 2025
@arjan-bal arjan-bal added the Area: Server Includes Server, Streams and Server Options. label May 7, 2025
@evanj evanj removed their assignment May 7, 2025
@evanj
Copy link
Contributor Author

evanj commented May 7, 2025

I believe I addressed all the code review comments. I resolved all the line comments above, and unassigned myself. Happy to address any other feedback!

@arjan-bal arjan-bal merged commit c7aec4d into grpc:master May 7, 2025
15 checks passed
purnesh42H pushed a commit to purnesh42H/grpc-go that referenced this pull request May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Server Includes Server, Streams and Server Options. Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants