time: add a fast path for the IMF-fixdate layout in AppendFormat - #80752
Closed
davidteather wants to merge 1 commit into
Closed
time: add a fast path for the IMF-fixdate layout in AppendFormat#80752davidteather wants to merge 1 commit into
davidteather wants to merge 1 commit into
Conversation
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
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:
|
Contributor
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/811180. |
Contributor
|
Message from David Teather: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/811180. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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