-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Open
Labels
FeatureRequestIssues asking for a new feature that does not need a proposal.Issues asking for a new feature that does not need a proposal.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.Issues related to the Go compiler and/or runtime.
Description
Go version
go1.25
Output of go env in your module/workspace:
n/aWhat did you do?
Consider these two benchmarks:
func BenchmarkA(b *testing.B) {
for b.Loop() {
out = out[:0]
out = append(out, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
}
}
func BenchmarkB(b *testing.B) {
for b.Loop() {
out = out[:0]
out = append(out, 0)
out = append(out, 1)
out = append(out, 2)
out = append(out, 3)
out = append(out, 4)
out = append(out, 5)
out = append(out, 6)
out = append(out, 7)
out = append(out, 8)
out = append(out, 9)
out = append(out, 0)
out = append(out, 1)
out = append(out, 2)
out = append(out, 3)
out = append(out, 4)
out = append(out, 5)
out = append(out, 6)
out = append(out, 7)
out = append(out, 8)
out = append(out, 9)
}
}What did you see happen?
BenchmarkA
BenchmarkA-32 1000000000 0.7381 ns/op
BenchmarkB
BenchmarkB-32 82785476 14.66 ns/op
BenchmarkB runs much slower than BenchmarkA.
What did you expect to see?
I would expect these to run about the same speed. The capacity may slighter differ, but I don't think that's guaranteed by append. Furthermore, I suspect the compiler could probably pre-compute what the capacity would have been and preserve identical behavior.
adonovan
Metadata
Metadata
Assignees
Labels
FeatureRequestIssues asking for a new feature that does not need a proposal.Issues asking for a new feature that does not need a proposal.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.Issues related to the Go compiler and/or runtime.