Skip to content

Commit

Permalink
Fixed: Perf Issue with Unneeded MemoryPool Borrows and Overrun in Rea…
Browse files Browse the repository at this point in the history
…d Buffer

Description is in post after: #2217 (review)
  • Loading branch information
Sewer56 committed Oct 30, 2024
1 parent 39fcdfb commit 4f58913
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/NexusMods.DataModel/ChunkedStreams/ChunkedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public ChunkedStream(T source, int capacity = 16)

/// <inheritdoc />
public override void Flush() { }


/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));

/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
public override int Read(Span<byte> buffer)
{
if (_position >= _source.Size.Value)
{
Expand All @@ -60,7 +62,7 @@ public override int Read(byte[] buffer, int offset, int count)

chunk.Slice((int)chunkOffset, toRead)
.Span
.CopyTo(buffer.AsSpan(offset, toRead));
.CopyTo(buffer.SliceFast(0, toRead));
_position += (ulong)toRead;

Debug.Assert(_position <= _source.Size.Value, "Read more than the size of the stream");
Expand All @@ -70,7 +72,7 @@ public override int Read(byte[] buffer, int offset, int count)

/// <inheritdoc />
public override async ValueTask<int> ReadAsync(Memory<byte> buffer,
CancellationToken cancellationToken = new CancellationToken())
CancellationToken cancellationToken = new())
{
if (_position >= _source.Size.Value)
{
Expand Down

0 comments on commit 4f58913

Please sign in to comment.