Skip to content

Commit b91c821

Browse files
committed
stream: move serverStream.mu to cache line 2 to eliminate false sharing
serverHeaderBinlogged (written unsynchronised in SendHeader and Send) and mu shared cache line 3 (192–255) after the initial tail-grouping commit. Concurrent writes to serverHeaderBinlogged invalidate the cache line before every mu.Lock(), adding ~5–21× overhead under concurrent load. Moving mu to immediately before trInfo places it at offset 184 (cache line 2: 128–191). The tail bools remain at offset 240+ (cache line 3), so stressor writes no longer affect mu.Lock() latency. Benchmark before this commit (serverHeaderBinlogged stressor): stressors=0 3.9 ns stressors=1 20.1 ns (5.2×) stressors=4 79.9 ns (20.4×) Benchmark after (different cache lines): stressors=0 3.7 ns stressors=1 3.8 ns (~1×) stressors=4 3.7 ns (~1×) No size change: serverStream remains 248 B. Fixes #9349
1 parent ea29db8 commit b91c821

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

stream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,20 +1728,20 @@ type serverStream struct {
17281728

17291729
maxReceiveMessageSize int
17301730
maxSendMessageSize int
1731-
trInfo *traceInfo
1731+
1732+
// mu guards trInfo.tr after the service handler runs.
1733+
mu sync.Mutex
1734+
trInfo *traceInfo
17321735

17331736
statsHandler stats.Handler
17341737

17351738
binlogs []binarylog.MethodLogger
17361739

1737-
mu sync.Mutex // protects trInfo.tr after the service handler runs.
1738-
17391740
// Bool fields are grouped at the tail to eliminate the alignment padding
17401741
// that would otherwise follow each bool when the next field is pointer- or
17411742
// int-sized. See https://github.com/grpc/grpc-go/issues/9349 for benchmarks.
17421743
// Add new bool fields here, not inline above.
17431744

1744-
// Not guarded by mu.
17451745
recvFirstMsg bool // set after the first message is received
17461746
// serverHeaderBinlogged indicates whether server header has been logged. It
17471747
// will happen when one of the following two happens: stream.SendHeader(),

0 commit comments

Comments
 (0)