Skip to content

bufio: add fast path to WriteString for strings that fit in the buffer - #80693

Open
davidteather wants to merge 1 commit into
golang:masterfrom
davidteather:bufio-writestring-fast-path
Open

bufio: add fast path to WriteString for strings that fit in the buffer#80693
davidteather wants to merge 1 commit into
golang:masterfrom
davidteather:bufio-writestring-fast-path

Conversation

@davidteather

@davidteather davidteather commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

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

@gopherbot

Copy link
Copy Markdown
Contributor

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:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from David Teather:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/809320.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Ian Lance Taylor:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/809320.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

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.
After addressing review feedback, remember to publish your drafts!

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
@davidteather
davidteather force-pushed the bufio-writestring-fast-path branch from a645f77 to d509a2c Compare August 4, 2026 02:03
@gopherbot

Copy link
Copy Markdown
Contributor

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:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from David Teather:

Patch Set 2:

(4 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/809320.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bufio: WriteString is slower than Write for a payload that fits in the buffer

2 participants