Add missing fast path for DateTime format "G"#129374
Open
danmoseley wants to merge 1 commit into
Open
Conversation
Mirrors the existing single-char fast paths for 'o', 'r', 's', 'u' and the null/empty-format invariant fast path that ToString(InvariantCulture) already uses, routing through TryFormatInvariantG instead of FormatCustomized. Output is byte-identical to the existing pattern-based path ("MM/dd/yyyy HH:mm:ss", 19 chars). ~5.5x faster (41.58 ns -> 7.54 ns median).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-datetime |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an invariant-culture fast path for the standard "G" format when formatting a DateTime (i.e., offset.Ticks == NullOffset) by directly using the existing TryFormatInvariantG implementation. This aligns "G" with the existing single-character fast paths (e.g., "o", "r", "s", "u") and the already-existing null/empty-format invariant fast path.
Changes:
- Add a
"G"switch case inDateTimeFormat.Formatthat fast-pathsDateTime.ToString("G", InvariantCulture)toTryFormatInvariantG. - Add the corresponding
"G"fast path inDateTimeFormat.TryFormat<TChar>for invariant culture +DateTime(NullOffset) scenarios.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs | Adds an invariant "G" fast path in both Format and TryFormat<TChar> using TryFormatInvariantG when offset is NullOffset. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
Open
3 tasks
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.
Adds a fast path for
DateTime.ToString("G", CultureInfo.InvariantCulture)similar to existing single-char fast path that'o','r','s', and'u'already have inDateTimeFormat.Format/TryFormat<TChar>, and identical to the one the null/empty-format case (dt.ToString(CultureInfo.InvariantCulture)) already takes viaTryFormatInvariantG.The fast path is gated by
offset.Ticks == NullOffset && ReferenceEquals(dtfi, DateTimeFormatInfo.InvariantInfo), soDateTimeOffset.ToString("G", InvariantCulture)and any custom/clonedDateTimeFormatInfofall through to the existing pattern-based path unchanged.Perf Results
5.5x faster
ResultsComparer, threshold 1%, noise 1 ns, 5 launches × 15 iterations × 16384 invocations, P-core pinned on i9-14900K:The remaining 19
Perf_DateTime.ToString*cases (other formats, current-culture,DateTimeOffset) are unchanged. Allocation unchanged everywhere.ToStringInvariant(format)is a new benchmark inPerf_DateTime.csthat callsdate.ToString(format, CultureInfo.InvariantCulture).Tests
Output is byte-identical to the slow path for
DateTime+ invariant culture ("MM/dd/yyyy HH:mm:ss", 19 chars). ExistingDateTime/DateTimeOffsetformatting tests pass. In addition, an ad-hoc correctness harness compared baseline vs. modified outputs over 10 DateTimes (incl.MinValue,MaxValue, year 1, year 9999, Feb 29 2000, allDateTimeKinds) × 5 cultures × 16 formats × {ToString,TryFormat} plus DateTimeOffset equivalents — 1040 byte-identical outputs. Separately verifiedTryFormatwith too-small destinations (sizes 0..20 char, 0..25 UTF-8), composite formatting$"{dt:G}", cloned DTFI, and DTFI with customizedLongTimePattern— all behave identically to baseline.Note
This PR description was drafted by GitHub Copilot.