aboutsummaryrefslogtreecommitdiff
path: root/src/fmt/print.go
AgeCommit message (Collapse)Author
2026-01-30fmt: avoid storing input arguments on pp to help escape analysisthepudds
This is part of a series of CLs that aim to reduce how often interface arguments escape for the print functions in fmt, such as val here: func f(val int) { fmt.Sprintf("%d", val) } Prior to this change, arguments immediately escape in doPrintf because they are stored on the *pp in printArg: parameter a leaks to <heap> for (*pp).doPrintf with derefs=1: flow: <heap> ← *a: from a[argNum] (dot of pointer) at .\print.go:1077:18 from (*pp).printArg(p, a[argNum], rune(c)) (call parameter) at .\print.go:1077:16 parameter arg leaks to <heap> for (*pp).printArg with derefs=0: flow: <heap> ← arg: from p.arg = arg (assign) at .\print.go:682:8 The *pp is heap allocated, and the heap cannot point to stack variables, so the arguments currently cannot be on the stack. This change instead threads through the input arguments as individual type any and reflect.Value parameters, rather than storing on the *pp. After this change, input arguments still escape, but now for other reasons. Updates #8618 Updates #62653 Change-Id: I81984daeceb761ce4ce269b150b888950ce2c5d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/524938 Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2025-11-27fmt: document space behavior of AppendAlan Donovan
Also, introduce the {Print,Fprint,Sprint,Append}{,f,ln} cross product of functions at the top of the docs. Fixes #74656 Change-Id: I85a156cd545ca866e579d8020ddf165cd4bcb26f Reviewed-on: https://go-review.googlesource.com/c/go/+/688877 Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
2025-09-03unicode/utf8: make DecodeRune{,InString} inlineableJulien Cretel
This change makes the fast path for ASCII characters inlineable in DecodeRune and DecodeRuneInString and removes most instances of manual inlining at call sites. Here are some benchmark results (no change to allocations): goos: darwin goarch: amd64 pkg: unicode/utf8 cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz │ old │ new │ │ sec/op │ sec/op vs base │ DecodeASCIIRune-8 2.4545n ± 2% 0.6253n ± 2% -74.52% (p=0.000 n=20) DecodeJapaneseRune-8 3.988n ± 1% 4.023n ± 1% +0.86% (p=0.050 n=20) DecodeASCIIRuneInString-8 2.4675n ± 1% 0.6264n ± 2% -74.61% (p=0.000 n=20) DecodeJapaneseRuneInString-8 3.992n ± 1% 4.001n ± 1% ~ (p=0.625 n=20) geomean 3.134n 1.585n -49.43% Note: when #61502 gets resolved, DecodeRune and DecodeRuneInString should be reverted to their idiomatic implementations. Fixes #31666 Updates #48195 Change-Id: I4be25c4f52417dc28b3a7bd72f1b04018470f39d GitHub-Last-Rev: 2e352a0045027e059be79cdb60241b5cf35fec71 GitHub-Pull-Request: golang/go#75181 Reviewed-on: https://go-review.googlesource.com/c/go/+/699675 Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2024-09-23fmt: fix incorrect doc commentwanggang
I noticed that the comment incorrectly stated 'WriteString implemented WriteString', it should be 'implemented io.StringWriter' instead. Change-Id: I844a8c805e5f0c32b5aea68c4bba6982f6fcc8a7 GitHub-Last-Rev: a0d93b6e9d3c2a508d1f3c0ce23e98274ec421fb GitHub-Pull-Request: golang/go#69546 Reviewed-on: https://go-review.googlesource.com/c/go/+/614575 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Rob Pike <r@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com>
2024-05-13fmt, internal/fmtsort: refactor SortedMap to use slice of structs for map ↵aimuz
sorting This change refactors the SortedMap type in the fmtsort package from using two parallel slices for keys and values to a single slice of structs. This improves code clarity and reduces the complexity of handling map entries. Affected files and their respective functions have been updated to work with the new structure, including adjustments in fmt/print.go and text/template/exec.go to iterate over the new map representation. goos: darwin goarch: arm64 pkg: fmt cpu: Apple M2 Max │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ SprintfPadding-12 21.29n ± 5% 20.89n ± 8% ~ (p=0.393 n=10) SprintfEmpty-12 2.986n ± 4% 2.997n ± 10% ~ (p=0.697 n=10) SprintfString-12 8.327n ± 9% 8.493n ± 12% ~ (p=0.579 n=10) SprintfTruncateString-12 15.93n ± 10% 15.56n ± 10% ~ (p=0.853 n=10) SprintfTruncateBytes-12 14.56n ± 12% 14.13n ± 11% ~ (p=0.796 n=10) SprintfSlowParsingPath-12 9.026n ± 15% 9.511n ± 14% ~ (p=0.646 n=10) SprintfQuoteString-12 40.88n ± 3% 40.73n ± 1% ~ (p=0.782 n=10) SprintfInt-12 6.279n ± 7% 6.130n ± 6% ~ (p=0.218 n=10) SprintfIntInt-12 11.08n ± 10% 11.37n ± 10% ~ (p=0.424 n=10) SprintfPrefixedInt-12 31.24n ± 3% 31.21n ± 2% ~ (p=0.912 n=10) SprintfFloat-12 13.96n ± 7% 13.99n ± 15% ~ (p=0.986 n=10) SprintfComplex-12 49.16n ± 7% 50.57n ± 6% ~ (p=0.436 n=10) SprintfBoolean-12 7.578n ± 15% 7.267n ± 11% ~ (p=0.529 n=10) SprintfHexString-12 36.14n ± 2% 35.74n ± 1% ~ (p=0.118 n=10) SprintfHexBytes-12 48.74n ± 1% 48.34n ± 4% ~ (p=0.128 n=10) SprintfBytes-12 60.16n ± 3% 61.36n ± 5% ~ (p=0.218 n=10) SprintfStringer-12 39.02n ± 10% 39.31n ± 9% ~ (p=0.739 n=10) SprintfStructure-12 161.2n ± 1% 133.9n ± 4% -16.90% (p=0.000 n=10) ManyArgs-12 31.87n ± 17% 33.00n ± 12% ~ (p=0.165 n=10) FprintInt-12 32.32n ± 0% 33.13n ± 1% +2.49% (p=0.000 n=10) FprintfBytes-12 47.31n ± 0% 47.99n ± 1% +1.44% (p=0.000 n=10) FprintIntNoAlloc-12 32.05n ± 1% 33.12n ± 0% +3.34% (p=0.000 n=10) ScanInts-12 130.5µ ± 1% 131.3µ ± 0% +0.57% (p=0.000 n=10) ScanRecursiveInt-12 40.83m ± 1% 40.65m ± 2% ~ (p=0.353 n=10) ScanRecursiveIntReaderWrapper-12 40.77m ± 2% 40.83m ± 2% ~ (p=0.971 n=10) geomean 100.6n 100.3n -0.32% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ SprintfPadding-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfEmpty-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfString-12 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateString-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateBytes-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfSlowParsingPath-12 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfQuoteString-12 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfIntInt-12 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfPrefixedInt-12 64.00 ± 0% 64.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfFloat-12 8.000 ± 0% 8.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfComplex-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfBoolean-12 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexString-12 80.00 ± 0% 80.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexBytes-12 104.0 ± 0% 104.0 ± 0% ~ (p=1.000 n=10) ¹ SprintfBytes-12 88.00 ± 0% 88.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfStringer-12 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfStructure-12 216.0 ± 0% 168.0 ± 0% -22.22% (p=0.000 n=10) ManyArgs-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintfBytes-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ FprintIntNoAlloc-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ScanInts-12 14.87Ki ± 0% 14.87Ki ± 0% ~ (p=1.000 n=10) ¹ ScanRecursiveInt-12 16.37Ki ± 0% 16.34Ki ± 9% ~ (p=0.950 n=10) ScanRecursiveIntReaderWrapper-12 16.43Ki ± 8% 16.35Ki ± 0% ~ (p=0.052 n=10) geomean ² -1.03% ² ¹ all samples are equal ² summaries must be >0 to compute geomean │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ SprintfPadding-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfEmpty-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateBytes-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfSlowParsingPath-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfQuoteString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfIntInt-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfPrefixedInt-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfFloat-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfComplex-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfBoolean-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexBytes-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfBytes-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfStringer-12 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfStructure-12 8.000 ± 0% 6.000 ± 0% -25.00% (p=0.000 n=10) ManyArgs-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintfBytes-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ FprintIntNoAlloc-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ScanInts-12 1.590k ± 0% 1.590k ± 0% ~ (p=1.000 n=10) ¹ ScanRecursiveInt-12 1.592k ± 0% 1.592k ± 0% ~ (p=0.303 n=10) ScanRecursiveIntReaderWrapper-12 1.594k ± 0% 1.594k ± 0% ~ (p=0.582 n=10) geomean ² -1.14% ² ¹ all samples are equal ² summaries must be >0 to compute geomean Change-Id: I2e850d827d2fd7d6618db60f7071977af5639032 GitHub-Last-Rev: 5a4afcf045331c6864902e848ededc1562d5fa53 GitHub-Pull-Request: golang/go#67256 Reviewed-on: https://go-review.googlesource.com/c/go/+/584155 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: qiu laidongfeng2 <2645477756@qq.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-04-02fmt: add available godoc linkcui fliter
Change-Id: Ia7eaa654b44625983d09284d906a7b67ef589696 Reviewed-on: https://go-review.googlesource.com/c/go/+/535082 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-03-04fmt: allow padding and minus flags at the same timeMitar
Existing implementation did not allow setting both padding and minus flags at the same time because standard formatting does not allow that. But custom Formatter interface implementations might have use of it. This change moves the check from the place flags are parsed to where they are used in standard formatting. Fixes #61784 Change-Id: If5909d45dc929ddf911453e1056a4661abe76e52 GitHub-Last-Rev: d99ec55d3bbd9b2a8f14c8ade2fb25d6e0c174c3 GitHub-Pull-Request: golang/go#61836 Reviewed-on: https://go-review.googlesource.com/c/go/+/516975 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Martin Möhrmann <moehrmann@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Martin Möhrmann <martin@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org>
2024-01-31fmt: revert "don't pad strings with zeros"Russ Cox
This reverts CL 555776 (commit 704401ffa06). Scores of tests break inside Google, and there was a test for the old behavior, so clearly we thought it was correct at one point. An example of code that broke inside Google is: func (pn ProjectNumber) PaddedHexString() string { return fmt.Sprintf("%016s", strconv.FormatInt(int64(pn), 16)) } Here is another example: // IPv4toISO create ISO address base on a given IPv4 address. func IPv4toISO(v4 string) (string, error) { if net.ParseIP(v4).To4() == nil { return "", fmt.Errorf("invalid IPv4 address") } s := strings.Split(v4, ".") var ss string for _, n := range s { ss = ss + fmt.Sprintf("%03s", n) } if len(ss) != 12 { return "", fmt.Errorf("invalid IPv4 address") } return fmt.Sprint("49.0001." + ss[0:4] + "." + ss[4:8] + "." + ss[8:12] + ".00"), nil } This is doing the weird but apparently standard conversion from IPv4 to ISO ISIS Area 1 (see for example [1]). Here is an example from github.com/netbirdio/netbird: func generateNewToken() (string, string, error) { secret, err := b.Random(PATSecretLength) if err != nil { return "", "", err } checksum := crc32.ChecksumIEEE([]byte(secret)) encodedChecksum := base62.Encode(checksum) paddedChecksum := fmt.Sprintf("%06s", encodedChecksum) plainToken := PATPrefix + secret + paddedChecksum hashedToken := sha256.Sum256([]byte(plainToken)) encodedHashedToken := b64.StdEncoding.EncodeToString(hashedToken[:]) return encodedHashedToken, plainToken, nil } base62.Encode returns a string no leading zeros; the %06s adds leading zeros. Are there other ways to write these examples? Yes. Has all this code worked until now? Also yes. The change to this behavior observed that right padding doesn't add zeros, only left padding, but that makes sense: in numbers without decimal points, zeros on the left preserve the value while zeros on the right change it. Since we agree that this case is probably not important either way, preserve the long-time behavior of %0s. Will document it in a followup CL: this is a clean revert. Reopen #56486. [1] https://community.cisco.com/t5/routing/isis-net-address-configuration/m-p/1338984/highlight/true#M127827 Change-Id: Ie7dd35227f46933ccc9bfa1eac5fa8608f6d1918 Reviewed-on: https://go-review.googlesource.com/c/go/+/559196 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-01-23fmt: don't pad strings with zerosRob Pike
It's what the documentation says, and oddly it already behaves correctly for right padding, not left. (We never pad with zeros on the right.) Just don't do it. Fixes #56486 Change-Id: I2465edea93c69084e33bee0d945d5a1b85e6cd14 Reviewed-on: https://go-review.googlesource.com/c/go/+/555776 Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-09-08fmt: adjust comment after CL 524940Daniel Martí
https://go.dev/cl/524940 swapped a call to Slice with Bytes, but the comment below still referenced Slice. Change-Id: Iedc772e1c49c4108bcd06f4cea0e637f011d053c Reviewed-on: https://go-review.googlesource.com/c/go/+/526356 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: t hepudds <thepudds1460@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-09-07fmt: avoid reflect.Value.Slice to help escape analysisthepudds
This is part of a series of CLs that aim to reduce how often interface arguments escape for the print functions in fmt. Prior to this change, one reason arguments escape is because printValue calls reflect.Value.Slice, which causes its value argument to escape (though at this CL, that is shrouded in the fmt escape analysis logs by other printValue escape reasons). This CL avoids that usage by calling f.Bytes instead, which is possible because we know f is a slice of bytes or an addressable array of bytes. Arguments still escape for other reasons. Change-Id: Ic3f064117a364007e1dd3197cef9d641abbf784a Reviewed-on: https://go-review.googlesource.com/c/go/+/524940 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: t hepudds <thepudds1460@gmail.com>
2023-09-07fmt: avoid reflect.Value.Pointer to help escape analysisthepudds
This is part of a series of CLs that aim to reduce how often interface arguments escape for the print functions in fmt. Prior to this change, one reason arguments escape is because fmtPointer calls reflect.Value.Pointer: ./print.go:551:39: parameter value leaks to <heap> for (*pp).fmtPointer with derefs=0: ./print.go:551:39: flow: <heap> ← value: ./print.go:551:39: from reflect.Value.Pointer(value) (call parameter) at ./print.go:555:20 printValue also has its value argument escape for this reason, among others. This CL changes those uses to reflect.Value.UnsafePointer instead, which does not cause an escape. Arguments still escape for other reasons. Change-Id: I81c4f737f11fe835c5ccb122caee40a39b553451 Reviewed-on: https://go-review.googlesource.com/c/go/+/524939 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: t hepudds <thepudds1460@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-08-17fmt: amend comment for getFieldJes Cok
Change-Id: I52c9ed0c1a178f3ae3eb4f135d8f11018075fe3b GitHub-Last-Rev: 407aa89c88fc7bb3e4ad9ef55c2a0c5c2f92642c GitHub-Pull-Request: golang/go#62061 Reviewed-on: https://go-review.googlesource.com/c/go/+/519935 Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2023-08-17fmt: fix receiver names are differentch3nnn
"buffer" call the receiver "b" in other method, don't call it "bp" in another. Keep the same receiver names, as prescribed in Go Code Review Comments (https://go.dev/s/style#receiver-names). Change-Id: I9fafc799a9e4102419ed743b941bca74e908f5c0 GitHub-Last-Rev: c8b851d372f3966e3c5eec7c331ad05aacb1ebda GitHub-Pull-Request: golang/go#62066 Reviewed-on: https://go-review.googlesource.com/c/go/+/520016 Auto-Submit: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-05-24fmt: correct documentation for FormatterPaul Jolly
Before this CL, the documentation for Formatter suggested that implementers of Format(f State, verb rune) could use Fprint(f) or Sprint(f) to generate output. The Sprint(f) suggestion however is invalid. Fix that by simply suggesting Sprint() alongside Fprint(f). Fixes #60358 Change-Id: I024e996f6360b812968ef2cd5073cb4c223459e3 Reviewed-on: https://go-review.googlesource.com/c/go/+/497379 Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Run-TryBot: Paul Jolly <paul@myitcv.org.uk> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-09-29errors, fmt: add support for wrapping multiple errorsDamien Neil
An error which implements an "Unwrap() []error" method wraps all the non-nil errors in the returned []error. We replace the concept of the "error chain" inspected by errors.Is and errors.As with the "error tree". Is and As perform a pre-order, depth-first traversal of an error's tree. As returns the first matching result, if any. The new errors.Join function returns an error wrapping a list of errors. The fmt.Errorf function now supports multiple instances of the %w verb. For #53435. Change-Id: Ib7402e70b68e28af8f201d2b66bd8e87ccfb5283 Reviewed-on: https://go-review.googlesource.com/c/go/+/432898 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
2022-09-21fmt: rely on utf8.AppendRuneJoe Tsai
This is both simpler and more performant. The need for fmt.fmtC to manually check for utf8.MaxRune is subtle to avoid overflow when converting uint64 to rune, so a test case was added to exercise this edge case. Change-Id: I0f2e6cce91dcd4cc6b88190c29807ca1c58e999d Reviewed-on: https://go-review.googlesource.com/c/go/+/412335 Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com>
2022-09-03fmt: recycle printers with large buffersCarlo Alberto Ferraris
Previously when a printer had a large buffer we dropped both the buffer and the printer. There is no need to drop the printer in this case, as a printer with a nil buffer is valid. So we just drop the buffer and recycle the printer anyway. This saves one allocation in case the buffer is over the limit. Also tighten some of the tests for other unrelated cases. Change-Id: Iba1b6a71ca4691464b8c68ab0b6ab0d4d5d6168c Reviewed-on: https://go-review.googlesource.com/c/go/+/427395 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-06fmt: add a function to recover the original format string given a StateRob Pike
Sometimes when implementing a Formatter it's helpful to use the fmt package without invoking the formatter. This new function, FormatString, makes that easier in some cases by recreating the original formatting directive (such as "%3.2f") that caused Formatter.Format to be called. The original Formatter interface is probably not what we would design today, but we're stuck with it. FormatString, although it takes a State as an argument, compensates by making Formatter a little more flexible. The State does not include the verb so (unlike in the issue), we must provide it explicitly in the call to FormatString. Doing it there minimizes allocations by returning the complete format string. Fixes #51668 Updates #51195 Change-Id: Ie31c8256515864b2f460df45fbd231286b8b7a28 Reviewed-on: https://go-review.googlesource.com/c/go/+/400875 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Russ Cox <rsc@golang.org>
2022-05-17fmt: add Append, Appendln, AppendfRob Pike
These are straightforward variants of the existing Sprintf etc., but append the resulting bytes to a provided buffer rather than returning a string. Internally, there is potentially some allocation because the package uses a pool of buffers to build its output. We make no attempt to override that, so the result is first printed into the pool and then copied to the output. Since it is a managed pool, asymptotically there should be no extra allocation. Fixes #47579 RELNOTE=yes Change-Id: Icef797f9b6f0c84d03e7035d95c06cdb819e2649 Reviewed-on: https://go-review.googlesource.com/c/go/+/406177 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-04-05all: replace `` and '' with “ (U+201C) and ” (U+201D) in doc commentsRuss Cox
go/doc in all its forms applies this replacement when rendering the comments. We are considering formatting doc comments, including doing this replacement as part of the formatting. Apply it to our source files ahead of time. For #51082. Change-Id: Ifcc1f5861abb57c5d14e7d8c2102dfb31b7a3a19 Reviewed-on: https://go-review.googlesource.com/c/go/+/384262 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-10-26all: use reflect.{Pointer,PointerTo}Cuong Manh Le
Updates #47651 Updates #48665 Change-Id: I69a87b45a5cad7a07fbd855040cd9935cf874554 Reviewed-on: https://go-review.googlesource.com/c/go/+/358454 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2020-10-13fmt: explain how Formatter interface affects verbs and flagsBryan Boreham
Formatter is mentioned further down, but it's helpful to add it amongst the verbs and flags. Background: I spent a while puzzling how "%+v" prints a stack trace for github.com/pkg/errors when this isn't documented under 'flags'. Change-Id: Ic70145902a36780147dedca568b3cf482974fc38 GitHub-Last-Rev: 6571b499f211a2266812af66dd3b88dff602cabf GitHub-Pull-Request: golang/go#39860 Reviewed-on: https://go-review.googlesource.com/c/go/+/240000 Reviewed-by: Rob Pike <r@golang.org> Trust: Rob Pike <r@golang.org> Trust: Ian Lance Taylor <iant@golang.org>
2020-08-17fmt: avoid badverb formatting for %q when used with integersMartin Möhrmann
Instead of returning a bad verb error format for runes above utf8.Maxrune return a quoted utf8.RuneError rune (\ufffd). This makes the behaviour consistent with the "c" verb and aligns behaviour to not return bad verb error format when a verb is applied to the correct argument type. Fixes #14569 Change-Id: I679485f6bb90ebe408423ab68af16cce38816cd0 Reviewed-on: https://go-review.googlesource.com/c/go/+/248759 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2019-05-23fmt: always clear wrapErrsMartin Möhrmann
Like panicking and erroring - wrapErrs should always be reset to the default false. wrapErrs should only be true when set by Errorf. Change-Id: I4d51cc2f0905109e232b0983dc5331bd34f138bc Reviewed-on: https://go-review.googlesource.com/c/go/+/178517 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2019-05-15fmt: support %wDamien Neil
When fmt.Errorf is provided with a %w verb with an error operand, return an error implementing an Unwrap method returning that operand. It is invalid to use %w with other formatting functions, to use %w multiple times in a format string, or to use %w with a non-error operand. When the Errorf format string contains an invalid use of %w, the returned error does not implement Unwrap. Change-Id: I534e20d3b163ab22c2b137b1c9095906dc243221 Reviewed-on: https://go-review.googlesource.com/c/go/+/176998 Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-05-15errors, fmt: revert rejected changes for Go 1.13Damien Neil
Reverts the following changes: https://go.googlesource.com/go/+/1f90d081391d4f5911960fd28d81d7ea5e554a8f https://go.googlesource.com/go/+/8bf18b56a47a98b9dd2fa03beb358312237a8c76 https://go.googlesource.com/go/+/5402854c3557f87fa2741a52ffc15dfb1ef333cc https://go.googlesource.com/go/+/37f84817247d3b8e687a701ccb0d6bc7ffe3cb78 https://go.googlesource.com/go/+/6be6f114e0d483a233101a67c9644cd72bd3ae7a Partially reverts the followinng change, removing the errors.Opaque function and the errors.Wrapper type definition: https://go.googlesource.com/go/+/62f5e8156ef56fa61e6af56f4ccc633bde1a9120 Updates documentation referencing the Wrapper type. Change-Id: Ia622883e39cafb06809853e3fd90b21441124534 Reviewed-on: https://go-review.googlesource.com/c/go/+/176997 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-05-09fmt: rename buffer.WriteByte to writeByteRuss Cox
Renaming the method makes clear, both to readers and to vet, that this method is not the implementation of io.ByteWriter. Working toward making the tree vet-safe instead of having so many exceptions in cmd/vet/all/whitelist. For #31916. Change-Id: I79da062ca6469b62a6b9e284c6cf2413c7425249 Reviewed-on: https://go-review.googlesource.com/c/go/+/176109 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-02-27fmt: add frame info to Errorf and support %wMarcel van Lohuizen
Partly implements proposal Issue #29934. Change-Id: Ibcf12f383158dcfbc313ab29c417a710571d1acb Reviewed-on: https://go-review.googlesource.com/c/163559 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2019-02-26fmt: format 0b, 0o prefixes in %#b and %ORuss Cox
This CL modifies fmt's printer to implement %#b and %O to emit leading 0b and 0o prefixes on binary and octal. (%#o is already taken and emits "0377"; %O emits "0o377".) See golang.org/design/19308-number-literals for background. For #19308. For #12711. Vet update is #29986. Change-Id: I7c38a4484c48a03abe9f6d45c7d981c7c314f583 Reviewed-on: https://go-review.googlesource.com/c/160246 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2019-02-26fmt: format hex floats and complexesRuss Cox
This CL modifies fmt's printer to implement %x and %X for formatting floating-point data (floats and complexes) in standard hexadecimal notation. See golang.org/design/19308-number-literals for background. For #29008. Vet update is #29986. Change-Id: If2842a11631bc393a1ebcf6914ed07658652af5a Reviewed-on: https://go-review.googlesource.com/c/160245 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-12-13fmt: include failing method name in panic messageIan Lance Taylor
Fixes #25707 Change-Id: Idfa379db8cc0e105ea68455ec0b4a0dbc1b3f485 Reviewed-on: https://go-review.googlesource.com/c/153827 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-14fmt: avoid allocation when formatting byte slice arguments with verb sMartin Möhrmann
fmtBytes is in the top 10 callers of runtime.slicebytetostring according to Google wide profiling data. Avoid the string conversion of the input byte slice in fmtBytes by calling a newly added specialized fmtS function for byte slices. Expand tests for verb s with widths to test strings and byte slice arguments. SprintfTruncateString 157ns ± 4% 156ns ± 3% ~ (p=0.122 n=20+20) SprintfTruncateBytes 188ns ± 2% 155ns ± 3% -18.00% (p=0.000 n=20+19) name old alloc/op new alloc/op delta SprintfTruncateString 16.0B ± 0% 16.0B ± 0% ~ (all equal) SprintfTruncateBytes 64.0B ± 0% 16.0B ± 0% -75.00% (p=0.000 n=20+20) name old allocs/op new allocs/op delta SprintfTruncateString 1.00 ± 0% 1.00 ± 0% ~ (all equal) SprintfTruncateBytes 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=20+20) Change-Id: I461bf514d4232b39bd9c812f7faa4e5ef693a03b Reviewed-on: https://go-review.googlesource.com/c/145284 Run-TryBot: Martin Möhrmann <martisch@uos.de> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-10-18fmt: print maps in key-sorted orderRob Pike
For easier testing, change the way maps are printed so they appear in a consistent order between runs. Do this by printing them in key-sorted order. To do this, we add a package at the root, internal/fmtsort, that implements a general mechanism for sorting map keys regardless of their type. This is a little messy and probably slow, but formatted printing of maps has never been fast and is already always reflection-driven. The rules are: The ordering rules are more general than with Go's < operator: - when applicable, nil compares low - ints, floats, and strings order by < - NaN compares less than non-NaN floats - bool compares false before true - complex compares real, then imag - pointers compare by machine address - channel values compare by machine address - structs compare each field in turn - arrays compare each element in turn. - interface values compare first by reflect.Type describing the concrete type and then by concrete value as described in the previous rules. The new package is internal because we really do not want everyone using this to sort things. It is slow, not general, and only suitable for the subset of types that can be map keys. Also use the package in text/template, which already had a weaker version of this mechanism. This change requires adding a dependency on sort to the fmt package, but that isn't disruptive to the dependency tree. Fixes #21095 Change-Id: Ia602115c7de5d95993dbd609611d8bd96e054157 Reviewed-on: https://go-review.googlesource.com/c/142737 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2018-09-19fmt: fix usage of sync.PoolJoe Tsai
The current usage of sync.Pool is leaky because it stores an arbitrary sized buffer into the pool. However, sync.Pool assumes that all items in the pool are interchangeable from a memory cost perspective. Due to the unbounded size of a buffer that may be added, it is possible for the pool to eventually pin arbitrarily large amounts of memory in a live-lock situation. As a simple fix, we just set a maximum size that we permit back into the pool. We do not need to fix the use of a sync.Pool in scan.go since the free method has always enforced a maximum capacity since the first commit of the scan logic. Fixes #27740 Updates #23199 Change-Id: I875278f7dba42625405df36df3e9b028252ce5e3 Reviewed-on: https://go-review.googlesource.com/136116 Reviewed-by: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-08-22fmt: print values for map keys with non-reflexive equalityMartin Möhrmann
Previously fmt would first obtain a list of map keys and then look up the value for each key. Since NaNs can be map keys but cannot be fetched directly, the lookup would fail and return a zero reflect.Value, which formats as <nil>. golang.org/cl/33572 added a map iterator to the reflect package that is used in this CL to retrieve the key and value from the map and prints the correct value even for keys that are not equal to themselves. Fixes #14427 Change-Id: I9e1522959760b3de8b7ecf7a6e67cd603339632a Reviewed-on: https://go-review.googlesource.com/129777 Reviewed-by: Alan Donovan <adonovan@google.com>
2018-02-27fmt: change some unexported method names to camel caseDarshan Parajuli
Change-Id: I12f96a9397cfaebe11e616543d804bd62cd72da0 Reviewed-on: https://go-review.googlesource.com/97379 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-09-20fmt: Implement pp.WriteString methodRajath Agasthya
This allows io.WriteString to make use of WriteString method implemented by pp when writing a string to fmt.State. Fixes #20786 Change-Id: Ice7a92bf303127ad87f05562217fa076f5c589ad Reviewed-on: https://go-review.googlesource.com/61430 Reviewed-by: Rob Pike <r@golang.org>
2017-08-26all: remove some double spaces from commentsDaniel Martí
Went mainly for the ones that make no sense, such as the ones mid-sentence or after commas. Change-Id: Ie245d2c19cc7428a06295635cf6a9482ade25ff0 Reviewed-on: https://go-review.googlesource.com/57293 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-08-14fmt: add ascii fast path for decoding verbsMartin Möhrmann
name old time/op new time/op delta SprintfSlowParsingPath 108ns ± 4% 103ns ± 4% -4.53% (p=0.000 n=18+18) Change-Id: I174463f303d1857e8d5b8a6283c025b3546e7b39 Reviewed-on: https://go-review.googlesource.com/44450 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2017-02-19fmt: remove unused global variable byteTypeMartin Möhrmann
Change list https://golang.org/cl/20686/ removed the last use of the variable byteType. Change-Id: I4ea79095136a49a9d22767b37f48f3404da05056 Reviewed-on: https://go-review.googlesource.com/37197 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-12-11fmt: undo clearflags in catchPanic after error message has been printedMartin Möhrmann
Fixes #18282 Change-Id: I024ca4a03bbbcccd48a0a6245bc3ec22c6a90288 Reviewed-on: https://go-review.googlesource.com/34254 TryBot-Result: Gobot Gobot <gobot@golang.org> Run-TryBot: Martin Möhrmann <moehrmann@google.com> Reviewed-by: Rob Pike <r@golang.org>
2016-12-07fmt: remove unnecessary else statementOdin Ugedal
Change-Id: If30ccfcf56d56fb40102ad567a980793bd39f320 Reviewed-on: https://go-review.googlesource.com/34071 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-10-18fmt: always handle special methods if print operand is a reflect.ValueMartin Möhrmann
Check for and call the special printing and format methods such as String at printing depth 0 when printing the concrete value of a reflect.Value. Fixes: #16015 Change-Id: I23bd2927255b60924e5558321e98dd4a95e11c4c Reviewed-on: https://go-review.googlesource.com/30753 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Martin Möhrmann <martisch@uos.de> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-15fmt: optimize struct layout and intbuf sizeMartin Möhrmann
Move boolean fields to the end of the pp struct for better packing. Increase the fmt.intbuf size to leave no padding space unused at the end of the fmt struct on 32 bit architectures. The size of the pp struct on amd64 is decreased from 192 byte to 184 byte and on 386 from 132 byte to 128 byte. Simplify buffer size calculation in fmt_integer. Consolidate test cases for intbuf overflow checks. amd64 Haswell: name old time/op new time/op delta SprintfPadding-2 227ns ± 2% 227ns ± 1% ~ (p=0.941 n=25+25) SprintfEmpty-2 38.4ns ± 6% 35.8ns ±23% -6.71% (p=0.030 n=24+25) SprintfString-2 100ns ± 0% 101ns ± 0% +1.00% (p=0.000 n=19+18) SprintfTruncateString-2 142ns ± 1% 142ns ± 1% +0.37% (p=0.028 n=25+25) SprintfQuoteString-2 397ns ± 0% 393ns ± 0% -0.82% (p=0.000 n=21+24) SprintfInt-2 101ns ± 2% 102ns ± 4% ~ (p=0.159 n=21+24) SprintfIntInt-2 155ns ± 3% 155ns ± 3% ~ (p=0.934 n=25+25) SprintfPrefixedInt-2 252ns ± 2% 251ns ± 2% ~ (p=0.198 n=25+25) SprintfFloat-2 184ns ± 3% 179ns ± 2% -3.07% (p=0.000 n=24+25) SprintfComplex-2 532ns ± 2% 535ns ± 2% +0.64% (p=0.046 n=25+24) SprintfBoolean-2 90.5ns ± 3% 91.6ns ± 2% +1.17% (p=0.000 n=22+22) SprintfHexString-2 164ns ± 2% 165ns ± 3% ~ (p=0.066 n=25+25) SprintfHexBytes-2 171ns ± 3% 170ns ± 3% ~ (p=0.941 n=25+24) SprintfBytes-2 320ns ± 1% 313ns ± 1% -2.31% (p=0.000 n=24+21) SprintfStringer-2 347ns ± 2% 348ns ± 2% ~ (p=0.426 n=24+24) SprintfStructure-2 753ns ± 1% 742ns ± 1% -1.49% (p=0.000 n=24+25) FprintInt-2 145ns ± 0% 144ns ± 0% -0.69% (p=0.000 n=24+23) FprintfBytes-2 163ns ± 0% 163ns ± 0% -0.27% (p=0.000 n=25+25) FprintIntNoAlloc-2 108ns ± 0% 105ns ± 0% -2.78% (p=0.000 n=25+2 386 Haswell: name old time/op new time/op delta SprintfPadding-2 426ns ± 2% 422ns ± 1% -0.89% (p=0.000 n=25+24) SprintfEmpty-2 24.6ns ± 1% 24.5ns ± 0% -0.39% (p=0.000 n=22+15) SprintfString-2 99.1ns ± 3% 95.6ns ± 0% -3.52% (p=0.000 n=25+24) SprintfTruncateString-2 156ns ± 4% 153ns ± 1% -1.65% (p=0.000 n=24+23) SprintfQuoteString-2 500ns ± 2% 493ns ± 1% -1.49% (p=0.000 n=25+24) SprintfInt-2 92.6ns ± 9% 88.3ns ± 1% -4.72% (p=0.000 n=25+25) SprintfIntInt-2 143ns ± 7% 137ns ± 2% -4.01% (p=0.000 n=23+25) SprintfPrefixedInt-2 268ns ±19% 264ns ±16% ~ (p=0.826 n=24+24) SprintfFloat-2 242ns ± 4% 246ns ± 2% +1.60% (p=0.000 n=25+25) SprintfComplex-2 1.04µs ± 3% 1.03µs ± 1% -0.89% (p=0.026 n=25+25) SprintfBoolean-2 82.2ns ± 9% 80.7ns ± 2% ~ (p=0.163 n=24+24) SprintfHexString-2 240ns ± 5% 224ns ± 2% -6.94% (p=0.000 n=25+25) SprintfHexBytes-2 245ns ± 3% 234ns ± 2% -4.55% (p=0.000 n=25+25) SprintfBytes-2 432ns ±13% 419ns ± 2% ~ (p=0.081 n=23+25) SprintfStringer-2 356ns ± 4% 356ns ± 4% ~ (p=0.988 n=25+25) SprintfStructure-2 968ns ± 5% 948ns ± 2% -2.11% (p=0.000 n=25+25) FprintInt-2 206ns ± 0% 201ns ± 0% -2.43% (p=0.000 n=25+21) FprintfBytes-2 187ns ± 1% 187ns ± 1% ~ (p=0.420 n=25+25) FprintIntNoAlloc-2 173ns ± 0% 168ns ± 0% -2.89% (p=0.000 n=25+2 amd64 Ivy Bridge: name old time/op new time/op delta SprintfPadding-4 203ns ± 4% 210ns ± 8% +3.27% (p=0.000 n=23+25) SprintfEmpty-4 24.4ns ± 2% 24.4ns ± 3% ~ (p=0.487 n=24+25) SprintfString-4 92.4ns ± 2% 93.1ns ± 3% ~ (p=0.087 n=22+25) SprintfTruncateString-4 137ns ± 3% 136ns ± 2% -1.02% (p=0.002 n=25+25) SprintfQuoteString-4 378ns ± 1% 373ns ± 1% -1.32% (p=0.000 n=24+22) SprintfInt-4 89.9ns ± 3% 90.3ns ± 4% ~ (p=0.444 n=25+25) SprintfIntInt-4 137ns ± 4% 138ns ± 3% ~ (p=0.112 n=25+23) SprintfPrefixedInt-4 155ns ±14% 154ns ±14% ~ (p=0.791 n=25+25) SprintfFloat-4 154ns ± 2% 154ns ± 3% ~ (p=0.789 n=25+25) SprintfComplex-4 396ns ± 2% 402ns ± 3% +1.53% (p=0.001 n=23+25) SprintfBoolean-4 71.0ns ± 3% 71.2ns ± 2% ~ (p=0.515 n=25+24) SprintfHexString-4 156ns ± 3% 150ns ± 5% -3.69% (p=0.000 n=24+25) SprintfHexBytes-4 154ns ± 3% 157ns ± 5% +1.72% (p=0.003 n=24+25) SprintfBytes-4 297ns ± 4% 291ns ± 3% -1.86% (p=0.000 n=25+25) SprintfStringer-4 275ns ± 3% 265ns ± 3% -3.51% (p=0.000 n=25+25) SprintfStructure-4 878ns ± 2% 823ns ± 2% -6.21% (p=0.000 n=25+22) FprintInt-4 145ns ± 1% 147ns ± 2% +0.94% (p=0.001 n=23+25) FprintfBytes-4 166ns ± 1% 168ns ± 2% +0.81% (p=0.000 n=24+25) FprintIntNoAlloc-4 113ns ± 2% 109ns ± 2% -3.79% (p=0.000 n=24+25) 386 Ivy Bridge: name old time/op new time/op delta SprintfPadding-4 353ns ± 4% 354ns ± 4% ~ (p=0.769 n=25+24) SprintfEmpty-4 21.9ns ± 6% 21.1ns ± 3% -3.45% (p=0.000 n=24+25) SprintfString-4 94.7ns ± 1% 93.0ns ± 3% -1.77% (p=0.000 n=24+23) SprintfTruncateString-4 150ns ± 2% 147ns ± 0% -1.71% (p=0.000 n=25+21) SprintfQuoteString-4 472ns ± 1% 479ns ± 1% +1.48% (p=0.000 n=25+23) SprintfInt-4 87.0ns ± 2% 85.3ns ± 2% -1.95% (p=0.000 n=25+25) SprintfIntInt-4 137ns ± 2% 134ns ± 2% -1.97% (p=0.000 n=24+23) SprintfPrefixedInt-4 166ns ± 8% 161ns ± 8% -3.07% (p=0.023 n=24+24) SprintfFloat-4 226ns ± 1% 219ns ± 1% -2.97% (p=0.000 n=24+25) SprintfComplex-4 867ns ± 1% 784ns ± 1% -9.47% (p=0.000 n=24+23) SprintfBoolean-4 77.2ns ± 2% 76.0ns ± 2% -1.63% (p=0.000 n=25+25) SprintfHexString-4 212ns ± 2% 214ns ± 2% +0.96% (p=0.000 n=25+25) SprintfHexBytes-4 221ns ± 2% 218ns ± 1% -1.42% (p=0.000 n=25+24) SprintfBytes-4 423ns ± 3% 417ns ± 1% -1.49% (p=0.000 n=25+24) SprintfStringer-4 306ns ± 3% 298ns ± 3% -2.57% (p=0.000 n=24+25) SprintfStructure-4 1.00µs ± 2% 0.98µs ± 2% -1.34% (p=0.000 n=24+24) FprintInt-4 202ns ± 3% 197ns ± 2% -2.04% (p=0.000 n=25+25) FprintfBytes-4 186ns ± 2% 184ns ± 2% -0.88% (p=0.000 n=24+25) FprintIntNoAlloc-4 170ns ± 2% 166ns ± 2% -2.26% (p=0.000 n=24+25) Change-Id: I46e62bf8b6afa90a24f75b40f1d354b2084b910b Reviewed-on: https://go-review.googlesource.com/20984 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2016-04-03fmt: hold on to all free buffers, regardless of sizeBrad Fitzpatrick
This code made sense before fmt switched to using sync.Pool, but a sync.Pool clears all items on GC, so not reusing something based on size is just a waste of memory. Change-Id: I201312b0ee6c572ff3c0ffaf71e42623a160d23f Reviewed-on: https://go-review.googlesource.com/21480 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-27fmt: unify array and slice formatting for bytes and other typesMartin Möhrmann
Make verbs b,c,o and U work for any array and slice of integer type including byte and uint8. Fix a bug that triggers badverb for []uint8 and []byte type on the slice/array level instead of on each element like for any other slice or array type. Add tests that make sure we do not accidentally alter the behavior of printing []byte for []byte and []uint8 type if they are used at the top level when formatting with %#v. name old time/op new time/op delta SprintfHexBytes-2 177ns ± 2% 176ns ± 2% ~ (p=0.066 n=48+49) SprintfBytes-2 330ns ± 1% 329ns ± 1% ~ (p=0.118 n=45+47) Fixes #13478 Change-Id: I99328a184973ae219bcc0f69c3978cb1ff462888 Reviewed-on: https://go-review.googlesource.com/20686 Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2016-03-27fmt: cleanup reflect value handlingMartin Möhrmann
Merge printReflectValue into printValue. Determine if handleMethods was already called in printArg by checking if depth is 0. Do not call handleMethods on depth 0 again in printValue to not introduce a performance regression. handleMethods is called already in printArg to not introduce a performance penalty for top-level Stringer, GoStringer, Errors and Formatters by using reflect.ValueOf on them just to retrieve them again as interface{} values in printValue. Clear p.arg in printValue after handleMethods to print the type of the value inside the reflect.Value when a bad verb is encountered on the top level instead of printing "reflect.Value=" as the type of the argument. This also fixes a bug that incorrectly prints the whole map instead of just the value for a key if the returned value by the map for the key is an invalid reflect value. name old time/op new time/op delta SprintfPadding-2 229ns ± 2% 227ns ± 1% -0.50% (p=0.013 n=20+20) SprintfEmpty-2 36.4ns ± 6% 37.2ns ±14% ~ (p=0.091 n=18+20) SprintfString-2 102ns ± 1% 102ns ± 0% ~ (p=0.751 n=20+20) SprintfTruncateString-2 142ns ± 0% 141ns ± 1% -0.95% (p=0.000 n=16+20) SprintfQuoteString-2 389ns ± 0% 388ns ± 0% -0.12% (p=0.019 n=20+20) SprintfInt-2 100ns ± 2% 100ns ± 1% ~ (p=0.188 n=20+15) SprintfIntInt-2 155ns ± 3% 154ns ± 2% ~ (p=0.092 n=20+20) SprintfPrefixedInt-2 250ns ± 2% 251ns ± 3% ~ (p=0.559 n=20+20) SprintfFloat-2 177ns ± 2% 175ns ± 1% -1.30% (p=0.000 n=20+20) SprintfComplex-2 516ns ± 1% 510ns ± 1% -1.13% (p=0.000 n=19+16) SprintfBoolean-2 90.9ns ± 3% 90.6ns ± 1% ~ (p=0.193 n=19+19) SprintfHexString-2 171ns ± 1% 169ns ± 1% -1.44% (p=0.000 n=19+20) SprintfHexBytes-2 180ns ± 1% 180ns ± 1% ~ (p=0.060 n=19+18) SprintfBytes-2 330ns ± 1% 329ns ± 1% -0.42% (p=0.003 n=20+20) SprintfStringer-2 354ns ± 3% 352ns ± 3% ~ (p=0.525 n=20+19) SprintfStructure-2 804ns ± 3% 776ns ± 2% -3.56% (p=0.000 n=20+20) FprintInt-2 155ns ± 0% 151ns ± 1% -2.35% (p=0.000 n=19+20) FprintfBytes-2 169ns ± 0% 170ns ± 1% +0.81% (p=0.000 n=18+19) FprintIntNoAlloc-2 112ns ± 0% 109ns ± 1% -2.28% (p=0.000 n=20+20) Change-Id: Ib9a39082ed1be0f1f7499ee6fb6c9530f043e43a Reviewed-on: https://go-review.googlesource.com/20923 Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2016-03-26fmt: split doPrint into two specialized functionsMartin Möhrmann
Remove format flag reset from doPrint. Flags will not be set in doPrint and printArg will not return with flags modified. Remove the extra arguments addspace and addnewline and split up doPrint into two simpler and specialized functions. Change-Id: Ib884d027abfbb31c6f01b008f51d6d76fc0c1a17 Reviewed-on: https://go-review.googlesource.com/21181 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>