Skip to content

transport: optimize header field allocation - #9297

Open
2307vivek wants to merge 6 commits into
grpc:masterfrom
2307vivek:optimize_slice_resizing
Open

transport: optimize header field allocation#9297
2307vivek wants to merge 6 commits into
grpc:masterfrom
2307vivek:optimize_slice_resizing

Conversation

@2307vivek

Copy link
Copy Markdown

In http2_server.go, both writeHeaderLocked and writeStatus initialized header field slices with a hard-coded capacity of 2, then appended all metadata values via appendHeaderFieldsFromMD. With metadata-heavy responses, the slice would double its capacity multiple times, each re-allocation copying all existing elements.

This pr pre-sizes header field slices to the exact count of metadata header fields before appending, eliminating repeated slice re-allocations during response header/trailer construction.

Benchmarks

$ go test ./internal/transport/ -bench='BenchmarkAppendHeaderFieldsFromMD' -benchmem -run='^$' -timeout 60s

Metadata Count Allocs/op (Before → After) Bytes/op (Before → After) Time/op (Before → After)
0 0 → 0 0 → 0 B 18.0 → 26.3 ns
4 2 → 1 (-50%) 480 → 320 B (-33%) 632 → 585 ns (-7.5%)
12 4 → 1 (-75%) 2,592 → 1,024 B (-60%) 2,456 → 1,219 ns (-50%)
48 6 → 1 (-83%) 11,808 → 4,096 B (-65%) 10,765 → 5,323 ns (-51%)
$ go run benchmark/benchmain/main.go -benchtime=60s -workloads=unary -compression=off -maxConcurrentCalls=120 -trace=off -reqSizeBytes=1024 -respSizeBytes=1024 -networkMode=Local -recvBufferPool=simple -resultFile="unaryafter"
$ go run benchmark/benchresult/main.go unarybefore unaryafter

Title         Before           After            Percentage
-----         ------           -----            ----------
TotalOps      2981316          3126535          4.87%
SendOps       0                0                NaN%
RecvOps       0                0                NaN%
Bytes/op      12969.88         13010.16         0.32%
Allocs/op     132.48           132.46           0.00%
ReqT/op       407049011.20     426876245.33     4.87%
RespT/op      407049011.20     426876245.33     4.87%
50th-Lat      2.260482ms       2.158989ms      -4.49%
90th-Lat      3.573724ms       3.39454ms       -5.01%
99th-Lat      5.42035ms        5.141398ms      -5.15%
Avg-Lat       2.413806ms       2.301533ms      -4.65%
GoVersion     go1.26.5         go1.26.5
GrpcVersion   1.84.0-dev       1.84.0-dev

RELEASE NOTES: none

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 7, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: 2307vivek / name: Vivek Singh (9e20c85)

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.56%. Comparing base (03255a9) to head (2dfd478).
⚠️ Report is 39 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9297      +/-   ##
==========================================
+ Coverage   83.08%   87.56%   +4.47%     
==========================================
  Files         423      429       +6     
  Lines       35210    30637    -4573     
==========================================
- Hits        29254    26826    -2428     
+ Misses       4436     3811     -625     
+ Partials     1520        0    -1520     
Files with missing lines Coverage Δ
internal/transport/http2_client.go 94.85% <100.00%> (+2.25%) ⬆️
internal/transport/http2_server.go 93.41% <100.00%> (+3.22%) ⬆️

... and 427 files with indirect coverage changes

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

@2307vivek
2307vivek force-pushed the optimize_slice_resizing branch from 3895d3f to aaa8589 Compare August 7, 2026 09:18
@2307vivek 2307vivek changed the title internal/transport: optimize header field allocation transport: optimize header field allocation Aug 7, 2026
@easwars
easwars requested a review from mbissa August 7, 2026 20:02
@easwars easwars 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 Aug 7, 2026
@easwars easwars added this to the 1.84 Release milestone Aug 7, 2026
@2307vivek

Copy link
Copy Markdown
Author

Gentle ping @mbissa.

Also the failing test is not related to these changes. Would appreciate if someone can rerun the checks again.

Comment thread internal/transport/http2_server.go Outdated
Comment thread internal/transport/http2_server_header_bench_test.go Outdated
Comment thread internal/transport/http2_server.go
Comment thread internal/transport/http2_server.go
@2307vivek

Copy link
Copy Markdown
Author

BenchmarkServerHeaderFields 10 samples, benchstat:

mdCount benchmark allocs/op B/op sec/op
0 writeHeaderLocked 3 → 3 175 → 174 (−0.29%) ~ (indistinguishable)
0 writeStatus 6 → 5 (−16.67%) 391 → 311 (−20.46%) −14.81% (p=0.000)
4 writeHeaderLocked 6 → 3 (−50.00%) 1367 → 510 (−62.69%) −18.27% (p=0.000)
4 writeStatus 8 → 5 (−37.50%) 1418 → 630 (−55.56%) −15.75% (p=0.000)
12 writeHeaderLocked 7 → 3 (−57.14%) 2708 → 1218 (−55.03%) −13.59% (p=0.001)
12 writeStatus 9 → 5 (−44.44%) 2762 → 1271 (−53.98%) −13.99% (p=0.000)

@2307vivek

Copy link
Copy Markdown
Author

Just a gentle ping @mbissa

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.

4 participants