Skip to content

transport: optimize header field allocation - #9297

Open
2307vivek wants to merge 1 commit into
grpc:masterfrom
2307vivek:optimize_slice_resizing
Open

transport: optimize header field allocation#9297
2307vivek wants to merge 1 commit 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

❌ Patch coverage is 81.48148% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.03%. Comparing base (fa603ec) to head (3895d3f).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
internal/xds/resolver/serviceconfig.go 28.57% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9297      +/-   ##
==========================================
- Coverage   83.06%   83.03%   -0.03%     
==========================================
  Files         423      423              
  Lines       35215    35224       +9     
==========================================
- Hits        29251    29250       -1     
- Misses       4440     4446       +6     
- Partials     1524     1528       +4     
Files with missing lines Coverage Δ
internal/transport/http2_server.go 91.71% <100.00%> (+1.28%) ⬆️
internal/xds/server/routing.go 84.41% <100.00%> (-0.59%) ⬇️
internal/xds/xdsclient/xdsresource/matcher.go 69.90% <100.00%> (-2.17%) ⬇️
internal/xds/resolver/serviceconfig.go 88.18% <28.57%> (-3.55%) ⬇️

... and 23 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.

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