Skip to content

cmd/compile: move basic blocks, which return non-nil errors, to the end of assembly output #65068

@valyala

Description

@valyala

Basic blocks, which return non-nil errors, are usually executed rarely. So it would great from performance perspective to move them to the end of assembly output. This should provide the following benefits:

  • This will remove unnecessary jumps over rarely executed basic blocks.
  • This will make frequently executed code more compact.
  • This should reduce pressure on CPU instruction cache and may improve performance for the executed code.

The following heuristics can be used for determining whether the basic block returns non-nil error:

  • The block is located in the function, which returns error. The function may return multiple results. If at least a single returned result is error, then it is good idea to look closer to basic blocks of this function.
  • The last statement in the basic block is return, which returns non-nil error. If some globally defined error is returned, then assume it is non-nil. If some function is called at the place of error return result, then assume it returns non-nil error in the following cases:
    • If this is a well-known function from standard library, which returns errors, such as fmt.Errorf() or errors.New().
    • If the function accepts at least a single non-nil error argument. This should cover common idiom: if err != nil { return someErrorWrapper(args, err) }.

I verified these heuristics are missing in Go1.21.

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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions