Skip to content

Commit 5ab7746

Browse files
committed
map: avoid copy in appendPerCPUSlice
Signed-off-by: Lorenz Bauer <[email protected]>
1 parent aab8492 commit 5ab7746

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

internal/sysenc/buffer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func (b Buffer) CopyTo(dst []byte) int {
5454
return copy(dst, b.unsafeBytes())
5555
}
5656

57+
// AppendTo appends the buffer onto dst.
58+
func (b Buffer) AppendTo(dst []byte) []byte {
59+
return append(dst, b.unsafeBytes()...)
60+
}
61+
5762
// Pointer returns the location where a syscall should write.
5863
func (b Buffer) Pointer() sys.Pointer {
5964
// NB: This deliberately ignores b.length to support zero-copy

marshalers.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ func appendPerCPUSlice(buf []byte, slice any, possibleCPUs, elemLength, alignedE
7070
return nil, err
7171
}
7272

73-
elemBytes.CopyTo(buf[len(buf) : len(buf)+elemLength]) // Only copies elemLength
74-
buf = buf[:len(buf)+alignedElemLength]
73+
buf = elemBytes.AppendTo(buf)
74+
buf = append(buf, make([]byte, alignedElemLength-elemLength)...)
7575
}
76+
7677
// Ensure buf is zero-padded full size.
77-
for i := 0; i < possibleCPUs-sliceLen; i++ {
78-
b := make([]byte, alignedElemLength)
79-
buf = append(buf, b...)
80-
}
78+
buf = append(buf, make([]byte, (possibleCPUs-sliceLen)*alignedElemLength)...)
79+
8180
return buf, nil
8281
}
8382

0 commit comments

Comments
 (0)