Skip to content

Commit

Permalink
Reset the buffer only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jan 21, 2025
1 parent 7bd3d46 commit ed58c5e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/DotNext.IO/IO/PoolingBufferedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,21 +489,24 @@ private int ReadCore(Span<byte> data)

if (data.IsEmpty)
{
// nothing to do
if (readPosition == readLength)
Reset();
}
else if (data.Length > maxBufferSize)
{
Debug.Assert(readPosition == readLength);

bytesRead += stream.Read(data);
Reset();
}
else
{
Debug.Assert(readPosition == readLength);

readPosition = 0;
readLength = stream.Read(EnsureBufferAllocated().Span);
bytesRead += ReadFromBuffer(data);
}

if (readPosition == readLength)
Reset();

return bytesRead;
}
Expand Down Expand Up @@ -564,12 +567,13 @@ private async ValueTask<int> ReadCoreAsync(Memory<byte> data, CancellationToken

if (data.IsEmpty)
{
// nothing to do
if (readPosition == readLength)
Reset();
}
else if (data.Length > MaxBufferSize)
else if (data.Length >= maxBufferSize)
{
Debug.Assert(readPosition == readLength);
bytesRead += await stream.ReadAsync(data, token).ConfigureAwait(false);
Reset();
}
else
{
Expand All @@ -578,9 +582,6 @@ private async ValueTask<int> ReadCoreAsync(Memory<byte> data, CancellationToken
readLength = await stream.ReadAsync(EnsureBufferAllocated().Memory, token).ConfigureAwait(false);
bytesRead += ReadFromBuffer(data.Span);
}

if (readPosition == readLength)
Reset();

return bytesRead;
}
Expand Down

0 comments on commit ed58c5e

Please sign in to comment.