Skip to content

Commit

Permalink
Use ArrayPool for buffered reading in StreamBox (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Aug 27, 2023
1 parent 63a8819 commit dbcdb07
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions Src/IronPython/Runtime/PythonFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#nullable enable

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -118,14 +119,18 @@ internal sealed class StreamBox {
return _readStream.Read(bytes, 0, buffer.NumBytes());
}

const int chunkSize = 0x400; // 1 KiB
bytes = new byte[chunkSize];
var span = buffer.AsSpan();
for (int pos = 0; pos < span.Length; pos += chunkSize) {
int toRead = Math.Min(chunkSize, span.Length - pos);
int hasRead = _readStream.Read(bytes, 0, toRead);
bytes.AsSpan(0, hasRead).CopyTo(span.Slice(pos));
if (hasRead < toRead) return pos + hasRead;
const int chunkSize = 0x1000; // 4 KiB, default buffer size of FileSteam
bytes = ArrayPool<byte>.Shared.Rent(chunkSize);
try {
for (int pos = 0; pos < span.Length; pos += chunkSize) {
int toRead = Math.Min(chunkSize, span.Length - pos);
int hasRead = _readStream.Read(bytes, 0, toRead);
bytes.AsSpan(0, hasRead).CopyTo(span.Slice(pos));
if (hasRead < toRead) return pos + hasRead;
}
} finally {
ArrayPool<byte>.Shared.Return(bytes);
}
return span.Length;
#endif
Expand Down

0 comments on commit dbcdb07

Please sign in to comment.