all: use unsafe.Add instead of unsafe.Pointer(uintptr(...) + ...)

We have an optimization for this specific pattern, but it's really just
a hack. With the addition of unsafe.Add in Go 1.17 we can directly
specify the intent instead and eventually remove this special case.

The code is also easier to read.
This commit is contained in:
Ayke van Laethem
2022-12-21 16:14:36 +01:00
committed by Ron Evans
parent d98c0afbab
commit 4ec1e58aa6
29 changed files with 84 additions and 95 deletions
+3 -8
View File
@@ -243,13 +243,8 @@ func (ch *channel) push(value unsafe.Pointer) bool {
// copy value to buffer
memcpy(
unsafe.Pointer( // pointer to the base of the buffer + offset = pointer to destination element
uintptr(ch.buf)+
uintptr( // element size * equivalent slice index = offset
ch.elementSize* // element size (bytes)
ch.bufHead, // index of first available buffer entry
),
),
unsafe.Add(ch.buf, // pointer to the base of the buffer + offset = pointer to destination element
ch.elementSize*ch.bufHead), // element size * equivalent slice index = offset
value,
ch.elementSize,
)
@@ -274,7 +269,7 @@ func (ch *channel) pop(value unsafe.Pointer) bool {
}
// compute address of source
addr := unsafe.Pointer(uintptr(ch.buf) + (ch.elementSize * ch.bufTail))
addr := unsafe.Add(ch.buf, (ch.elementSize * ch.bufTail))
// copy value from buffer
memcpy(