Skip to content

Commit 955d89f

Browse files
committed
bytes: add padding in bytes.Buffer
Add padding in bytes.Buffer for avoid false sharing. BufferPaddingSize has also been added for this into internal/cpu, so that the padding is specific to each architecture. Signed-off-by: Maxim Eryomenko <[email protected]>
1 parent 3ce865d commit 955d89f

14 files changed

+54
-15
lines changed

src/bytes/buffer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package bytes
88

99
import (
1010
"errors"
11+
"internal/cpu"
1112
"io"
1213
"unicode/utf8"
1314
)
@@ -22,15 +23,15 @@ type Buffer struct {
2223
off int // read at &buf[off], write at &buf[len(buf)]
2324
lastRead readOp // last read operation, so that Unread* can work correctly.
2425

25-
// FIXME: it would be advisable to align Buffer to cachelines to avoid false
26-
// sharing.
26+
// This is padding for Buffer to cache lines to avoid false sharing.
27+
_ [cpu.BufferPaddingSize]byte
2728
}
2829

2930
// The readOp constants describe the last action performed on
3031
// the buffer, so that UnreadRune and UnreadByte can check for
3132
// invalid usage. opReadRuneX constants are chosen such that
3233
// converted to int they correspond to the rune size that was read.
33-
type readOp int8
34+
type readOp int
3435

3536
// Don't use iota for these, as the values need to correspond with the
3637
// names and comments, which is easier to see when being explicit.

src/internal/cpu/cpu.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type CacheLinePad struct{ _ [CacheLinePadSize]byte }
1919
// so we use the constant per GOARCH CacheLinePadSize as an approximation.
2020
var CacheLineSize uintptr = CacheLinePadSize
2121

22+
var BufferPaddingSize uintptr = BufferCacheLinePadSize
23+
2224
var X86 x86
2325

2426
// The booleans in x86 contain the correspondingly named cpuid feature bit.

src/internal/cpu/cpu_386.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const GOARCH = "386"
7+
const (
8+
GOARCH = "386"
9+
BufferCacheLinePadSize = 44
10+
)

src/internal/cpu/cpu_amd64.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const GOARCH = "amd64"
7+
const (
8+
GOARCH = "amd64"
9+
BufferCacheLinePadSize = 24
10+
)

src/internal/cpu/cpu_amd64p32.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const GOARCH = "amd64p32"
7+
const (
8+
GOARCH = "amd64p32"
9+
BufferCacheLinePadSize = 44
10+
)

src/internal/cpu/cpu_arm.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 32
7+
const (
8+
CacheLinePadSize = 32
9+
BufferCacheLinePadSize = 12
10+
)
811

912
// arm doesn't have a 'cpuid' equivalent, so we rely on HWCAP/HWCAP2.
1013
// These are linknamed in runtime/os_(linux|freebsd)_arm.go and are

src/internal/cpu/cpu_arm64.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 64
7+
const (
8+
CacheLinePadSize = 64
9+
BufferCacheLinePadSize = 24
10+
)
811

912
// arm64 doesn't have a 'cpuid' equivalent, so we rely on HWCAP/HWCAP2.
1013
// These are initialized by archauxv in runtime/os_linux_arm64.go.

src/internal/cpu/cpu_mips.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 32
7+
const (
8+
CacheLinePadSize = 32
9+
BufferCacheLinePadSize = 12
10+
)

src/internal/cpu/cpu_mips64.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 32
7+
const (
8+
CacheLinePadSize = 32
9+
BufferCacheLinePadSize = 24
10+
)

src/internal/cpu/cpu_mips64le.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 32
7+
const (
8+
CacheLinePadSize = 32
9+
BufferCacheLinePadSize = 24
10+
)

src/internal/cpu/cpu_mipsle.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 32
7+
const (
8+
CacheLinePadSize = 32
9+
BufferCacheLinePadSize = 12
10+
)

src/internal/cpu/cpu_ppc64x.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package cpu
88

9-
const CacheLinePadSize = 128
9+
const (
10+
CacheLinePadSize = 128
11+
BufferCacheLinePadSize = 88
12+
)
1013

1114
// ppc64x doesn't have a 'cpuid' equivalent, so we rely on HWCAP/HWCAP2.
1215
// These are initialized by archauxv in runtime/os_linux_ppc64x.go.

src/internal/cpu/cpu_s390x.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 256
7+
const (
8+
CacheLinePadSize = 256
9+
BufferCacheLinePadSize = 216
10+
)
811

912
// bitIsSet reports whether the bit at index is set. The bit index
1013
// is in big endian order, so bit index 0 is the leftmost bit.

src/internal/cpu/cpu_wasm.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
package cpu
66

7-
const CacheLinePadSize = 64
7+
const (
8+
CacheLinePadSize = 64
9+
BufferCacheLinePadSize = 44
10+
)

0 commit comments

Comments
 (0)