Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/coreclrPublic archive

Commit4450e5c

Browse files
authored
make DateTime.UtcNow 5% faster to minimize the leap second performance impact (#26046)
* make few methods used by DateTime.UtcNow inlinable to minimize the leap second performance regression impact, related to #25728* apply suggested micro-optimizations
1 parentef1b0f7 commit4450e5c

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

‎src/System.Private.CoreLib/shared/System/DateTime.cs‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -641,32 +641,38 @@ public int CompareTo(DateTime value)
641641

642642
// Returns the tick count corresponding to the given year, month, and day.
643643
// Will check the if the parameters are valid.
644+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
644645
privatestaticlongDateToTicks(intyear,intmonth,intday)
645646
{
646-
if(year>=1&&year<=9999&&month>=1&&month<=12)
647+
if(year<1||year>9999||month<1||month>12||day<1)
647648
{
648-
int[]days=IsLeapYear(year)?s_daysToMonth366:s_daysToMonth365;
649-
if(day>=1&&day<=days[month]-days[month-1])
650-
{
651-
inty=year-1;
652-
intn=y*365+y/4-y/100+y/400+days[month-1]+day-1;
653-
returnn*TicksPerDay;
654-
}
649+
ThrowHelper.ThrowArgumentOutOfRange_BadYearMonthDay();
650+
}
651+
652+
int[]days=IsLeapYear(year)?s_daysToMonth366:s_daysToMonth365;
653+
if(day>days[month]-days[month-1])
654+
{
655+
ThrowHelper.ThrowArgumentOutOfRange_BadYearMonthDay();
655656
}
656-
thrownewArgumentOutOfRangeException(null,SR.ArgumentOutOfRange_BadYearMonthDay);
657+
658+
inty=year-1;
659+
intn=y*365+y/4-y/100+y/400+days[month-1]+day-1;
660+
returnn*TicksPerDay;
657661
}
658662

659663
// Return the tick count corresponding to the given hour, minute, second.
660664
// Will check the if the parameters are valid.
665+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
661666
privatestaticlongTimeToTicks(inthour,intminute,intsecond)
662667
{
663668
//TimeSpan.TimeToTicks is a family access function which does no error checking, so
664669
//we need to put some error checking out here.
665-
if(hour>=0&&hour<24&&minute>=0&&minute<60&&second>=0&&second<60)
670+
if((uint)hour>=24||(uint)minute>=60||(uint)second>=60)
666671
{
667-
return(TimeSpan.TimeToTicks(hour,minute,second));
672+
ThrowHelper.ThrowArgumentOutOfRange_BadHourMinuteSecond();
668673
}
669-
thrownewArgumentOutOfRangeException(null,SR.ArgumentOutOfRange_BadHourMinuteSecond);
674+
675+
returnTimeSpan.TimeToTicks(hour,minute,second);
670676
}
671677

672678
// Returns the number of days in the month given by the year and
@@ -1182,13 +1188,14 @@ public int Year
11821188
// Checks whether a given year is a leap year. This method returns true if
11831189
// year is a leap year, or false if not.
11841190
//
1191+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11851192
publicstaticboolIsLeapYear(intyear)
11861193
{
11871194
if(year<1||year>9999)
11881195
{
1189-
thrownewArgumentOutOfRangeException(nameof(year),SR.ArgumentOutOfRange_Year);
1196+
ThrowHelper.ThrowArgumentOutOfRange_Year();
11901197
}
1191-
returnyear%4==0&&(year%100!=0||year%400==0);
1198+
return(year&3)==0&&((year&15)==0||(year%25)!=0);
11921199
}
11931200

11941201
// Constructs a DateTime from a string. The string must specify a

‎src/System.Private.CoreLib/shared/System/ThrowHelper.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ internal static void ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count()
131131
ExceptionResource.ArgumentOutOfRange_Count);
132132
}
133133

