Reduce the cost of creating a MemoryMappedFile#63790
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsExplanation:
Windows
Unix
Related to #62768
|
...libraries/System.IO.MemoryMappedFiles/src/Microsoft/Win32/SafeMemoryMappedFileHandle.Unix.cs
Show resolved
Hide resolved
| SetHandle(handlePtr); | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) |
There was a problem hiding this comment.
Is this going be an observable behavior change for user defined FileStreams? The potential user-defined Dispose is not going to be called anymore.
There was a problem hiding this comment.
Yes, although that looks like it's already a discrepancy between our Windows and Unix implementation, with the Windows implementation not Dispose'ing of the file stream and the Unix implementation doing so (unless I'm missing it in the code). While it would then be a behavior change on Unix, as the Unix implementation is trying to match the pre-existing Windows semantics it seems like a reasonable bug fix.
...libraries/System.IO.MemoryMappedFiles/src/Microsoft/Win32/SafeMemoryMappedFileHandle.Unix.cs
Show resolved
Hide resolved
| long fileSize = mode switch | ||
| { | ||
| FileMode.CreateNew or FileMode.Create => 0, // the file is brand new | ||
| _ => RandomAccess.GetLength(fileHandle) |
There was a problem hiding this comment.
If GetLength throws, the file handle won't be Dispose'd.
There was a problem hiding this comment.
Also, doesn't this throw for non-regular files? Is the exception that occurs in such a case the same as it was before?
There was a problem hiding this comment.
If GetLength throws, the file handle won't be Dispose'd.
that is correct, I am going to add a try-catch block
Also, doesn't this throw for non-regular files?
both FileStream.Length and RandomAccess.GetLength throw for non-seekable files.
the special character device files report that they are seekable and of a size equal zero
There was a problem hiding this comment.
Do we have tests that validate the exception in this case, both with capacity == 0 and capacity != 0?
There was a problem hiding this comment.
Do we have tests that validate the exception in this case, both with capacity == 0 and capacity != 0?
We don't. For both cases, the current implementation would throw for non-seekable file (without closing the handle).
For capacity == 0 here when accessing fileStream.Length:
For capacity != 0 here when accessing fileStream.Length:
There was a problem hiding this comment.
@stephentoub I've sent a PR with tests #63927
Once it's gets merged, I am going to sync this PR with main branch.
There was a problem hiding this comment.
@stephentoub I've merged the tests into this PR and they are all passing. PTAL
src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.cs
Show resolved
Hide resolved
src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.cs
Outdated
Show resolved
Hide resolved
...braries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Unix.cs
Outdated
Show resolved
Hide resolved
37e5f05 to
59e4f24
Compare
Explanation:
FileStream, we can use more lightweightSafeFileHandle. It reduces the managed allocations.Windows
Unix
Related to #62768