Skip to content

cmd/compile: Opportunity for de-pessimization in provably const range loops #8346

Open
@thockin

Description

@thockin
What does 'go version' print?

go version go1.3 linux/amd64

What steps reproduce the problem?

Using the 2-return 'range' over a slice of structs will copy each struct, even if the
struct is provably not modified in the loop.  This is a premature pessimization that the
compiler could and should eliminate.  Ideally the spec would guarantee this optimization
(a la RVO in C++) so people could count on it and maybe even warn when it is not true.

The for-each construct is much nicer to read and write, but this behavior sort of makes
it a non-starter for large data.  This is discussed in many places on the web, with the
general recommendation of "just iterate on index".  This is unfortunate for
the language.

Alternatively: some syntax to say explicitly "iterate by reference" or by
pointer.

Ian asked me to file this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestedIssues that may be good for new contributors looking for work to do.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions