You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: lay out global errors.New results in static data #30820
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.
Reacted by Bryan C. Mills, Oleg Kovalov and Mark Pulford
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.
Reacted by Bryan C. Mills, Oleg Kovalov and Carlo Alberto Ferraris
Activity
randall77 commentedon Mar 14, 2019
The compiler already does the latter part of what you described. Starting with:
The compiler generates:
mvdan commentedon Mar 14, 2019
This would be a nice follow-up step to #30468.
/cc @bcmills @neild @mpvl
mvdan commentedon Mar 14, 2019
I wonder, would there be a plan to do the same for simple global uses of
fmt.Errorflikevar ErrFoo = fmt.Errorf("foo error")?Not that
fmt.Errorfis necessarily better for global errors, but i f we madeerrors.Newcomparatively inexpensive, it could lead to a recommended practice to never usefmt.Errorffor globals.Which is maybe fine, but ideally we don't want developers to have to remember that distinction.
gopherbot commentedon Aug 30, 2022
Change https://go.dev/cl/426156 mentions this issue:
cmd/compile: lay out global errors.New results in static datagopherbot commentedon Nov 14, 2022
Change https://go.dev/cl/450136 mentions this issue:
cmd/compile: handle simple inlined calls in staticinitgopherbot commentedon Nov 22, 2022
Change https://go.dev/cl/452817 mentions this issue:
cmd/compile: reenable inlstaticinitcmd/compile: reenable inlstaticinit
cmd/compile: handle simple inlined calls in staticinit