Skip to content

Instantly share code, notes, and snippets.

@ncruces
Created April 11, 2025 07:19
Show Gist options
  • Save ncruces/e7974aa3d155c2374c52c040fe613b3e to your computer and use it in GitHub Desktop.
Save ncruces/e7974aa3d155c2374c52c040fe613b3e to your computer and use it in GitHub Desktop.
// https://github.com/golang/go/blob/go1.24.0/src/bytes/bytes.go#L664-L673
func fill[T comparable](buf []T, value T) {
var zero T
switch {
case value == zero:
clear(buf)
case len(buf) != 0:
buf[0] = value
maxChunk := 8 * 1024 / unsafe.Sizeof(value)
for i := 1; i < len(buf); {
chunk := min(i, int(maxChunk))
i += copy(buf[i:], buf[:chunk])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment