Skip to content

fix SafeFileHandle.CanSeek performance regression#125807

Open
adamsitnik wants to merge 1 commit intomainfrom
adsitnik-perf-fix
Open

fix SafeFileHandle.CanSeek performance regression#125807
adamsitnik wants to merge 1 commit 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().

Comment on lines +341 to +343
// which does not allow opening directories or symbolic links (it resolves them).
// That is why we can skip the extra check performed by GetDiskBasedType.
Interop.Kernel32.FileTypes.FILE_TYPE_DISK when Path is not null => FileHandleType.RegularFile,
Copy link

Copilot AI Mar 19, 2026

Choose a reason for hiding this comment

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

The new fast-path assumes Path is not null implies the handle can't be a directory or a symlink, but SafeFileHandle.Open passes through FileOptions and FileStreamHelpers.ValidateArguments explicitly allows (FileOptions)0x02000000 (FILE_FLAG_BACKUP_SEMANTICS). With that flag, CreateFile can successfully open directory handles while still setting _path, so this switch arm would misclassify such handles as RegularFile (making Type/CanSeek incorrect). Consider narrowing the fast-path to cases where FILE_FLAG_BACKUP_SEMANTICS is not set (or otherwise proving directories can’t be opened) and adjust the comment accordingly.

Suggested change
// which does not allow opening directories or symbolic links (it resolves them).
// That is why we can skip the extra check performed by GetDiskBasedType.
Interop.Kernel32.FileTypes.FILE_TYPE_DISK when Path is not null => FileHandleType.RegularFile,
// which does not allow opening directories or symbolic links (it resolves them) unless
// FILE_FLAG_BACKUP_SEMANTICS (0x02000000) is specified in FileOptions to allow opening directories.
// When backup semantics are not used, we can skip the extra check performed by GetDiskBasedType.
Interop.Kernel32.FileTypes.FILE_TYPE_DISK when Path is not null && (GetFileOptions() & (FileOptions)0x02000000) == 0 => FileHandleType.RegularFile,

Copilot uses AI. Check for mistakes.
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

2 participants