fix SafeFileHandle.CanSeek performance regression#125807
Open
adamsitnik wants to merge 5 commits intomainfrom
Open
fix SafeFileHandle.CanSeek performance regression#125807adamsitnik wants to merge 5 commits intomainfrom
adamsitnik wants to merge 5 commits intomainfrom
Conversation
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;
}
} |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Contributor
There was a problem hiding this comment.
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
GetFileTypeCorefast-path forFILE_TYPE_DISKhandles whenPathis available, returningRegularFilewithout callingGetDiskBasedType().
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
This was referenced Mar 20, 2026
Open
- 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
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;
}
} |
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
danmoseley
reviewed
Mar 20, 2026
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.cs
Show resolved
Hide resolved
danmoseley
reviewed
Mar 20, 2026
...raries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.Windows.cs
Show resolved
Hide resolved
danmoseley
reviewed
Mar 20, 2026
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Outdated
Show resolved
Hide resolved
danmoseley
approved these changes
Mar 20, 2026
stephentoub
approved these changes
Mar 20, 2026
Co-authored-by: Dan Moseley <danmose@microsoft.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fixes #125660