Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Use FileStream's ctor instead of File.Open when all access is synchronous #5680

Merged
merged 1 commit into from
Jan 26, 2016

Conversation

stephentoub
Copy link
Member

Several places in our libraries use use File.Open* methods. These are simple wrappers for FileStream's constructors, and all use the default "useAsync" value of true (this varies from the the full framework, where the default is false). When only using synchronous Stream methods, a value of true is pure overhead, as each Read/Write/Flush method needs to schedule the asynchronous operation and then block waiting for it to complete. In the few places we're using File.Open* in this manner, I've changed them to instead use FileStream's ctor directly, to avoid the unnecessary access overheads.

cc: @ericstj, @ianhays, @mellinoe

@@ -43,7 +43,7 @@ private bool TryLoadManagedAssemblyMetadata()
try
{
// Try to load the file using the managed metadata reader
using (FileStream assemblyStream = File.OpenRead(_fileName))
using (FileStream assemblyStream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, useAsync: false))
Copy link
Member

Choose a reason for hiding this comment

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

Can you use a named parameter for buffer size throughout?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, will add.

…nous

Several places in our libraries use use File.Open* methods.  These are simple wrappers for FileStream's constructors, and all use the default "useAsync" value of true (this varies from the the full framework, where the default is false).  When only using synchronous Stream methods, a value of true is pure overhead, as each Read/Write/Flush method needs to schedule the asynchronous operation and then block waiting for it to complete.  In the few places we're using File.Open* in this manner, I've changed them to instead use FileStream's ctor directly, to avoid the unnecessary access overheads.
@ericstj
Copy link
Member

ericstj commented Jan 25, 2016

For a couple of these I am concerned that we don't actually have an async API. In particular the ZipArchive ones. Those can be some pretty expensive blocking reads.
For FileVersionInfo it's just a small segment of the file that is read so that's not terrible but it could still take a while if the file is on slow media (network, tape, etc).
Should we look at adding Async APIs?

@stephentoub
Copy link
Member Author

I agree. I think it's certainly worthwhile looking at adding additional async compression APIs. Until that happens, though, I think the changes in this PR are pure goodness. Agreed?

@ericstj
Copy link
Member

ericstj commented Jan 25, 2016

Yeah, LGTM. I filed an issue on adding APIs.

@ianhays
Copy link
Contributor

ianhays commented Jan 25, 2016

LGTM

stephentoub added a commit that referenced this pull request Jan 26, 2016
Use FileStream's ctor instead of File.Open when all access is synchronous
@stephentoub stephentoub merged commit d214ddb into dotnet:master Jan 26, 2016
@stephentoub stephentoub deleted the fs_open branch January 26, 2016 03:17
@karelz karelz modified the milestone: 1.0.0-rtm Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Use FileStream's ctor instead of File.Open when all access is synchronous

Commit migrated from dotnet/corefx@d214ddb
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants