Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

make DateTime.UtcNow 5% faster to minimize the leap second performance impact #26046

Merged
merged 2 commits into from
Aug 8, 2019

Conversation

adamsitnik
Copy link
Member

Today I've again profiled #25728 and was looking for some ways of lowering the regression.

I started with reading the source code of FileTimeToSystemTime and RtlpTimeToTimeFields methods. The RtlpTimeToTimeFields takes care of leap second support, it knows all leap seconds and knows how to handle the positive and negative ones. Copying this logic from OS to .NET makes obviously no sense, it's too complex and too expensive to maintain.

By inlining some small methods that are called by DateTime.UtcNow I was able to make it 5% faster. It ain't much, but that's all I could do.

Before:

obraz

After:

obraz

[Benchmark]
public DateTimeOffset GetUtcNow() => DateTimeOffset.UtcNow;

[Benchmark]
public DateTime GetUtcNow() => DateTime.UtcNow;
Type Method Toolchain Mean Ratio
Perf_DateTime GetUtcNow \after\CoreRun.exe 67.25 ns 0.95
Perf_DateTime GetUtcNow \before\CoreRun.exe 70.75 ns 1.00
Perf_DateTimeOffset GetUtcNow \after\CoreRun.exe 80.81 ns 0.97
Perf_DateTimeOffset GetUtcNow \before\CoreRun.exe 83.27 ns 1.00

And yes, all the [MethodImpl(MethodImplOptions.AggressiveInlining)] were really needed. Moving the throws to ThrowHelper was not enough to get those methods inlined.

/cc @danmosemsft

…ap second performance regression impact, related to #25728
@adamsitnik adamsitnik requested a review from tarekgh August 7, 2019 15:49
Copy link
Member

@gfoidl gfoidl left a comment

Choose a reason for hiding this comment

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

Some more micro-optimizations.

@adamsitnik
Copy link
Member Author

Thanks for all the suggested micro-optimizations, I was able to improve the time by 0.5%

@adamsitnik adamsitnik merged commit 4450e5c into dotnet:master Aug 8, 2019
@adamsitnik adamsitnik deleted the dateTimeNowPerfFix branch August 8, 2019 15:50
@danmoseley
Copy link
Member

Given the regression from 2.2 was so much larger, my assumption is that it is not worth porting this into 3.0.

@tarekgh
Copy link
Member

tarekgh commented Aug 8, 2019

May be it is worth to track this for 3.1?

@danmoseley
Copy link
Member

Our bar right now is a little bit lower or the same as the bar we will use for 3.1. So if we don't take it for 3.0 we would not take it for 3.1.

@tarekgh
Copy link
Member

tarekgh commented Aug 8, 2019

@danmosemsft thanks for the clarification.

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…e impact (dotnet/coreclr#26046)

* make few methods used by DateTime.UtcNow inlinable to minimize the leap second performance regression impact, related to dotnet/coreclr#25728

* apply suggested micro-optimizations


Commit migrated from dotnet/coreclr@4450e5c
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants