Skip to content

Avoid expensive GetFileInformationByHandleEx syscall if possible #49541

@adamsitnik

Description

@adamsitnik

To solve #16354 we are going to track the file offset in memory and avoid expensive Seek calls (draft 91423fa):

SeekCore(_fileHandle, destination.Length, SeekOrigin.Current);

SeekCore(_fileHandle, source.Length, SeekOrigin.Current);

This will allow for removing another expensive SetLength call and solve #25905 (draft a7ca4cb):

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 ;) ):

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 = x or Seek(x) that extends or shrinks the file
  • user performs a call to SetLength(x) that extends or shrinks the file

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions