The Go programming language https://go.dev
Find a file
thepudds c1e6e49d5d fmt: reduce Errorf("x") allocations to match errors.New("x")
For unformatted strings, it comes up periodically that there are
more allocations using fmt.Errorf("x") compared to errors.New("x").
People cite it as a reason to switch code using fmt.Errorf to
use errors.New instead.

Three examples from the last few weeks essentially made
this suggestion: #75235, CL 708496, and CL 708618. Prior to that,
it is periodically suggested as a vet check (e.g., proposals #17173
and #52696) or in various CLs to change the standard library
(e.g., CL 403938 and CL 588776).

On the other hand, I believe the position of the core Go team
is that it is usually not worthwhile to make such a change. For example,
in #52696, Russ wrote:

  Thanks for raising the issue, but please don't do this. Using
  fmt.Errorf("foo") is completely fine, especially in a program where
  all the errors are constructed with fmt.Errorf. Having to
  mentally switch between two functions based on the argument
  is unnecessary noise.

This CL attempts to mostly take performance out of the discussion.

We drop from 2 allocations to 0 allocations for a non-escaping error,
and drop from 2 allocations to 1 allocation for an escaping error:

  _ = fmt.Errorf("foo")    // non-escaping error
  sink = fmt.Errorf("foo") // escaping error

This now matches the allocations for errors.New("foo") in both cases.

The CPU cost difference is greatly reduced, though there is still
a small ~4ns difference measurable in these microbenchmarks. Previously,
it was ~64ns vs. ~21ns for fmt.Errorf("x") vs. errors.New("x")
for escaping errors, whereas with this CL it is now ~25ns vs. ~21ns.

When fmt.Errorf("foo") executes with this CL, there are essentially
three optimizations now, in rough order of usefulness:
 (1) we always avoid an allocation inside the doPrintf machinery;
 (2) if the error does not otherwise escape, we can stack allocate
     the errors.errorString struct by virtue of mid-stack inlining
     of fmt.Errorf and the resulting inlining of errors.New, which
     also can be more effective via PGO;
 (3) stringslite.IndexByte is a tiny bit faster than going through the
     for loops looking for '%' inside doPrintf.

See https://blog.filippo.io/efficient-go-apis-with-the-inliner/ for
background on avoiding heap allocations via mid-stack inlining.

The common case here is likely that the string format argument is a
constant when there are no other arguments.

However, one concern could be that by not allocating a copy, we could
now keep a string argument alive longer with this change, which could
be a pessimization if for example that string argument is a
slice of a much bigger string:

  s := bigString[m:n]
  longLivedErr := fmt.Errorf(s)

Aside from that being perhaps unusual code, vet will complain about
s there as a "non-constant format string in call to fmt.Errorf", so that
particular example seems unlikely to occur frequently in practice.

The main benchmark results are below. "old" is prior to this CL, "new"
is with this CL. The non-escaping case is "local", the escaping case is
"sink". In practice, I suspect errors escape the majority of the time.
Benchmark code at https://go.dev/play/p/rlRSO1ehx8O

goos: linux
goarch: amd64
pkg: fmt
cpu: AMD EPYC 7B13
                         │  old-7bd6fac4.txt      │           new-dcd2a72f0.txt    │
                         │      sec/op            │   sec/op     vs base           │
Errorf/no-args/local-16              63.76n ± 1%   4.874n ± 0%  -92.36% (n=120)
Errorf/no-args/sink-16               64.25n ± 1%   25.81n ± 0%  -59.83% (n=120)
Errorf/int-arg/local-16              90.86n ± 1%   90.97n ± 1%        ~ (p=0.713 n=120)
Errorf/int-arg/sink-16               91.81n ± 1%   91.10n ± 1%   -0.76% (p=0.036 n=120)
geomean                              76.46n        31.95n       -58.20%

                         │  old-7bd6fac4.txt      │             new-dcd2a72f0.txt  │
                         │       B/op             │    B/op     vs base            │
Errorf/no-args/local-16               19.00 ± 0%    0.00 ± 0%  -100.00% (n=120)
Errorf/no-args/sink-16                19.00 ± 0%   16.00 ± 0%   -15.79% (n=120)
Errorf/int-arg/local-16               24.00 ± 0%   24.00 ± 0%         ~ (p=1.000 n=120) ¹
Errorf/int-arg/sink-16                24.00 ± 0%   24.00 ± 0%         ~ (p=1.000 n=120) ¹
geomean                               21.35                    ?                        ² ³
¹ all samples are equal

                         │  old-7bd6fac4.txt      │       new-dcd2a72f0.txt        │
                         │     allocs/op          │ allocs/op   vs base            │
Errorf/no-args/local-16               2.000 ± 0%   0.000 ± 0%  -100.00% (n=120)
Errorf/no-args/sink-16                2.000 ± 0%   1.000 ± 0%   -50.00% (n=120)
Errorf/int-arg/local-16               2.000 ± 0%   2.000 ± 0%         ~ (p=1.000 n=120) ¹
Errorf/int-arg/sink-16                2.000 ± 0%   2.000 ± 0%         ~ (p=1.000 n=120) ¹
geomean                               2.000                    ?                        ² ³
¹ all samples are equal

Change-Id: Ib27c52933bec5c2236624c577fbb1741052e792f
Reviewed-on: https://go-review.googlesource.com/c/go/+/708836
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: t hepudds <thepudds1460@gmail.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
2025-10-06 17:34:52 -07:00
.github .github: update language change proposal template 2024-08-08 19:02:29 +00:00
api net/http/httputil: deprecate ReverseProxy.Director 2025-10-02 12:53:31 -07:00
doc spec: close tag 2025-10-06 07:58:34 -07:00
lib lib/fips140: re-seal v1.0.0 2025-09-25 12:53:19 -07:00
misc all: replace strings.Split with strings.SplitSeq 2025-09-15 17:34:45 -07:00
src fmt: reduce Errorf("x") allocations to match errors.New("x") 2025-10-06 17:34:52 -07:00
test internal/buildcfg: enable greenteagc experiment by default 2025-10-06 16:01:40 -07:00
.gitattributes all: treat all files as binary, but check in .bat with CRLF 2020-06-08 15:31:43 +00:00
.gitignore runtime,internal: move runtime/internal/sys to internal/runtime/sys 2024-07-23 19:05:35 +00:00
codereview.cfg codereview.cfg: add codereview.cfg for master branch 2021-02-19 18:44:53 +00:00
CONTRIBUTING.md doc: normalize proposal-process links 2023-03-29 22:00:27 +00:00
go.env cmd/go: additional doc-inspired tests and bug fixes 2023-06-06 19:18:46 +00:00
LICENSE LICENSE: update per Google Legal 2024-08-09 14:54:31 +00:00
PATENTS LICENSE: separate, change PATENTS text 2010-12-06 16:31:59 -05:00
README.md README: fix CC BY license name 2024-07-22 17:45:27 +00:00
SECURITY.md SECURITY.md: update the Reporting a Vulnerability link 2023-09-22 21:17:24 +00:00

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 4.0 Attribution license.

Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at https://go.dev/dl/.

After downloading a binary release, visit https://go.dev/doc/install for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit https://go.dev/doc/install/source for source installation instructions.

Contributing

Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at https://go.dev/doc/contribute.

Note that the Go project uses the issue tracker for bug reports and proposals only. See https://go.dev/wiki/Questions for a list of places to ask questions about the Go language.