Skip to content

cmd/compile: lay out global errors.New results in static data #30820

Closed

Description

@rsc

A significant fraction of init time in some programs
(notably the go command but surely others) is spent
executing lines like:

var ErrMyError = errors.New("my error")

This is such a widespread use that it may well be
worth recognizing in the compiler and turning into
static data, so that it can be laid out in the data section,
discarded by the linker as appropriate, and have no
link-time overhead at all.

The specific pattern would be exactly the above
(var foo = errors.New(constString)) and it would turn into

var foo = error(&errors.errorString{s: constString})

This would require that the compiler be able to lay out an
implicit interface conversion in the data section as well.
If that can't be done in general, it would suffice to special
case the "errors.errorString to error" conversion.

If this sounds like fun to someone familiar with the
compiler, please have a look. Thanks.

Activity

  1. added this to the Go1.13 milestone on Mar 14, 2019
  2. randall77 commented on Mar 14, 2019

    @randall77
    Contributor

    The compiler already does the latter part of what you described. Starting with:

    type errorString struct {
    	s string
    }
    
    func (e *errorString) Error() string {
    	return e.s
    }
    
    var foo = error(&errorString{s: "foobar"})
    

    The compiler generates:

    "".foo SDATA size=16
    	0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    	rel 0+8 t=1 go.itab.*"".errorString,error+0
    	rel 8+8 t=1 ""..stmp_0+0
    ""..stmp_0 SDATA size=16
    	0x0000 00 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00  ................
    	rel 0+8 t=1 go.string."foobar"+0
    go.string."foobar" SRODATA dupok size=6
    	0x0000 66 6f 6f 62 61 72                                foobar
    
  3. mvdan commented on Mar 14, 2019

    @mvdan
    Member

    This would be a nice follow-up step to #30468.

    /cc @bcmills @neild @mpvl

  4. mvdan commented on Mar 14, 2019

    @mvdan
    Member

    I wonder, would there be a plan to do the same for simple global uses of fmt.Errorf like var ErrFoo = fmt.Errorf("foo error")?

    Not that fmt.Errorf is necessarily better for global errors, but i f we made errors.New comparatively inexpensive, it could lead to a recommended practice to never use fmt.Errorf for globals.

    Which is maybe fine, but ideally we don't want developers to have to remember that distinction.

  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Apr 12, 2019
  6. modified the milestones: Go1.13, Go1.14 on Jul 8, 2019
  7. modified the milestones: Go1.14, Backlog on Oct 9, 2019
  8. gopherbot commented on Aug 30, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/426156 mentions this issue: cmd/compile: lay out global errors.New results in static data

  9. gopherbot commented on Nov 14, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/450136 mentions this issue: cmd/compile: handle simple inlined calls in staticinit

  10. Repository owner moved this from Triage Backlog to Done in Go Compiler / Runtime / Commandon Nov 16, 2022
  11. modified the milestones: Backlog, Go1.20 on Nov 16, 2022
  12. gopherbot commented on Nov 22, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/452817 mentions this issue: cmd/compile: reenable inlstaticinit

  13. added a commit that references this issue on Nov 23, 2022
  14. locked and limited conversation to collaborators on Nov 22, 2023
  15. added a commit that references this issue on Feb 20, 2024
    b1678e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions