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

CoreFx #22409 StreamWriter Span based API and tests #23915

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.IO.Tests
{
public partial class WriteTests
{
[Fact]
public void WriteReadOnlySpanTest()
{
char[] chArr = TestDataProvider.CharData;
ReadOnlySpan<char> readSpan = new ReadOnlySpan<char>(chArr);

Stream ms = CreateStream();
StreamWriter sw = new StreamWriter(ms);
StreamReader sr;

sw.Write(readSpan);
sw.Flush();
ms.Position = 0;
sr = new StreamReader(ms);

for (int i = 0; i < chArr.Length; i++)
{
Assert.Equal((int)chArr[i], sr.Read());
}
ms.Dispose();
}

[Fact]
public void WriteLineReadOnlySpanTest()
{
char[] chArr = TestDataProvider.CharData;
ReadOnlySpan<char> readSpan = new ReadOnlySpan<char>(chArr);

Stream ms = CreateStream();
StreamWriter sw = new StreamWriter(ms);
StreamReader sr;

sw.Write(readSpan);
sw.Flush();
ms.Position = 0;
sr = new StreamReader(ms);

string readData = sr.ReadLine();
Assert.Equal(new string(chArr), readData);

ms.Dispose();
}
}
}
1 change: 1 addition & 0 deletions src/System.IO/tests/System.IO.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="IndentedTextWriter.cs" />
<Compile Include="BinaryReader\BinaryReaderTests.cs" />
<Compile Include="MemoryStream\MemoryStream.GetBufferTests.cs" />
<Compile Include="StreamWriter\StreamWriter.WriteTests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" />
<Compile Include="Stream\Stream.cs" />
<Compile Include="StreamReader\StreamReader.cs" />
<Compile Include="StreamReader\StreamReader.StringCtorTests.cs" />
Expand Down
142 changes: 54 additions & 88 deletions src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace System.IO
{
Expand Down Expand Up @@ -330,52 +331,13 @@ public override void Write(char[] buffer)
return;
}

CheckAsyncTaskInProgress();

// Threshold of 4 was chosen after running perf tests
if (buffer.Length <= 4)
if (GetType() == typeof(StreamWriter))
{
for (int i = 0; i < buffer.Length; i++)
{
if (_charPos == _charLen)
{
Flush(false, false);
}

Debug.Assert(_charLen - _charPos > 0, "StreamWriter::Write(char[]) isn't making progress! This is most likely a race in user code.");
_charBuffer[_charPos] = buffer[i];
_charPos++;
}
WriteCore(new ReadOnlySpan<char>(buffer));
}
else
{
int count = buffer.Length;

int index = 0;
while (count > 0)
{
if (_charPos == _charLen)
{
Flush(false, false);
}

int n = _charLen - _charPos;
if (n > count)
{
n = count;
}

Debug.Assert(n > 0, "StreamWriter::Write(char[]) isn't making progress! This is most likely a race in user code.");
Buffer.BlockCopy(buffer, index * sizeof(char), _charBuffer, _charPos * sizeof(char), n * sizeof(char));
_charPos += n;
index += n;
count -= n;
}
}

if (_autoFlush)
{
Flush(true, false);
base.Write(buffer);
}
}

Expand All @@ -397,9 +359,35 @@ public override void Write(char[] buffer, int index, int count)
{
throw new ArgumentException(SR.Argument_InvalidOffLen);
}
if (GetType() == typeof(StreamWriter))
{
WriteCore(new ReadOnlySpan<char>(buffer, index, count));
}
else
{
base.Write(buffer, index, count);
}
}

public override void Write(ReadOnlySpan<char> source)
{
if (GetType() == typeof(StreamWriter))
{
WriteCore(source);
}
else
{
base.Write(source);
}
}

