Remove unsafe from DateTimeRawInfo#121479
Merged
EgorBo merged 7 commits intodotnet:mainfrom Nov 12, 2025
Merged
Conversation
Member
Author
Member
Author
|
@EgorBot -amd -intel -arm using System;
using System.Collections.Generic;
using System.Globalization;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(DateTimeParsingBenchmarks).Assembly).Run(args);
public class DateTimeParsingBenchmarks
{
public static IEnumerable<object[]> TestData()
{
yield return ["2025-11-10", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None];
yield return ["10/11/2025 14:37:05", "dd/MM/yyyy HH:mm:ss", new CultureInfo("en-GB"), DateTimeStyles.None];
yield return ["11/10/2025 2:37:05 PM", "M/d/yyyy h:mm:ss tt", new CultureInfo("en-US"), DateTimeStyles.AssumeLocal];
yield return ["Mon, 10 Nov 2025 14:37:05 GMT", "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal];
yield return ["2025-11-10T14:37:05+01:00", "yyyy'-'MM'-'dd'T'HH':'mm':'ssK", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind];
}
[Benchmark]
[ArgumentsSource(nameof(TestData))]
public DateTime TryParseExact_WithCases(string text, string format, CultureInfo culture, DateTimeStyles styles)
=> DateTime.TryParseExact(text, format, culture, styles, out var dt) ? dt : default;
} |
am11
reviewed
Nov 10, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs
Show resolved
Hide resolved
This was referenced Nov 10, 2025
Open
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the DateTimeParse class by converting data structures to more efficient representations and removing unsafe code. The changes improve memory layout and performance while maintaining the same functionality.
Key changes:
- Converts the state machine lookup table from a jagged 2D array (
DS[][]) to a flat 1DReadOnlySpan<DS>with a helper method for indexed access - Adds explicit base types to internal enums (
TM,DS,DTSubStringType) for better memory efficiency - Removes unsafe code by replacing
int*withInlineArray3<int>inDateTimeRawInfo
Member
Author
|
PTAL @stephentoub @tannergooding I removed an unsafe block and had to perform a few micro-optimizations to mitigate perf impact from bounds checking.
|
stephentoub
reviewed
Nov 11, 2025
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs
Outdated
Show resolved
Hide resolved
stephentoub
approved these changes
Nov 11, 2025
Member
stephentoub
left a comment
There was a problem hiding this comment.
LGTM if there are no regressions
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs
Outdated
Show resolved
Hide resolved
AaronRobinsonMSFT
approved these changes
Nov 12, 2025
Member
Author
|
/ba-g certificate issues |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Remove a bit of unsafe code from DateTimeRawInfo + some small perf tweaks to mitigate performance impact from bounds checks.