Skip to content

stream: group bool fields at tail of clientStream to reduce allocator size class 288B→256B - #9281

Open
gidotencate wants to merge 2 commits into
grpc:masterfrom
gidotencate:stream-clientstream-group-bools
Open

stream: group bool fields at tail of clientStream to reduce allocator size class 288B→256B#9281
gidotencate wants to merge 2 commits into
grpc:masterfrom
gidotencate:stream-clientstream-group-bools

Conversation

@gidotencate

@gidotencate gidotencate commented Jul 30, 2026

Copy link
Copy Markdown

Reorder clientStream fields so all 7 bool fields are grouped at the tail of the struct. Add (s).TestClientStreamSize to guard against regressions.

Why

clientStream had 7 bool fields scattered among pointer- and int-sized fields. Each bool was followed by 3–7 bytes of alignment padding before the next 4- or 8-byte-aligned field, totalling ~40 bytes of wasted padding:

Field Padding after
sentLast + receivedFirstMsg (together) 6 B
serverHeaderBinlogged 3 B (before sync.Mutex, align 4)
firstAttempt 7 B (before int, align 8)
finished 7 B (before pointer, align 8)
committed 7 B (before func(), align 8)
nameResolutionDelay 7 B tail padding

Result: 280 B struct → 288 B allocator size class.

Grouping all bools at the tail packs them into 7 B + 1 B tail pad = 8 B, reducing the struct to 248 B → 256 B allocator size class: 32 B saved per RPC stream unconditionally, ~10.8% less GC pressure at scale (benchmarks in #9280).

Code clarity

Lock discipline is preserved explicitly: the bool group is split into // Not guarded by mu and // Guarded by mu sections with per-field comments matching the original. A comment in the struct explains the layout rationale and points to #9280. (s).TestClientStreamSize gives a clear error message directing future contributors to add new bool fields in the grouped section.

Closes #9280

RELEASE NOTES:

  • core: reorder clientStream bool fields to reduce struct size from the 288-byte to the 256-byte allocator size class, saving 32 bytes per RPC stream.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 30, 2026

Copy link
Copy Markdown

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

  • ✅ login: gidotencate / name: Gido ten Cate (9ed2533)

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.14%. Comparing base (6f508fa) to head (f441de4).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9281      +/-   ##
==========================================
+ Coverage   83.05%   83.14%   +0.09%     
==========================================
  Files         422      422              
  Lines       35040    35044       +4     
==========================================
+ Hits        29101    29139      +38     
+ Misses       4427     4403      -24     
+ Partials     1512     1502      -10     
Files with missing lines Coverage Δ
stream.go 82.46% <ø> (ø)

... and 32 files with indirect coverage changes

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

@easwars easwars added the Type: Performance Performance improvements (CPU, network, memory, etc) label Jul 30, 2026
@easwars easwars added this to the 1.84 Release milestone Jul 30, 2026
@easwars
easwars requested a review from mbissa July 30, 2026 21:45
@gidotencate
gidotencate force-pushed the stream-clientstream-group-bools branch from 9ed2533 to 83e2471 Compare July 30, 2026 21:48
Comment thread stream.go Outdated
Comment thread stream.go Outdated
Comment thread stream_size_test.go Outdated
Comment thread stream_size_test.go Outdated
clientStream had 7 bool fields scattered among pointer- and int-sized fields,
creating ~40 bytes of alignment padding and placing the struct in the 288-byte
allocator size class (280 B struct → 288 B class).

Grouping all bools at the tail packs them contiguously (7 B + 1 B tail pad)
and reduces the struct to 248 B, which fits in the 256-byte size class:
32 B saved unconditionally per RPC stream, ~10.8% less GC pressure at scale.

Lock discipline is preserved with explicit "Not guarded by mu" / "Guarded by
mu" section comments within the bool group. A TestClientStreamSize test
guards against future growth back across the class boundary; new bool fields
should be added to the grouped section, not inline.

Benchmarks, cache-line analysis, and the rejected "surgical" alternative
(placing mu-guarded bools adjacent to mu caused 20-22% slowdowns from
false sharing on CL2) are documented in the linked issue.

Fixes grpc#9280
@gidotencate
gidotencate force-pushed the stream-clientstream-group-bools branch from 83e2471 to 9f84105 Compare July 30, 2026 22:00
@easwars easwars removed their assignment Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Performance Performance improvements (CPU, network, memory, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream: clientStream has 33B alignment padding; grouping bool fields reduces alloc class from 288B to 256B

4 participants