Skip to content

fix SafeFileHandle.CanSeek performance regression#125807

Merged
adamsitnik merged 5 commits intomainfrom
adsitnik-perf-fix
Mar 21, 2026
Merged

fix SafeFileHandle.CanSeek performance regression#125807
adamsitnik merged 5 commits intomainfrom
adsitnik-perf-fix

Conversation

@adamsitnik
Copy link
Member

fixes #125660

@adamsitnik adamsitnik added this to the 11.0.0 milestone Mar 19, 2026
@adamsitnik adamsitnik self-assigned this Mar 19, 2026
Copilot AI review requested due to automatic review settings March 19, 2026 22:14
@adamsitnik adamsitnik added area-System.IO tenet-performance Performance related issue labels Mar 19, 2026
@adamsitnik
Copy link
Member Author

@EgorBot -windows_intel

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class Benchmarks
{
    private string? _filePath;

    [GlobalSetup]
    public void Setup()
    {
        _filePath = Path.GetTempFileName();
        File.WriteAllBytes(_filePath, new byte[1_000]);
    }

    [GlobalCleanup]
    public void Cleanup() => File.Delete(_filePath!);

    [Benchmark]
    public byte[] ReadAllBytes() => File.ReadAllBytes(_filePath!);

    [Benchmark]
    public Task ReadAllBytesAsync() => File.ReadAllBytesAsync(_filePath!);

    [Benchmark]
    public bool OpenAndCanSeek()
    {
        using FileStream handle = File.OpenRead(_filePath!);
        return handle.CanSeek;
    }
}

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

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 targets a Windows performance regression in SafeFileHandle.CanSeek / SafeFileHandle.Type usage by avoiding an extra handle-type probe for common disk-file handles.

Changes:

  • Adds a GetFileTypeCore fast-path for FILE_TYPE_DISK handles when Path is available, returning RegularFile without calling GetDiskBasedType().

- use existing public API for opening directories as SafeFileHandle to ensure we have test coverage for it
- make sure NoBuffering and BackupOrRestore are not defined in multiple places
- fix the bug
- remove volatile
@adamsitnik
Copy link
Member Author

@EgorBot -windows_intel

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class Benchmarks
{
    private string? _filePath;

    [GlobalSetup]
    public void Setup()
    {
        _filePath = Path.GetTempFileName();
        File.WriteAllBytes(_filePath, new byte[1_000]);
    }

    [GlobalCleanup]
    public void Cleanup() => File.Delete(_filePath!);

    [Benchmark]
    public byte[] ReadAllBytes() => File.ReadAllBytes(_filePath!);

    [Benchmark]
    public Task ReadAllBytesAsync() => File.ReadAllBytesAsync(_filePath!);

    [Benchmark]
    public bool OpenAndCanSeek()
    {
        using FileStream handle = File.OpenRead(_filePath!);
        return handle.CanSeek;
    }
}

Copilot AI review requested due to automatic review settings March 20, 2026 12:23
@adamsitnik adamsitnik requested a review from danmoseley March 20, 2026 12:25
@adamsitnik adamsitnik enabled auto-merge (squash) March 20, 2026 12:25
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

@adamsitnik adamsitnik requested a review from stephentoub March 20, 2026 16:11
Copilot AI review requested due to automatic review settings March 20, 2026 22:31
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@adamsitnik adamsitnik merged commit edd2a2a into main Mar 21, 2026
146 checks passed
@adamsitnik adamsitnik deleted the adsitnik-perf-fix branch March 21, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO tenet-performance Performance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf] Windows/x64: 9 Regressions on 3/10/2026 8:09:51 PM +00:00

4 participants