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

Commit

Permalink
Use private core methods, perf test data
Browse files Browse the repository at this point in the history
  • Loading branch information
WinCPP committed Oct 5, 2017
1 parent a6b0a37 commit d3ac17e
Showing 1 changed file with 54 additions and 23 deletions.
77 changes: 54 additions & 23 deletions src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,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 @@ -412,7 +438,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 @@ -434,7 +460,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 @@ -447,21 +473,6 @@ public override void Write(char[] buffer, int index, int count)
}
}

public override void Write(ReadOnlySpan<char> source)
{
char[] buffer = ArrayPool<char>.Shared.Rent(source.Length);

try
{
source.CopyTo(new Span<char>(buffer));
Write(buffer, 0, source.Length);
}
finally
{
ArrayPool<char>.Shared.Return(buffer);
}
}

public override void Write(string value)
{
if (value == null)
Expand Down Expand Up @@ -529,6 +540,31 @@ public override void WriteLine(string value)
value = String.Empty;
}

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

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

}

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

int count = value.Length;
Expand All @@ -547,7 +583,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 All @@ -571,11 +607,6 @@ public override void WriteLine(string value)
}
}

public override void WriteLine(ReadOnlySpan<char> source)
{
WriteLine(new string(source.ToArray()));
}

#region Task based Async APIs
public override Task WriteAsync(char value)
{
Expand Down

0 comments on commit d3ac17e

Please sign in to comment.