bufio: add fast path to WriteString for strings that fit in the buffer - #80693
bufio: add fast path to WriteString for strings that fit in the buffer#80693davidteather wants to merge 1 commit into
Conversation
|
This PR (HEAD: a645f77) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/809320. Important tips:
|
|
Message from David Teather: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
|
Message from Ian Lance Taylor: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
|
Message from Jorropo: Patch Set 1: Code-Review+2 Commit-Queue+1 (4 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 1: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2026-08-03T06:20:55Z","revision":"7b84ac61db2901dc022315129b48fdb802bd6e58"} Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
|
Message from Jorropo: Patch Set 1: -Commit-Queue (Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_55763>) Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 1: This CL has passed the run Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
|
Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com: Patch Set 1: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
WriteString sets up its io.StringWriter fallback before checking
whether the string simply fits in the remaining buffer space, which
is the common case. The setup is not free: the interface value is
zeroed and state is spilled on every call, including the calls that
never use the fallback, making WriteString measurably slower than
Write for an identical payload that fits.
Handle the fits-in-buffer case first. The fallback loop then only
runs when the string does not fit, so its condition is known true on
entry: test it at the bottom of the loop instead so it is not
evaluated twice. The fallback path gets slightly faster as well.
goos: darwin
goarch: arm64
pkg: bufio
cpu: Apple M2 Pro
│ old │ new │
│ sec/op │ sec/op vs base │
WriterCopyOptimal-10 49.77n ± 0% 49.61n ± 1% -0.32% (p=0.017 n=25)
Wrial-10 49.98n ± 1% 49.82n ± 1% ~ (p=0.655 n=25)
WriterCopyNoReadFrom-10 2.295µ ± 2% 2.240µ ± 2% -2.40% (p=0.050 n=25)
WriterEmpty-10 470.2n ± 2% 463.3n ± 2% ~ (p=0.209 n=25)
WriterFlush-10 5.052n ± 1% 4.771n ± 1% -5.56% (p=0.000 n=25)
WriteString/small-10 4.774n ± 0% 3.529n ± 0% -26.08% (p=0.000 n=25)
WriteString/huge-10 5.757n ± 0% 5.469n ± 0% -5.00% (p=0.000 n=25)
geomean 44.98n 42.15n -6.31%
B/op and allocs/op are unchanged.
Fixes golang#80692
a645f77 to
d509a2c
Compare
|
This PR (HEAD: d509a2c) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/809320. Important tips:
|
|
Message from David Teather: Patch Set 2: (4 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/809320. |
WriteString sets up its io.StringWriter fallback before checking
whether the string simply fits in the remaining buffer space, which
is the common case. The setup is not free: the interface value is
zeroed and state is spilled on every call, including the calls that
never use the fallback, making WriteString measurably slower than
Write for an identical payload that fits.
Handle the fits-in-buffer case first. The fallback loop then only
runs when the string does not fit, so its condition is known true on
entry: test it at the bottom of the loop instead so it is not
evaluated twice. The fallback path gets slightly faster as well.
goos: darwin
goarch: arm64
pkg: bufio
cpu: Apple M2 Pro
│ old │ new │
│ sec/op │ sec/op vs base │
WriterCopyOptimal-10 49.77n ± 0% 49.61n ± 1% -0.32% (p=0.017 n=25)
WriterCopyUnoptimal-10 49.98n ± 1% 49.82n ± 1% ~ (p=0.655 n=25)
WriterCopyNoReadFrom-10 2.295µ ± 2% 2.240µ ± 2% -2.40% (p=0.050 n=25)
WriterEmpty-10 470.2n ± 2% 463.3n ± 2% ~ (p=0.209 n=25)
WriterFlush-10 5.052n ± 1% 4.771n ± 1% -5.56% (p=0.000 n=25)
WriteString/small-10 4.774n ± 0% 3.529n ± 0% -26.08% (p=0.000 n=25)
WriteString/huge-10 5.757n ± 0% 5.469n ± 0% -5.00% (p=0.000 n=25)
geomean 44.98n 42.15n -6.31%
B/op and allocs/op are unchanged.
Fixes #80692