Skip to content

cmd/compile: avoid memclr for provable overwritten slices #57153

@dsnet

Description

@dsnet

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).

\cc @randall77 @martisch @lavalamp

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions