Skip to content

Commit fa6ccb0

Browse files
committed
stream: move addrConnStream.mu to cache line 2 to eliminate false sharing
sentLast, receivedFirstMsg, and decompressorSet are all written without holding mu (they are written in CloseSend and recvMsg during normal stream operation). After the tail-grouping commit they landed at offset 232–234 (cache line 3: 192–255), the same cache line as mu (offset 224). Concurrent writes to these fields invalidate the cache line before every mu.Lock(), adding up to ~18× overhead under concurrent load. Moving mu to immediately before parser places it at offset 184 (cache line 2: 128–191). The tail bools remain at offset 232+ (cache line 3), so stressor writes no longer affect mu.Lock() latency. Benchmark before this commit (sentLast stressor): stressors=0 3.9 ns stressors=1 19.1 ns (4.9×) stressors=4 69.1 ns (17.7×) Benchmark after (different cache lines): stressors=0 3.7 ns stressors=1 3.7 ns (~1×) stressors=4 3.9 ns (~1×) No size change: addrConnStream remains 240 B. Fixes #9348
1 parent 852e985 commit fa6ccb0

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

stream.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,12 +1502,14 @@ type addrConnStream struct {
15021502
codec baseCodec
15031503
sendCompressorV0 Compressor
15041504
sendCompressorV1 encoding.Compressor
1505-
decompressorV0 Decompressor
1506-
decompressorV1 encoding.Compressor
1507-
parser parser
1505+
decompressorV0 Decompressor
1506+
decompressorV1 encoding.Compressor
15081507

15091508
// mu guards finished and is held for the entire finish method.
1510-
mu sync.Mutex
1509+
// Placed here so it falls on cache line 2 (offsets 128–191), keeping it
1510+
// off the cache line shared with the tail bool fields (offset 232+).
1511+
mu sync.Mutex
1512+
parser parser
15111513

15121514
// Bool fields are grouped at the tail to eliminate the alignment padding
15131515
// that would otherwise follow each bool when the next field is pointer- or

0 commit comments

Comments
 (0)