Repository navigation
Format TimeSpan with CoreLib's TimeSpanFormat from the framework's IL (#447) - #537
Open
jonathanperis wants to merge 2 commits into
Open
jonathanperis wants to merge 2 commits into
jonathanperis wants to merge 2 commits into
Conversation
…#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
Contributor
Bugbot couldn't run - usage limit reachedBugbot 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
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.
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 likeDateTimeFormatsince #426:host:System.TimeSpan::this.ToString,this.TryFormatinsrc/Csharpc/upstream-types/System.Globalization.txt. EveryToStringoverload andTryFormat(UTF-16 and UTF-8) is now TimeSpan's own IL.TimeSpanFormat.FormatC/Formatthrough[UnsafeAccessor]s incorelib/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) andStopwatch.ToString.DateTimeOps' standard and custom TimeSpan formatter and its helpers (TimeSpanStandard,TimeSpanCustom,TimeSpanFormatWith, quote/escape parsing);TimeSpanTryFormatshadow members, which formatted a whole string and then copied it;BclValueTypesToStringmappings.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)
timespan_format_throughputrun, median of 7timespan_format_throughputsizetools/il-first/bench/timespan_format_throughput.cs(new). It coversToString(), "g"/"G" under the invariant culture and fr-FR, custom formats,TryFormatin UTF-16 and UTF-8, and interpolation.TryFormatStandard/FormatCustomized(char and byte): sample 1610 +29 328 B, 186 +19 504 B. Sample 82 shrinks by 144 B.tests/size.shstays within every budget (14 of 14), so no budget is raised.What the tests prove
tests/run.sh --opt -O2, all 4 shards, 882 of 882.tests/slicer.sh10 of 10.MinValue/MaxValue;%;IFormattableandISpanFormattablecalls;TryFormatat 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.TryFormatin both encodings, andStopwatch.NumberFormat, and named cultures throwCultureNotFoundException.TryFormatwriting straight into the destination.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.mdanddocs/06-roadmap.mdnow 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
TimeSpanToString/TryFormat(UTF-16 and UTF-8) are now hosted from framework IL viahost:System.TimeSpan::this.ToString,this.TryFormatinSystem.Globalization.txt, using CoreLib'sTimeSpanFormatlikeDateTimeFormatsince #426.Removed the corelib's custom TimeSpan formatter (~170 lines in
DateTimeOps: standard/custom format paths, quote/escape parsing),TimeSpanTryFormatshadows (format-then-copy), and threeBclValueTypescodegen mappings forTimeSpan.ToString. Compiler entry points (interpolation, boxedTimeSpanin composite formatting,Stopwatch.ToString) still go through runtime exports but now callTimeSpanFormat.FormatC/Formatvia new[UnsafeAccessor]wrappers inDateTimeFormatting.cs.Adds regression samples 1610 (culture/custom/
TryFormatbounds) and 1611 (invariant mode),timespan_format_throughputbench, and doc/tracker updates marking #447 done. Measured on linux-x86_64: throughput ~0.70x wall time, −47% instructions; some binaries grow from frameworkTryFormatgenerics 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.