-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Use FileStream's ctor instead of File.Open when all access is synchronous #5680
Conversation
@@ -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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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. |
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? |
Yeah, LGTM. I filed an issue on adding APIs. |
LGTM |
Use FileStream's ctor instead of File.Open when all access is synchronous
Use FileStream's ctor instead of File.Open when all access is synchronous Commit migrated from dotnet/corefx@d214ddb
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