Skip to content

Commit ed58c5e

Browse files
committed
Reset the buffer only when needed
1 parent 7bd3d46 commit ed58c5e

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/DotNext.IO/IO/PoolingBufferedStream.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,21 +489,24 @@ private int ReadCore(Span<byte> data)
489489

490490
if (data.IsEmpty)
491491
{
492-
// nothing to do
492+
if (readPosition == readLength)
493+
Reset();
493494
}
494495
else if (data.Length > maxBufferSize)
495496
{
497+
Debug.Assert(readPosition == readLength);
498+
496499
bytesRead += stream.Read(data);
500+
Reset();
497501
}
498502
else
499503
{
504+
Debug.Assert(readPosition == readLength);
505+
500506
readPosition = 0;
501507
readLength = stream.Read(EnsureBufferAllocated().Span);
502508
bytesRead += ReadFromBuffer(data);
503509
}
504-
505-
if (readPosition == readLength)
506-
Reset();
507510

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

565568
if (data.IsEmpty)
566569
{
567-
// nothing to do
570+
if (readPosition == readLength)
571+
Reset();
568572
}
569-
else if (data.Length > MaxBufferSize)
573+
else if (data.Length >= maxBufferSize)
570574
{
571-
Debug.Assert(readPosition == readLength);
572575
bytesRead += await stream.ReadAsync(data, token).ConfigureAwait(false);
576+
Reset();
573577
}
574578
else
575579
{
@@ -578,9 +582,6 @@ private async ValueTask<int> ReadCoreAsync(Memory<byte> data, CancellationToken
578582
readLength = await stream.ReadAsync(EnsureBufferAllocated().Memory, token).ConfigureAwait(false);
579583
bytesRead += ReadFromBuffer(data.Span);
580584
}
581-
582-
if (readPosition == readLength)
583-
Reset();
584585

585586
return bytesRead;
586587
}

0 commit comments

Comments
 (0)