private void WriteCore(ReadOnlySpan<char> source)
{
CheckAsyncTaskInProgress();

int count = source.Length;
int index = 0;

// Threshold of 4 was chosen after running perf tests
if (count <= 4)
{
Expand All @@ -411,7 +399,7 @@ public override void Write(char[] buffer, int index, int count)
}

Debug.Assert(_charLen - _charPos > 0, "StreamWriter::Write(char[]) isn't making progress! This is most likely a race in user code.");
_charBuffer[_charPos] = buffer[index];
_charBuffer[_charPos] = source[index];
_charPos++;
index++;
count--;
Expand All @@ -433,7 +421,7 @@ public override void Write(char[] buffer, int index, int count)
}

Debug.Assert(n > 0, "StreamWriter::Write(char[], int, int) isn't making progress! This is most likely a race condition in user code.");
Buffer.BlockCopy(buffer, index * sizeof(char), _charBuffer, _charPos * sizeof(char), n * sizeof(char));
source.Slice(index, n).CopyTo(new Span<char>(_charBuffer, _charPos, n));
_charPos += n;
index += n;
count -= n;
Expand All @@ -453,52 +441,13 @@ public override void Write(string value)
return;
}

CheckAsyncTaskInProgress();

int count = value.Length;

// Threshold of 4 was chosen after running perf tests
if (count <= 4)
if (GetType() == typeof(StreamWriter))
{
for (int i = 0; i < count; i++)
{
if (_charPos == _charLen)
{
Flush(false, false);
}

Debug.Assert(_charLen - _charPos > 0, "StreamWriter::Write(String) isn't making progress! This is most likely a race condition in user code.");
_charBuffer[_charPos] = value[i];
_charPos++;
}
WriteCore(value.AsReadOnlySpan());
}
else
{
int index = 0;
while (count > 0)
{
if (_charPos == _charLen)
{
Flush(false, false);
}

int n = _charLen - _charPos;
if (n > count)
{
n = count;
}

Debug.Assert(n > 0, "StreamWriter::Write(String) isn't making progress! This is most likely a race condition in user code.");
value.CopyTo(index, _charBuffer, _charPos, n);
_charPos += n;
index += n;
count -= n;
}
}

if (_autoFlush)
{
Flush(true, false);
base.Write(value);
}
}

Expand All @@ -513,6 +462,23 @@ public override void WriteLine(string value)
value = String.Empty;
}

if (GetType() == typeof(StreamWriter))
{
WriteLineCore(value.AsReadOnlySpan());
}
else
{
base.WriteLine(value);
}
}

public override void WriteLine(ReadOnlySpan<char> source)
{
WriteLineCore(source);
}

private void WriteLineCore(ReadOnlySpan<char> value)
{
CheckAsyncTaskInProgress();

int count = value.Length;
Expand All @@ -531,7 +497,7 @@ public override void WriteLine(string value)
}

Debug.Assert(n > 0, "StreamWriter::WriteLine(String) isn't making progress! This is most likely a race condition in user code.");
value.CopyTo(index, _charBuffer, _charPos, n);
value.Slice(index, n).CopyTo(new Span<char>(_charBuffer, _charPos, n));
_charPos += n;
index += n;
count -= n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ public void WritePartialCharArray(int writeLength)
}
}

[Benchmark]
[MemberData(nameof(WriteLengthMemberData))]
public void WriteReadOnlySpan(int writeLength)
{
char[] buffer = new string('a', writeLength + 10).ToCharArray();
ReadOnlySpan<char> span = new ReadOnlySpan<char>(buffer);

int innerIterations = MemoryStreamSize / writeLength;
int outerIteration = TotalWriteCount / innerIterations;
using (var stream = new MemoryStream(MemoryStreamSize))
{
using (var writer = new StreamWriter(stream, new UTF8Encoding(false, true), DefaultStreamWriterBufferSize, true))
{
foreach (var iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
for (int i = 0; i < outerIteration; i++)
{
for (int j = 0; j < innerIterations; j++)
{
writer.Write(span);
}
writer.Flush();
stream.Position = 0;
}
}
}
}
}
}

[Benchmark]
[MemberData(nameof(WriteLengthMemberData))]
public void WriteString(int writeLength)
Expand Down