Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Nov 9, 2025

Remove a bit of unsafe code from DateTimeRawInfo + some small perf tweaks to mitigate performance impact from bounds checks.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 9, 2025
@EgorBo EgorBo added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 9, 2025
@EgorBo
Copy link
Member Author

EgorBo commented Nov 9, 2025

@MihuBot

@EgorBo
Copy link
Member Author

EgorBo commented Nov 9, 2025

@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;
}

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 1D ReadOnlySpan<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* with InlineArray3<int> in DateTimeRawInfo

@EgorBo
Copy link
Member Author

EgorBo commented Nov 10, 2025

PTAL @stephentoub @tannergooding

I removed an unsafe block and had to perform a few micro-optimizations to mitigate perf impact from bounds checking.

  1. converted DS[][] to an RVA single-dimension field
  2. marked a few structs as Auto-layout so they can shrink in size and various initialization/copies are sligtly cheaper.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

LGTM if there are no regressions

@EgorBo EgorBo enabled auto-merge (squash) November 12, 2025 20:42
@EgorBo
Copy link
Member Author

EgorBo commented Nov 12, 2025

/ba-g certificate issues

@EgorBo EgorBo merged commit 5574021 into dotnet:main Nov 12, 2025
133 of 142 checks passed
@EgorBo EgorBo deleted the remove-unsafe-datetimeparsing branch November 12, 2025 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants