-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
To solve #16354 we are going to track the file offset in memory and avoid expensive Seek calls (draft 91423fa):
runtime/src/libraries/System.Private.CoreLib/src/System/IO/LegacyFileStreamStrategy.Windows.cs
Line 868 in bcd07c5
| SeekCore(_fileHandle, destination.Length, SeekOrigin.Current); |
runtime/src/libraries/System.Private.CoreLib/src/System/IO/LegacyFileStreamStrategy.Windows.cs
Line 1073 in bcd07c5
| SeekCore(_fileHandle, source.Length, SeekOrigin.Current); |
This will allow for removing another expensive SetLength call and solve #25905 (draft a7ca4cb):
runtime/src/libraries/System.Private.CoreLib/src/System/IO/LegacyFileStreamStrategy.Windows.cs
Line 1062 in bcd07c5
| SetLengthCore(_filePosition + source.Length); |
To reach optimal syscall usage for async File IO and get 100% async IO on Windows we just need to get rid of calls to GetFileInformationByHandleEx (called by FileStream.Length which looks very innocent in the source code ;) ):
runtime/src/libraries/System.Private.CoreLib/src/System/IO/LegacyFileStreamStrategy.Windows.cs
Line 837 in bcd07c5
| long len = Length; |
From my initial observations, it seems that we should be able to cache the Length. The reasoning is that by default files are opened with FileShare.Read property, which means "allow other processes to just read from the file":
| private const FileShare DefaultShare = FileShare.Read; |
This should allow us to cache the Length (as long as the user does not specify FileShare.Write in an explicit way which is very rare) and invalidate it on our own when:
- user performs a write that extends the file
- user performs a call to
Position = xorSeek(x)that extends or shrinks the file - user performs a call to
SetLength(x)that extends or shrinks the file