Optimize DateTime decomposition in CoreLib by ts2do · Pull Request #1944 · dotnet/runtime (original) (raw)

Add uses of DateTime.GetDate (renamed from GetDatePart) where appropriate.
Add internal DateTime.GetTime method to eliminate duplicate computations.

Note that the change to TimeZoneInfo.TransitionTimeToDateTime, which adds TimeOfDay directly in ticks precision, can't break anything because the TransitionTime constructor ensures that TimeOfDay doesn't have sub-millisecond time.

Here are the relevant microbenchmarks in dotnet/performance, of which the rows with the bold text being the build with the changes:

System.Tests.Perf_DateTime

Method format Mean Error StdDev Median Min Max Ratio Gen 0 Gen 1 Gen 2 Allocated
ToString ? 292.30 ns 3.399 ns 2.838 ns 292.33 ns 286.58 ns 296.96 ns 0.99 0.0144 - - 64 B
ToString ? 295.86 ns 2.520 ns 2.104 ns 296.08 ns 291.85 ns 300.16 ns 1.00 0.0143 - - 64 B
ToString G 293.25 ns 3.067 ns 2.869 ns 293.66 ns 288.34 ns 297.03 ns 0.99 0.0143 - - 64 B
ToString G 295.89 ns 3.830 ns 3.583 ns 295.99 ns 289.15 ns 303.13 ns 1.00 0.0143 - - 64 B
ToString o 66.41 ns 0.943 ns 0.882 ns 66.37 ns 64.78 ns 67.96 ns 0.72 0.0190 - - 80 B
ToString o 92.34 ns 0.826 ns 0.773 ns 92.28 ns 91.13 ns 93.41 ns 1.00 0.0191 - - 80 B
ToString r 52.63 ns 0.433 ns 0.384 ns 52.64 ns 51.90 ns 53.41 ns 0.92 0.0190 - - 80 B
ToString r 57.13 ns 0.514 ns 0.481 ns 56.93 ns 56.59 ns 58.31 ns 1.00 Base 0.0189 - -
ToString s 289.34 ns 3.105 ns 2.904 ns 289.99 ns 283.88 ns 292.82 ns 1.02 0.0150 - - 64 B
ToString s 284.92 ns 1.898 ns 1.683 ns 285.08 ns 281.30 ns 287.85 ns 1.00 0.0151 - - 64 B

System.Tests.Perf_DateTimeOffset

Method format value Mean Error StdDev Median Min Max Ratio Gen 0 Gen 1 Gen 2 Allocated
ToString ? ? 666.74 ns 6.687 ns 6.255 ns 664.85 ns 658.04 ns 681.22 ns 1.01 0.0296 - - 128 B
ToString ? ? 662.49 ns 5.139 ns 4.555 ns 663.86 ns 653.89 ns 670.28 ns 1.00 0.0294 - - 128 B
ToString ? 12/30/2017 3:45:22 AM -08:00 658.87 ns 5.386 ns 5.038 ns 658.13 ns 651.09 ns 670.00 ns 0.98 0.0286 - - 128 B
ToString ? 12/30/2017 3:45:22 AM -08:00 671.86 ns 5.994 ns 5.607 ns 673.26 ns 663.39 ns 683.24 ns 1.00 0.0294 - - 128 B
ToString G ? 308.34 ns 4.298 ns 3.810 ns 308.64 ns 300.68 ns 315.02 ns 1.01 0.0148 - - 64 B
ToString G ? 304.10 ns 2.380 ns 2.226 ns 304.11 ns 300.51 ns 307.61 ns 1.00 0.0150 - - 64 B
ToString o ? 76.22 ns 0.368 ns 0.308 ns 76.17 ns 75.78 ns 76.92 ns 0.69 0.0209 - - 88 B
ToString o ? 110.96 ns 1.202 ns 1.066 ns 110.72 ns 109.07 ns 113.46 ns 1.00 0.0206 - - 88 B
ToString r ? 63.59 ns 0.742 ns 0.620 ns 63.76 ns 62.62 ns 64.42 ns 0.97 0.0190 - - 80 B
ToString r ? 65.52 ns 0.940 ns 0.880 ns 65.33 ns 63.77 ns 67.45 ns 1.00 0.0190 - - 80 B
ToString s ? 304.85 ns 3.213 ns 3.006 ns 305.61 ns 298.44 ns 309.04 ns 1.00 0.0147 - - 64 B
ToString s ? 305.75 ns 3.682 ns 3.444 ns 304.98 ns 300.63 ns 310.69 ns 1.00 0.0148 - - 64 B

System.Buffers.Text.Tests.Utf8FormatterTests.FormatterDateTimeOffsetNow

Method Mean Error StdDev Median Min Max Ratio Gen 0 Gen 1 Gen 2 Allocated
FormatterDateTimeOffsetNow 44.22 ns 0.473 ns 0.442 ns 44.35 ns 43.59 ns 45.01 ns 0.62 - - - -
FormatterDateTimeOffsetNow 71.83 ns 0.662 ns 0.587 ns 71.79 ns 71.00 ns 73.06 ns 1.00 - - - -

System.Text.Json.Tests.Perf_DateTimes

Method Formatted SkipValidation Mean Error StdDev Median Min Max Ratio Gen0 Gen1 Gen2 Allocated
WriteDateTimes False False 6.700 ms 0.0729 ms 0.0647 ms 6.700 ms 6.584 ms 6.801 ms 0.72 - - - 140 B
WriteDateTimes False False 9.313 ms 0.1085 ms 0.0962 ms 9.310 ms 9.159 ms 9.498 ms 1.00 - - - 120 B
WriteDateTimes False True 6.513 ms 0.0543 ms 0.0453 ms 6.529 ms 6.428 ms 6.578 ms 0.71 - - - 120 B
WriteDateTimes False True 9.149 ms 0.0635 ms 0.0563 ms 9.149 ms 9.025 ms 9.231 ms 1.00 - - - 120 B
WriteDateTimes True False 7.284 ms 0.0855 ms 0.0758 ms 7.270 ms 7.145 ms 7.394 ms 0.73 - - - 120 B
WriteDateTimes True False 9.926 ms 0.1158 ms 0.1083 ms 9.918 ms 9.735 ms 10.091 ms 1.00 - - - 120 B
WriteDateTimes True True 7.343 ms 0.0763 ms 0.0714 ms 7.328 ms 7.250 ms 7.479 ms 0.73 - - - 141 B
WriteDateTimes True True 10.120 ms 0.1492 ms 0.1323 ms 10.077 ms 9.938 ms 10.373 ms 1.00 - - - 120 B