Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PERF] DaysInMonth optimization #95229

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ public readonly partial struct DateTime
internal static ReadOnlySpan<uint> DaysToMonth365 => [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
internal static ReadOnlySpan<uint> DaysToMonth366 => [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366];

private static ReadOnlySpan<byte> DaysInMonth365 => [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
private static ReadOnlySpan<byte> DaysInMonth366 => [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
private const int DaysInMonth365 = 0b_11_10_11_10_11_11_10_11_10_11_00_11_00;
private const int DaysInMonth366 = DaysInMonth365 | 0x10;
private const byte ExtraDaysMask = 0x3;

public static readonly DateTime MinValue;
public static readonly DateTime MaxValue = new DateTime(MaxTicks, DateTimeKind.Unspecified);
Expand Down Expand Up @@ -1170,7 +1171,8 @@ public static int DaysInMonth(int year, int month)
{
if (month < 1 || month > 12) ThrowHelper.ThrowArgumentOutOfRange_Month(month);
// IsLeapYear checks the year argument
return (IsLeapYear(year) ? DaysInMonth366 : DaysInMonth365)[month - 1];
Copy link
Member

Choose a reason for hiding this comment

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

return (IsLeapYear(year) ? DaysInMonth366 : DaysInMonth365)[month - 1];

I thought the jit should be optimizing the span indexing. Maybe something we can try instead of doing subtract 1 every time, we can have the span contents starts with 0 and avoid the subtraction operation and measure.

Also, will be good to do the measurements on the real code and try to measure DateTime.DaysInMonth with and without the changes instead of writing a separate code mimic the library's code.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

The JIT can optimize and constant fold accesses to RVA static data (like the DaysInMonth365/DaysInMonth366 data currently is).

Altogether, this new code seems a lot less readable for what is barely 1/5th of a nanosecond faster (basically 1-2 instruction cycles).

This is unlikely to make any difference in any real world code and I personally don't think the additional complexity is worth it here.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @tannergooding here, if the perf gain is not going to make real difference in the public scenario, it is not worth the complexity.

int days = IsLeapYear(year) ? DaysInMonth366 : DaysInMonth365;
return 28 + (days >> (2 * month) & ExtraDaysMask);
}

// Converts an OLE Date to a tick count.
Expand Down
Loading