Skip to content

cmd/compile: idiomatic (returned) iterators are slower than unidiomatic, direct iterators #69411

Closed

Description

@eliasnaur

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:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/a/Library/Caches/go-build'
GOENV='/Users/a/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/a/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/a/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/a/proj/goroot'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/a/proj/goroot/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='devel go1.24-fc968d49c1 Fri Aug 30 11:42:55 2024 -0400'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/a/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/a/Downloads/itergarbage/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/9c/t7b8z6m93sxgr4m4m4kbvllw0000gn/T/go-build4265899566=/tmp/go-build -gno-record-gcc-switches -fno-common'

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.

$ go test -bench . -benchmem
goos: darwin
goarch: arm64
pkg: example.com
cpu: Apple M1 Pro
BenchmarkFormatIdiomatic-8         	39902122	        29.64 ns/op	      24 B/op	       2 allocs/op
BenchmarkFormatterIdiomatic-8      	38252599	        29.33 ns/op	      24 B/op	       2 allocs/op
BenchmarkFormatterNonIdiomatic-8   	381599230	         3.155 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	example.com	4.857s

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

Activity

  1. gabyhelp commented on Sep 11, 2024

    @gabyhelp

    Related Issues and Documentation

    (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

  2. changed the title [-]runtime: idiomatic (returned) iterators are slower than unidiomatic, direct iterators[/-] [+]cmd/compile: idiomatic (returned) iterators are slower than unidiomatic, direct iterators[/+] on Sep 12, 2024
  3. added this to the Backlog milestone on Sep 12, 2024
  4. timothy-king commented on Sep 12, 2024

    @timothy-king
    Contributor
  5. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 12, 2024
  6. moved this from Todo to All-But-Submitted in Go Compiler / Runtime / Commandon Sep 18, 2024
  7. moved this from All-But-Submitted to Todo in Go Compiler / Runtime / Commandon Sep 18, 2024
  8. dr2chase commented on Nov 5, 2024

    @dr2chase
    Contributor

    This is an inliner-problem, not a cost problem -- it still reproduces with the budget set at 1000.

  9. gopherbot commented on Nov 18, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/629195 mentions this issue: cmd/compile: strongly favor closure inlining

  10. added a commit that references this issue on Nov 19, 2024
    170436c
  11. modified the milestones: Backlog, Go1.24 on Nov 22, 2024
  12. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Nov 22, 2024
  13. locked and limited conversation to collaborators on Nov 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions