]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix formula for computing number of padding bytes
authorLudi Rehak <ludi317@gmail.com>
Sat, 25 Jun 2022 20:27:11 +0000 (13:27 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 19 Aug 2022 16:04:12 +0000 (16:04 +0000)
In order to prevent false sharing of cache lines, structs are
padded with some number of bytes. These bytes are unused, serving
only to make the size of the struct a multiple of the size of the
cache line.

The current calculation of how much to pad is an overestimation,
when the struct size is already a multiple of the cache line size
without padding. For these cases, no padding is necessary, and
the size of the inner pad field should be 0. The bug is that the
pad field is sized to a whole 'nother cache line, wasting space.

Here is the current formula that can never return 0:
cpu.CacheLinePadSize - unsafe.Sizeof(myStruct{})%cpu.CacheLinePadSize

This change simply mods that calculation by cpu.CacheLinePadSize,
so that 0 will be returned instead of cpu.CacheLinePadSize.

Change-Id: I26a2b287171bf47a3b9121873b2722f728381b5e
Reviewed-on: https://go-review.googlesource.com/c/go/+/414214
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/runtime/mheap.go
src/runtime/stack.go

index af14bf58a3ac94bd9cf5e75a35dab56e29859e0b..be53f7bd91ec5fd60795997f2cb9615b7f04d684 100644 (file)
@@ -200,7 +200,7 @@ type mheap struct {
        // central is indexed by spanClass.
        central [numSpanClasses]struct {
                mcentral mcentral
-               pad      [cpu.CacheLinePadSize - unsafe.Sizeof(mcentral{})%cpu.CacheLinePadSize]byte
+               pad      [(cpu.CacheLinePadSize - unsafe.Sizeof(mcentral{})%cpu.CacheLinePadSize) % cpu.CacheLinePadSize]byte
        }
 
        spanalloc             fixalloc // allocator for span*
index b94a4a7249e8d9beef56cd9cf19130849d14bb7a..22dc2d4748d17f48da0719d727f841bf72ed4498 100644 (file)
@@ -157,7 +157,7 @@ const (
 // There is a free list for each order.
 var stackpool [_NumStackOrders]struct {
        item stackpoolItem
-       _    [cpu.CacheLinePadSize - unsafe.Sizeof(stackpoolItem{})%cpu.CacheLinePadSize]byte
+       _    [(cpu.CacheLinePadSize - unsafe.Sizeof(stackpoolItem{})%cpu.CacheLinePadSize) % cpu.CacheLinePadSize]byte
 }
 
 type stackpoolItem struct {