Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
valyala opened this issue Jan 11, 2024 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@valyala
Copy link
Contributor

valyala commented Jan 11, 2024

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.

@dmitshur dmitshur added Performance NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 11, 2024
@dmitshur dmitshur added this to the Backlog milestone Jan 11, 2024
@dmitshur dmitshur changed the title compiler: move basic blocks, which return non-nil errors, to the end of assembly output cmd/compile: move basic blocks, which return non-nil errors, to the end of assembly output Jan 11, 2024
@dmitshur
Copy link
Contributor

CC @golang/compiler.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 11, 2024
@mknyszek mknyszek modified the milestones: Backlog, Unplanned Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
Development

No branches or pull requests

4 participants