Skip to content

Format TimeSpan with CoreLib's TimeSpanFormat from the framework's IL (#447) - #537

Open
jonathanperis wants to merge 2 commits into
mainfrom
feat/timespan-format-from-il
Open

jonathanperis wants to merge 2 commits into
mainfrom
feat/timespan-format-from-il

Conversation

@jonathanperis

@jonathanperis jonathanperis commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Roadmap flip (#447). Part of the IL-first tracker; #447 is already closed (moved into #475's checklist).

What changed and why

TimeSpan was the last date and time type whose formatting was still csharpc's own copy. Its formatting now comes from CoreLib's TimeSpanFormat, served from the framework's IL like DateTimeFormat since #426:

  • host:System.TimeSpan::this.ToString,this.TryFormat in src/Csharpc/upstream-types/System.Globalization.txt. Every ToString overload and TryFormat (UTF-16 and UTF-8) is now TimeSpan's own IL.
  • The compiler's remaining entry points call TimeSpanFormat.FormatC/Format through [UnsafeAccessor]s in corelib/Globalization/DateTimeFormatting.cs. Those entry points are interpolation holes (csrt_timespan_to_string/csrt_timespan_format), boxed TimeSpans in composite formatting (csrt_timespan_format_with) and Stopwatch.ToString.
  • Deleted:
    • DateTimeOps' standard and custom TimeSpan formatter and its helpers (TimeSpanStandard, TimeSpanCustom, TimeSpanFormatWith, quote/escape parsing);
    • the TimeSpanTryFormat shadow members, which formatted a whole string and then copied it;
    • the three BclValueTypes ToString mappings.

In total, 170 lines deleted and 33 added under src/.

TimeSpan's representation and arithmetic stay the corelib's (out of scope), and so does TimeZoneInfo (#98).

Measurements (linux-x86_64, stripped -O2, same host)

before (corelib formatter) after (CoreLib IL) NativeAOT
timespan_format_throughput run, median of 7 384 ms 269 ms (0.70x); measure-aot.sh 263 ms 258 ms (csharpc 1.02x)
instructions (callgrind) 3 924 M 2 080 M (−47 %) 2 511 M
timespan_format_throughput size 362 976 B 411 536 B (+48 560) 1 678 960 B
compile cpu 9.4 s 12.6 s publish
  • Benchmark: tools/il-first/bench/timespan_format_throughput.cs (new). It covers ToString(), "g"/"G" under the invariant culture and fr-FR, custom formats, TryFormat in UTF-16 and UTF-8, and interpolation.
  • Size of other programs:
    • Programs that format TimeSpans grow with the framework's generic TryFormatStandard/FormatCustomized (char and byte): sample 1610 +29 328 B, 186 +19 504 B. Sample 82 shrinks by 144 B.
    • regex and regexoptions grow by 4 416 B each. Both already rooted the boxed-TimeSpan exports through the match timeout.
    • hello, exceptions, typeinfo, async, collections, json, jsonwriter, linq, net, numbers and rsa are unchanged.
    • tests/size.sh stays within every budget (14 of 14), so no budget is raised.
  • Size vs speed: every size increase here comes with the measured speed gain above.

What the tests prove

  • TimeSpan samples: 82, 164, 186, 361, 376, 490, 509, 513, 542, 657, 864, 1224, 1252 and 1545, plus the two new ones, are byte-identical to real .NET at -O0 and -O2 (32 runs) and via IL (16).
  • Full suite: tests/run.sh --opt -O2, all 4 shards, 882 of 882. tests/slicer.sh 10 of 10.
  • New samples, recorded from .NET:
    • 1610_timespan_format_edges:
      • every overload on zero, ±1 tick, negative values, MinValue/MaxValue;
      • custom formats with quotes, escapes and %;
      • "c"/"g"/"G" under six cultures (fr-FR, de-DE, ar-SA and fa-IR decimal separators);
      • composite and interpolation holes with alignment, and boxed, IFormattable and ISpanFormattable calls;
      • TryFormat at every destination size in UTF-16 and UTF-8. Each returns false with 0 written until the text fits, and the sample checks that the two elements after the destination are untouched.
      • invalid formats through TryFormat in both encodings, and Stopwatch.
      • It is live on macOS/Windows (OS ICU) and skipped on WASI (named cultures).
    • 1611_timespan_format_invariant: invariant globalization mode, the one that runs on wasm32. "g"/"G" take their decimal separator from the culture's data, not its NumberFormat, and named cultures throw CultureNotFoundException.
  • No silent difference: the corelib's old formatter printed the same text on both new samples and on 82 and 186. So the flip fixes no wrong output; what it adds is CoreLib's code, its speed, and TryFormat writing straight into the destination.
  • wasm32: no WASI SDK on the dev host, so the wasm32 run is CI's.

What stays different

Nothing in TimeSpan formatting. TimeSpan's representation and arithmetic, and TimeZoneInfo, are out of scope.

Docs: a new row in docs/il-first/System.Globalization.md; remaining-flips.md and docs/06-roadmap.md now list the flip as done.


Note

Medium Risk
Touches globalization formatting paths and compiler-hosted BCL members with span write semantics; mitigated by extensive differential samples and explicit TryFormat overrun checks.

Overview
Completes the IL-first globalization flip for TimeSpan formatting (#447): public TimeSpan ToString/TryFormat (UTF-16 and UTF-8) are now hosted from framework IL via host:System.TimeSpan::this.ToString,this.TryFormat in System.Globalization.txt, using CoreLib's TimeSpanFormat like DateTimeFormat since #426.

Removed the corelib's custom TimeSpan formatter (~170 lines in DateTimeOps: standard/custom format paths, quote/escape parsing), TimeSpanTryFormat shadows (format-then-copy), and three BclValueTypes codegen mappings for TimeSpan.ToString. Compiler entry points (interpolation, boxed TimeSpan in composite formatting, Stopwatch.ToString) still go through runtime exports but now call TimeSpanFormat.FormatC/Format via new [UnsafeAccessor] wrappers in DateTimeFormatting.cs.

Adds regression samples 1610 (culture/custom/TryFormat bounds) and 1611 (invariant mode), timespan_format_throughput bench, and doc/tracker updates marking #447 done. Measured on linux-x86_64: throughput ~0.70x wall time, −47% instructions; some binaries grow from framework TryFormat generics while size budgets stay within limits.

Reviewed by Cursor Bugbot for commit 8fb0616. Bugbot is set up for automated code reviews on this repo. Configure here.

…#447)

TimeSpan's ToString overloads and TryFormat (UTF-16 and UTF-8) are now hosted from TimeSpan's own
IL through `this.` roots in upstream-types/System.Globalization.txt, so they run CoreLib's
TimeSpanFormat like every other date and time formatter since #426. The compiler's remaining entry
points (interpolation holes, boxed TimeSpans in composite formatting, Stopwatch.ToString) call
TimeSpanFormat.FormatC/Format through UnsafeAccessors in corelib/Globalization/DateTimeFormatting.cs.

Deleted: DateTimeOps' standard and custom TimeSpan formatter and its helpers, the TimeSpanTryFormat
shadow members (which formatted a whole string and copied it) and the three BclValueTypes ToString
mappings.

New samples recorded from .NET: 1604 (every overload on negative and extreme values, custom formats
with quotes and escapes, "c"/"g"/"G" under six cultures, composite and interpolation holes, TryFormat
at every destination size in UTF-16 and UTF-8, invalid formats through TryFormat, Stopwatch) and 1605
(invariant globalization mode). The corelib's formatter printed the same text on both: no silent
difference was found.

Measured on linux-x86_64 (stripped -O2, median of 7, same host):
- timespan_format_throughput (new, tools/il-first/bench): 384 to 269 ms (0.70x), 3 924 M to 2 080 M
  instructions; NativeAOT 258 ms and 2 511 M instructions (csharpc 1.02x of its time).
- size: timespan_format_throughput 362 976 to 411 536 B (NativeAOT 1 678 960), sample 1604 +29 328,
  186 +19 504, 82 -144; regex and regexoptions +4 416 (they root the boxed-TimeSpan exports through the
  match timeout); the other size programs unchanged. Every tests/size.sh budget still holds, none raised.
…from-il

# Conflicts:
#	docs/06-roadmap.md
#	tests/platform-skips.txt
@jonathanperis jonathanperis added this to the Drop-in for large apps milestone Nov 30, 2026
@jonathanperis jonathanperis added enhancement New feature or request area: bcl Library surface (corelib) labels Nov 30, 2026
@jonathanperis jonathanperis self-assigned this Nov 30, 2026
@cursor

cursor Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 7a16fec7-4924-48e0-be3c-6b3e0c67cab0)

This branch has not been deployed

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

Labels

area: bcl Library surface (corelib) enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant