cmd/compile: idiomatic (returned) iterators are slower than unidiomatic, direct iterators #69411
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsFix
The path to resolution is known, but the work has not been done.
Performance
Milestone
Go version
go version devel go1.24-fc968d49c1 Fri Aug 30 11:42:55 2024 -0400 darwin/arm64 (with CL 609095)
Output of
go env
in your module/workspace:What did you do?
Benchmarked https://go.dev/play/p/bNomKLr7kQX that compared the performance, in particular the allocations, of 3 ways to implement a nested iterator. The benchmarks are synthetic, because they're extracted from a larger project with more complex iterators.
What did you see happen?
The idiomatic way to create an iterator is to return it from a function or method. In my example, the iterator I want is
func FormatIdiomatic() iter.Seq[rune]
however, that and its method variant,
func (f *Formatter) RunesIdiomatic() iter.Seq[rune]
both result in allocations.
Notably, the unidiomatic
func (f *Formatter) RunesNonIdiomatic(yield func(rune) bool)
doesn't allocate and in general these "direct" iterators runs significantly faster than their idiomatic siblings.
What did you expect to see?
No allocations from idiomatic iterators, even if nested.
Note that I ran the tests with https://go-review.googlesource.com/c/go/+/609095 applied, which didn't seem to make a difference.
Related: #66469 and #69015
The text was updated successfully, but these errors were encountered: