Skip to content

Commit

Permalink
Merge pull request #13 from Jille/memset-generic-faster
Browse files Browse the repository at this point in the history
Speed up non-asm Memset implementation 3x
  • Loading branch information
bwesterb authored Jul 22, 2024
2 parents 3560730 + 1f7abc9 commit 3b33e86
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 3b33e86

Please sign in to comment.