134+
[DoesNotReturn]
135+
internalstaticvoidThrowArgumentOutOfRange_Year()
136+
{
137+
throwGetArgumentOutOfRangeException(ExceptionArgument.year,
138+
ExceptionResource.ArgumentOutOfRange_Year);
139+
}
140+
141+
[DoesNotReturn]
142+
internalstaticvoidThrowArgumentOutOfRange_BadYearMonthDay()
143+
{
144+
thrownewArgumentOutOfRangeException(null,SR.ArgumentOutOfRange_BadYearMonthDay);
145+
}
146+
147+
[DoesNotReturn]
148+
internalstaticvoidThrowArgumentOutOfRange_BadHourMinuteSecond()
149+
{
150+
thrownewArgumentOutOfRangeException(null,SR.ArgumentOutOfRange_BadHourMinuteSecond);
151+
}
152+
153+
[DoesNotReturn]
154+
internalstaticvoidThrowArgumentOutOfRange_TimeSpanTooLong()
155+
{
156+
thrownewArgumentOutOfRangeException(null,SR.Overflow_TimeSpanTooLong);
157+
}
158+
134159
[DoesNotReturn]
135160
internalstaticvoidThrowWrongKeyTypeArgumentException<T>(Tkey,TypetargetType)
136161
{
@@ -657,6 +682,8 @@ private static string GetArgumentName(ExceptionArgument argument)
657682
return"elementType";
658683
caseExceptionArgument.arrayIndex:
659684
return"arrayIndex";
685+
caseExceptionArgument.year:
686+
return"year";
660687
default:
661688
Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum.");
662689
return"";
@@ -687,6 +714,8 @@ private static string GetResourceString(ExceptionResource resource)
687714
returnSR.ArgumentOutOfRange_IndexCountBuffer;
688715
caseExceptionResource.ArgumentOutOfRange_Count:
689716
returnSR.ArgumentOutOfRange_Count;
717+
caseExceptionResource.ArgumentOutOfRange_Year:
718+
returnSR.ArgumentOutOfRange_Year;
690719
caseExceptionResource.Arg_ArrayPlusOffTooSmall:
691720
returnSR.Arg_ArrayPlusOffTooSmall;
692721
caseExceptionResource.NotSupported_ReadOnlyCollection:
@@ -901,6 +930,7 @@ internal enum ExceptionArgument
901930
endIndex,
902931
elementType,
903932
arrayIndex,
933+
year,
904934
}
905935

906936
//
@@ -912,6 +942,7 @@ internal enum ExceptionResource
912942
ArgumentOutOfRange_IndexCount,
913943
ArgumentOutOfRange_IndexCountBuffer,
914944
ArgumentOutOfRange_Count,
945+
ArgumentOutOfRange_Year,
915946
Arg_ArrayPlusOffTooSmall,
916947
NotSupported_ReadOnlyCollection,
917948
Arg_RankMultiDimNotSupported,

‎src/System.Private.CoreLib/shared/System/TimeSpan.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
usingSystem.Globalization;
6+
usingSystem.Runtime.CompilerServices;
67

78
namespaceSystem
89
{
@@ -288,13 +289,14 @@ public static TimeSpan FromTicks(long value)
288289
returnnewTimeSpan(value);
289290
}
290291

292+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
291293
internalstaticlongTimeToTicks(inthour,intminute,intsecond)
292294
{
293295
// totalSeconds is bounded by 2^31 * 2^12 + 2^31 * 2^8 + 2^31,
294296
// which is less than 2^44, meaning we won't overflow totalSeconds.
295297
longtotalSeconds=(long)hour*3600+(long)minute*60+(long)second;
296298
if(totalSeconds>MaxSeconds||totalSeconds<MinSeconds)
297-
thrownewArgumentOutOfRangeException(null,SR.Overflow_TimeSpanTooLong);
299+
ThrowHelper.ThrowArgumentOutOfRange_TimeSpanTooLong();
298300
returntotalSeconds*TicksPerSecond;
299301
}
300302

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp