Skip to content

Commit

Permalink
map: avoid copy in appendPerCPUSlice
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Dec 15, 2023
1 parent 9d5614c commit bd9d308
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/sysenc/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (b Buffer) CopyTo(dst []byte) int {
return copy(dst, b.unsafeBytes())
}

// AppendTo appends the buffer onto dst.
func (b Buffer) AppendTo(dst []byte) []byte {
return append(dst, b.unsafeBytes()...)
}

// Pointer returns the location where a syscall should write.
func (b Buffer) Pointer() sys.Pointer {
// NB: This deliberately ignores b.length to support zero-copy
Expand Down
11 changes: 5 additions & 6 deletions marshalers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ func appendPerCPUSlice(buf []byte, slice any, possibleCPUs, elemLength, alignedE
return nil, err
}

elemBytes.CopyTo(buf[len(buf) : len(buf)+elemLength]) // Only copies elemLength
buf = buf[:len(buf)+alignedElemLength]
buf = elemBytes.AppendTo(buf)
buf = append(buf, make([]byte, alignedElemLength-elemLength)...)
}

// Ensure buf is zero-padded full size.
for i := 0; i < possibleCPUs-sliceLen; i++ {
b := make([]byte, alignedElemLength)
buf = append(buf, b...)
}
buf = append(buf, make([]byte, (possibleCPUs-sliceLen)*alignedElemLength)...)

return buf, nil
}

Expand Down

0 comments on commit bd9d308

Please sign in to comment.