Skip to content

Commit

Permalink
Speed up non-asm Memset implementation 3x
Browse files Browse the repository at this point in the history
  • Loading branch information
Jille committed Jul 22, 2024
1 parent 9bb56b8 commit 1f7abc9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,19 @@ func Memset(dst []byte, b byte) {
}

func memsetGeneric(dst []byte, b byte) {
for i := range dst {
if b == 0 {
// Special case that the Go compiler can optimize.
for i := range dst {
dst[i] = 0
}
return
}
eightB := 0x0101010101010101 * uint64(b)
i := 0
for ; i <= len(dst)-8; i += 8 {
binary.LittleEndian.PutUint64(dst[i:], eightB)
}
for ; i < len(dst); i++ {
dst[i] = b
}
}

0 comments on commit 1f7abc9

Please sign in to comment.