Skip to content

time: add a fast path for the IMF-fixdate layout in AppendFormat - #80752

Closed
davidteather wants to merge 1 commit into
golang:masterfrom
davidteather:time-appendformat-http-date-fast-path
Closed

time: add a fast path for the IMF-fixdate layout in AppendFormat#80752
davidteather wants to merge 1 commit into
golang:masterfrom
davidteather:time-appendformat-http-date-fast-path

Conversation

@davidteather

@davidteather davidteather commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

AppendFormat special-cases RFC3339 and RFC3339Nano, every other layout
falls through to the general formatter, which re-parses the layout on
each call. IMF-fixdate is the format RFC 9110 requires HTTP senders to
generate, so it is re-parsed once per response by every call site that
writes a date header.

Add a specialization for it. GMT is a literal in this layout rather
than a zone abbreviation, so the time is formatted in its own location
exactly as the general path formats it, supplying UTC remains the
caller's responsibility.

The switch dispatches on layout length before comparing contents, so
RFC3339 is unaffected. Layouts sharing this one's length pay one
additional comparison. Both are covered by the benchmarks below.

goos: darwin
goarch: arm64
pkg: time
cpu: Apple M2 Pro
│ old │ new │
│ sec/op │ sec/op vs base │
Format-10 169.4n ± 0% 169.5n ± 0% ~ (p=0.697 n=25)
FormatRFC3339-10 107.9n ± 0% 107.8n ± 0% ~ (p=0.821 n=25)
FormatRFC3339Nano-10 109.9n ± 0% 109.6n ± 0% ~ (p=0.273 n=25)
FormatIMFFixdate-10 179.0n ± 1% 104.6n ± 0% -41.56% (p=0.000 n=25)
FormatRFC1123-10 179.8n ± 0% 180.0n ± 0% ~ (p=0.534 n=25)
FormatNow-10 105.1n ± 0% 105.8n ± 1% +0.67% (p=0.000 n=25)
geomean 137.6n 125.9n -8.49%

B/op and allocs/op are unchanged.

No caller needs to change. In the standard library this layout is used
by net/http, net/http/internal/http2, net/http/fcgi and net/http's file
server. net/http's Date header moved from a hand-rolled formatter to
AppendFormat in CL 647955. This makes that simplification free.

Fixes #80751

AppendFormat special-cases RFC3339 and RFC3339Nano, every other layout
falls through to the general formatter, which re-parses the layout on
each call. IMF-fixdate is the format RFC 9110 requires HTTP senders to
generate, so it is re-parsed once per response by every call site that
writes a date header.

Add a specialization for it. GMT is a literal in this layout rather
than a zone abbreviation, so the time is formatted in its own location
exactly as the general path formats it, supplying UTC remains the
caller's responsibility.

The switch dispatches on layout length before comparing contents, so
RFC3339 is unaffected. Layouts sharing this one's length pay one
additional comparison. Both are covered by the benchmarks below.

goos: darwin
goarch: arm64
pkg: time
cpu: Apple M2 Pro
                     │     old     │                 new                 │
                     │   sec/op    │   sec/op     vs base                │
Format-10              169.4n ± 0%   169.5n ± 0%        ~ (p=0.697 n=25)
FormatRFC3339-10       107.9n ± 0%   107.8n ± 0%        ~ (p=0.821 n=25)
FormatRFC3339Nano-10   109.9n ± 0%   109.6n ± 0%        ~ (p=0.273 n=25)
FormatIMFFixdate-10    179.0n ± 1%   104.6n ± 0%  -41.56% (p=0.000 n=25)
FormatRFC1123-10       179.8n ± 0%   180.0n ± 0%        ~ (p=0.534 n=25)
FormatNow-10           105.1n ± 0%   105.8n ± 1%   +0.67% (p=0.000 n=25)
geomean                137.6n        125.9n        -8.49%

B/op and allocs/op are unchanged.

No caller needs to change. In the standard library this layout is used
by net/http, net/http/internal/http2, net/http/fcgi and net/http's file
server. net/http's Date header moved from a hand-rolled formatter to
AppendFormat in CL 647955. This makes that simplification free.

Fixes golang#80751
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 2485c39) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/811180.

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 Gopher Robot:

Patch Set 1:

(1 comment)


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

@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/811180.
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.

time: optimize AppendFormat for the HTTP date layout (IMF-fixdate)

2 participants