Skip to content

Simplify TryParseFormatO fractional-seconds handling#129005

Merged
danmoseley merged 1 commit into
dotnet:mainfrom
danmoseley:runtime-simplify-parseo-fraction
Jun 5, 2026
Merged

Simplify TryParseFormatO fractional-seconds handling#129005
danmoseley merged 1 commit into
dotnet:mainfrom
danmoseley:runtime-simplify-parseo-fraction

Conversation

@danmoseley
Copy link
Copy Markdown
Member

Simplify TryParseFormatO fractional-seconds handling

The "O" (ISO 8601 round-trip) format always has exactly 7 fractional-second digits, which matches DateTime's tick precision (TimeSpan.TicksPerSecond == 10_000_000). The integer formed by those seven digits is therefore exactly the sub-second tick count, so we can pass it straight to TryAddTicks instead of routing it through a double divide / multiply / Math.Round round-trip.

The change is bit-exact: every one of the 10M possible 7-digit values produces the same tick count as before (verified by brute-force).

Tests

Existing System.Tests.DateTimeTests (1378 tests) and System.Tests.DateTimeOffsetTests (737 tests, 2 platform-skipped) pass against a CoreLib built with the change.

Microbenchmark

Side benefit: a small (~6%) speedup on Perf_DateTime.ParseO on R2R'd CoreLib.

Benchmarks used (unchanged, from dotnet/performance):

Run with BenchmarkDotNet Job.Default, same-process A/B (both --coreRuns passed in one invocation so process-startup, ASLR, and thermal effects don't bias the comparison), affinity-pinned to a single P-core:

dotnet run -c Release -f net11.0 --project src/benchmarks/micro/MicroBenchmarks.csproj -- \
  --filter "System.Tests.Perf_DateTime.ParseO" "System.Tests.Perf_DateTime.ParseR" \
  --coreRun <baseline-testhost>\CoreRun.exe <patched-testhost>\CoreRun.exe \
  --affinity 1

Environment: Windows 11, Intel Core i9-14900K (P-core 0 pinned), .NET 11.0 R2R'd System.Private.CoreLib (Release), BenchmarkDotNet v0.16.0-nightly.

Method Job Mean Error StdDev Ratio RatioSD
ParseR baseline 11.42 ns 0.168 ns 0.149 ns 1.00 0.00
ParseR patched 11.41 ns 0.163 ns 0.144 ns 1.00 0.02
ParseO baseline 11.87 ns 0.198 ns 0.175 ns 1.00 0.00
ParseO patched 11.14 ns 0.125 ns 0.111 ns 0.94 0.02

ParseR (control) is identical between the two binaries, confirming the harness is stable; ParseO improves ~6% (~0.73 ns/call).

Note

This PR was prepared with the assistance of GitHub Copilot.

The "O" (ISO 8601 round-trip) format always has exactly 7 fractional-second
digits, which matches DateTime's tick precision (TimeSpan.TicksPerSecond
is 10_000_000). The integer formed by those seven digits is therefore
exactly the sub-second tick count, so we can pass it straight to
TryAddTicks instead of routing it through a double divide / multiply /
Math.Round round-trip. Bit-exact equivalent.

Side benefit: small (~6%) speedup on Perf_DateTime.ParseO on R2R'd
CoreLib (i9-14900K), measured with a same-process A/B against an
unchanged Perf_DateTime.ParseR control.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.

@danmoseley danmoseley requested a review from tarekgh June 4, 2026 18:44
@danmoseley
Copy link
Copy Markdown
Member Author

I was testing copilot and had it do something useful 😄

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new

Copilot stopped reviewing on behalf of danmoseley due to an error June 4, 2026 19:37
Copy link
Copy Markdown
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants