Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure MetadataReader fast path works with every FileStream on every OS #50367

Merged
merged 4 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefaultLanguage>en-US</DefaultLanguage>
Expand All @@ -15,9 +15,11 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
Link="Common\Interop\Windows\Interop.Libraries.cs"
Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)' "/>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs" />
Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)' "/>
<Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
Expand Down Expand Up @@ -102,8 +104,9 @@
<Compile Include="System\Reflection\Internal\Utilities\EmptyArray.cs" />
<Compile Include="System\Reflection\Internal\Utilities\EncodingHelper.cs" Condition="$(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('net4'))" />
<Compile Include="System\Reflection\Internal\Utilities\EncodingHelper.netcoreapp.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netcoreapp.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netstandard2.0.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net461'" />
<Compile Include="System\Reflection\Internal\Utilities\Hash.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableByteArrayInterop.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableMemoryStream.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ internal static unsafe NativeHeapMemoryBlock ReadMemoryBlockNoLock(Stream stream
{
stream.Seek(start, SeekOrigin.Begin);

if (!isFileStream || !FileStreamReadLightUp.TryReadFile(stream, block.Pointer, start, size))
int bytesRead = 0;

if (!isFileStream || !FileStreamReadLightUp.TryReadFile(stream, block.Pointer, size, out bytesRead))
{
stream.CopyTo(block.Pointer, size);
stream.CopyTo(block.Pointer + bytesRead, size - bytesRead);
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
}

fault = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;

namespace System.Reflection.Internal
{
internal static class FileStreamReadLightUp
{
internal static bool IsFileStream(Stream stream) => stream is FileStream;

internal static unsafe bool TryReadFile(Stream stream, byte* buffer, int size, out int bytesRead)
{
bytesRead = stream.Read(new Span<byte>(buffer, size));

return bytesRead == size;
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,22 @@ internal static SafeHandle GetSafeFileHandle(Stream stream)
return handle;
}

internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
internal static unsafe bool TryReadFile(Stream stream, byte* buffer, int size, out int bytesRead)
{
if (readFileNotAvailable)
{
bytesRead = 0;
return false;
}

SafeHandle handle = GetSafeFileHandle(stream);
if (handle == null)
{
bytesRead = 0;
return false;
}

int result;
int bytesRead;

try
{
Expand All @@ -109,6 +110,7 @@ internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start,
catch
{
readFileNotAvailable = true;
bytesRead = 0;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace System.Reflection.Internal
{
internal static class FileStreamReadLightUp
{
private static bool IsReadFileAvailable =>
#if NETCOREAPP
OperatingSystem.IsWindows();
#else
Path.DirectorySeparatorChar == '\\';
#endif
private static bool IsReadFileAvailable => Path.DirectorySeparatorChar == '\\';

internal static bool IsFileStream(Stream stream) => stream is FileStream;

Expand Down Expand Up @@ -42,20 +36,22 @@ internal static class FileStreamReadLightUp
return handle;
}

internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
internal static unsafe bool TryReadFile(Stream stream, byte* buffer, int size, out int bytesRead)
{
if (!IsReadFileAvailable)
{
bytesRead = 0;
return false;
}

SafeHandle? handle = GetSafeFileHandle(stream);
if (handle == null)
{
bytesRead = 0;
return false;
}

int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);
int result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);

if (result == 0 || bytesRead != size)
{
Expand Down