-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
Consider the following pattern:
var n int
for _, src := range source {
n += len(src)
}
b := make([]byte, 0, n)
for _, src := range source {
b = append(b, src...)
}which often occurs when merging segmented buffers.
Running this benchmark shows that the CPU time is dominated by runtime.memclrNoHeapPointers, which occupies 44% of the time, while runtime.memmove occupies 36% of the time (this is possibly faster than memclr due to caching effects from the former function).
The compiler should be able to prove that every byte of this slice is overwritten, and thus avoid the call to memclr entirely.
If detecting this pattern is too difficult, we could instead special-case bytes.Join such that the internal call to make avoids the memclr (perhaps by calling a runtime-internal variant of make that avoids zeroing). Thus, the pattern above could be replaced with bytes.Join(source, nil).
Metadata
Metadata
Assignees
Labels
Type
Projects
Status