Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Temporarily removing use of ReadOnlySpan indexer in Runtime.Extensions #25326

Merged
merged 6 commits into from
Nov 22, 2017
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
Expand Up @@ -266,6 +266,9 @@
<ItemGroup Condition="'$(TargetGroup)' == 'uapaot'">
<ReferenceFromRuntime Include="System.Private.Interop" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' != 'uapaot'">
<ProjectReference Include="..\..\System.Runtime.CompilerServices.Unsafe\src\System.Runtime.CompilerServices.Unsafe.ilproj" />
</ItemGroup>
<ItemGroup>
<ReferenceFromRuntime Include="System.Private.CoreLib" />
</ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion src/System.Runtime.Extensions/src/System/IO/StreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -566,7 +567,13 @@ private bool IsPreamble()
return _checkPreamble;
}

ReadOnlySpan<byte> preamble = _encoding.Preamble;
ReadOnlySpan<byte> readonlyPreamble = _encoding.Preamble;
Span<byte> preamble;

unsafe
{
preamble = new Span<byte>(Unsafe.AsPointer<byte>(ref readonlyPreamble.DangerousGetPinnableReference()), readonlyPreamble.Length);
Copy link
Member

Choose a reason for hiding this comment

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

This has GC hole...

Copy link
Member

Choose a reason for hiding this comment

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

You need to pin the pointer while you are manipulating it.

}

Debug.Assert(_bytePos <= preamble.Length, "_compressPreamble was called with the current bytePos greater than the preamble buffer length. Are two threads using this StreamReader at the same time?");
int len = (_byteLen >= (preamble.Length)) ? (preamble.Length - _bytePos) : (_byteLen - _bytePos);
Expand Down
18 changes: 12 additions & 6 deletions src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -370,14 +371,19 @@ private unsafe void WriteCore(ReadOnlySpan<char> buffer, bool autoFlush)
{
CheckAsyncTaskInProgress();

if (buffer.Length <= 4 && // Threshold of 4 chosen based on perf experimentation
buffer.Length <= _charLen - _charPos)
Span<char> bufferSpan;
unsafe
{
bufferSpan = new Span<char>(Unsafe.AsPointer<char>(ref buffer.DangerousGetPinnableReference()), buffer.Length);
}
if (bufferSpan.Length <= 4 && // Threshold of 4 chosen based on perf experimentation
bufferSpan.Length <= _charLen - _charPos)
{
// For very short buffers and when we don't need to worry about running out of space
// in the char buffer, just copy the chars individually.
for (int i = 0; i < buffer.Length; i++)
for (int i = 0; i < bufferSpan.Length; i++)
{
_charBuffer[_charPos++] = buffer[i];
_charBuffer[_charPos++] = bufferSpan[i];
}
}
else
Expand All @@ -394,11 +400,11 @@ private unsafe void WriteCore(ReadOnlySpan<char> buffer, bool autoFlush)
throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed);
}

fixed (char* bufferPtr = &buffer.DangerousGetPinnableReference())
fixed (char* bufferPtr = &bufferSpan.DangerousGetPinnableReference())
fixed (char* dstPtr = &charBuffer[0])
{
char* srcPtr = bufferPtr;
int count = buffer.Length;
int count = bufferSpan.Length;
int dstPos = _charPos; // use a local copy of _charPos for safety
while (count > 0)
{
Expand Down