Skip to content

Conversation

arjan-bal
Copy link
Contributor

@arjan-bal arjan-bal commented Sep 1, 2025

This PR improves the size estimate while pre-allocating headerFields to avoid reallocations, which pprof showed were responsible for ~4% of total memory allocations. This change improves performance, increasing QPS by 1% while reducing bytes/op by 4% and latencies by 0.3-4%.

Tested

go run benchmark/benchresult/main.go unary-before unary-after       
unary-networkMode_Local-bufConn_false-keepalive_false-benchTime_1m0s-trace_false-latency_0s-kbps_0-MTU_0-maxConcurrentCalls_120-reqSiz
e_1024B-respSize_1024B-compressor_off-channelz_false-preloader_false-clientReadBufferSize_-1-clientWriteBufferSize_-1-serverReadBuffer
Size_-1-serverWriteBufferSize_-1-sleepBetweenRPCs_0s-connections_1-recvBufferPool_simple-sharedWriteBuffer_false
               Title       Before        After Percentage
            TotalOps      6327736      6390728     1.00%
             SendOps            0            0      NaN%
             RecvOps            0            0      NaN%
            Bytes/op     13903.23     13354.55    -3.95%
           Allocs/op       156.22       155.23    -0.64%
             ReqT/op 863946888.53 872547396.27     1.00%
            RespT/op 863946888.53 872547396.27     1.00%
            50th-Lat    1.00991ms   1.006914ms    -0.30%
            90th-Lat   1.678329ms   1.610331ms    -4.05%
            99th-Lat   2.517556ms   2.497122ms    -0.81%
             Avg-Lat   1.136117ms   1.125311ms    -0.95%
           GoVersion     go1.24.4     go1.24.4
         GrpcVersion   1.76.0-dev   1.76.0-dev

RELEASE NOTES:

  • client: Improve header slice length estimate to reduce re-allocations.

@arjan-bal arjan-bal added this to the 1.76 Release milestone Sep 1, 2025
@arjan-bal arjan-bal requested review from easwars and dfawley September 1, 2025 04:38
@arjan-bal arjan-bal added Type: Performance Performance improvements (CPU, network, memory, etc) Area: Transport Includes HTTP/2 client/server and HTTP server handler transports and advanced transport features. labels Sep 1, 2025
Copy link

codecov bot commented Sep 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.92%. Comparing base (5ed7cf6) to head (90e91b9).
⚠️ Report is 17 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8547      +/-   ##
==========================================
- Coverage   81.99%   81.92%   -0.07%     
==========================================
  Files         412      413       +1     
  Lines       40520    40541      +21     
==========================================
- Hits        33225    33215      -10     
- Misses       5904     5957      +53     
+ Partials     1391     1369      -22     
Files with missing lines Coverage Δ
internal/transport/http2_client.go 91.99% <100.00%> (+0.17%) ⬆️

... and 48 files with indirect coverage changes

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

}
if _, ok := ctx.Deadline(); ok {
hfLen++
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to also check for callHdr.SendCompress != "" and callHdr.PreviousAttempts > 0 and increment as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added these checks as well.


I only added the grpc-timeout header initially since it was the one showing up in the profiles while running the benchmark. This was probably because compression was disabled and retries can't be configured in the benchmark. The improvements were similar after adding checks for the two conditions mentioned above and running the benchmark again with compression enabled.

@easwars easwars assigned arjan-bal and unassigned easwars and dfawley Sep 5, 2025
@arjan-bal arjan-bal assigned easwars and unassigned arjan-bal Sep 10, 2025
@easwars easwars assigned arjan-bal and unassigned easwars Sep 10, 2025
@arjan-bal arjan-bal merged commit 8420f3f into grpc:master Sep 11, 2025
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Transport Includes HTTP/2 client/server and HTTP server handler transports and advanced transport features. Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants