Skip to content

Commit

Permalink
Optimize StreamBox.ReadInto
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Aug 26, 2023
1 parent cf59bec commit 13890d2
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Src/IronPython/Runtime/PythonFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ 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 i = 0; i < span.Length; i++) {
int b = _readStream.ReadByte();
if (b == -1) return i;
span[i] = (byte)b;
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;
}
return span.Length;
#endif
Expand Down

0 comments on commit 13890d2

Please sign in to comment.