dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.23k stars 4.45k forks source link

Refactor some DateTime and TimeSpan formatting/parsing methods #101640

Closed lilinus closed 1 week ago

lilinus commented 2 weeks ago
dotnet-policy-service[bot] commented 2 weeks ago

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

danmoseley commented 2 weeks ago

Are these sufficiently covered in dotnet/performance? Might be worth running appropriate benchmarks there.

tarekgh commented 1 week ago

@lilinus could you please merge the latest change from main to this PR to be able to proceed?

lilinus commented 1 week ago

@tarekgh, done. Thanks for the feedback etc. I also tried benchmarking the changes but had trouble with antivirus stopping the runtime.

tarekgh commented 1 week ago

I also tried benchmarking the changes but had trouble with antivirus stopping the runtime.

Is it possible you can temporary disable the antivirus? Or add exclusion to the runtime and test folders to have the antivirus ignore them?

lilinus commented 1 week ago

Managed to get it working. Benchmarked some DateTime and TimeSpan parsing. Results are looking good IMO. Link to benchmarks: here

BenchmarkDotNet v0.13.13-nightly.20240311.145, Windows 10 (10.0.19044.3086/21H2/November2021Update)
AMD Ryzen 9 4900HS with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK 9.0.100-preview.3.24204.13
  [Host]     : .NET 9.0.0 (9.0.24.17209), X64 RyuJIT AVX2
  Job-GLLMPZ : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2
  Job-AQRKOX : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2

PowerPlanMode=00000000-0000-0000-0000-000000000000  IterationTime=250ms  MaxIterationCount=20
MinIterationCount=15  WarmupCount=1

| Method          | Job        | Toolchain                                                                                                | arg1                      | arg2                                  | Mean      | Error    | StdDev   | Median    | Min       | Max       | Ratio | RatioSD | Gen0   | Allocated | Alloc Ratio |
| TimeSpan_Parse  | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 0.00.00.00.123            | d\\.hh\\.mm\\.ss\\.FFFFF              |  78.58 ns | 1.200 ns | 1.123 ns |  78.43 ns |  76.52 ns |  80.90 ns |  1.00 |    0.02 |      - |         - |          NA |
| TimeSpan_Parse  | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 0.00.00.00.123            | d\\.hh\\.mm\\.ss\\.FFFFF              |  64.23 ns | 0.889 ns | 0.831 ns |  64.38 ns |  62.55 ns |  65.61 ns |  0.82 |    0.02 |      - |         - |          NA |
| TimeSpan_Parse  | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 0.00.00.00.12345          | d\\.hh\\.mm\\.ss\\.fffff              |  72.58 ns | 0.264 ns | 0.234 ns |  72.57 ns |  72.30 ns |  73.05 ns |  1.00 |    0.00 |      - |         - |          NA |
| TimeSpan_Parse  | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 0.00.00.00.12345          | d\\.hh\\.mm\\.ss\\.fffff              |  63.92 ns | 0.693 ns | 0.614 ns |  63.78 ns |  62.92 ns |  64.95 ns |  0.88 |    0.01 |      - |         - |          NA |
| DateTime_Format | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 01/01/0001 00:00:12       | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.fff   | 213.02 ns | 0.465 ns | 0.388 ns | 212.95 ns | 212.32 ns | 213.69 ns |  1.00 |    0.00 | 0.0337 |      72 B |        1.00 |
| DateTime_Format | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 01/01/0001 00:00:12       | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.fff   | 179.06 ns | 0.303 ns | 0.268 ns | 179.04 ns | 178.52 ns | 179.53 ns |  0.84 |    0.00 | 0.0343 |      72 B |        1.00 |
| DateTime_Format | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 01/01/0001 00:00:12       | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.FFFFF | 146.01 ns | 0.579 ns | 0.542 ns | 145.84 ns | 145.43 ns | 147.21 ns |  1.00 |    0.01 | 0.0305 |      64 B |        1.00 |
| DateTime_Format | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 01/01/0001 00:00:12       | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.FFFFF | 109.59 ns | 0.142 ns | 0.126 ns | 109.61 ns | 109.28 ns | 109.79 ns |  0.75 |    0.00 | 0.0304 |      64 B |        1.00 |
| TimeSpan_Format | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 00:00:12.3456789          | d\\.hh\\.mm\\.ss\\.fffffff            | 177.14 ns | 0.506 ns | 0.473 ns | 177.02 ns | 176.46 ns | 178.08 ns |  1.00 |    0.00 | 0.0303 |      64 B |        1.00 |
| TimeSpan_Format | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 00:00:12.3456789          | d\\.hh\\.mm\\.ss\\.fffffff            | 164.35 ns | 0.436 ns | 0.407 ns | 164.33 ns | 163.68 ns | 165.22 ns |  0.93 |    0.00 | 0.0301 |      64 B |        1.00 |
| TimeSpan_Format | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 00:00:12.3456789          | d\\.hh\\.mm\\.ss\\.FFFFFFF            | 184.79 ns | 0.532 ns | 0.497 ns | 184.62 ns | 184.17 ns | 185.89 ns |  1.00 |    0.00 | 0.0300 |      64 B |        1.00 |
| TimeSpan_Format | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 00:00:12.3456789          | d\\.hh\\.mm\\.ss\\.FFFFFFF            | 177.78 ns | 0.358 ns | 0.317 ns | 177.76 ns | 177.20 ns | 178.39 ns |  0.96 |    0.00 | 0.0305 |      64 B |        1.00 |
| DateTime_Parse  | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 2024-05-02.21.40.42.123   | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.FFFFF | 225.67 ns | 0.378 ns | 0.335 ns | 225.61 ns | 225.21 ns | 226.24 ns |  1.00 |    0.00 |      - |         - |          NA |
| DateTime_Parse  | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 2024-05-02.21.40.42.123   | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.FFFFF | 202.20 ns | 0.250 ns | 0.209 ns | 202.22 ns | 201.87 ns | 202.51 ns |  0.90 |    0.00 |      - |         - |          NA |
| DateTime_Parse  | Job-GLLMPZ | \dnmain\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe | 2024-05-02.21.40.42.12345 | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.fffff | 219.44 ns | 0.367 ns | 0.325 ns | 219.45 ns | 218.83 ns | 220.02 ns |  1.00 |    0.00 |      - |         - |          NA |
| DateTime_Parse  | Job-AQRKOX | \dnrt\artifacts\bin\testhost\net9.0-windows-Release-x64\shared\Microsoft.NETCore.App\9.0.0\CoreRun.exe   | 2024-05-02.21.40.42.12345 | yyyy\\-MM\\-dd\\.HH\\.mm\\.ss\\.fffff | 218.17 ns | 0.361 ns | 0.320 ns | 218.18 ns | 217.31 ns | 218.65 ns |  0.99 |    0.00 |      - |         - |          NA |