|
5 | 5 | #nullable enable |
6 | 6 |
|
7 | 7 | using System; |
| 8 | +using System.Buffers; |
8 | 9 | using System.Collections.Concurrent; |
9 | 10 | using System.Collections.Generic; |
10 | 11 | using System.Diagnostics.CodeAnalysis; |
@@ -118,14 +119,18 @@ public int ReadInto(IPythonBuffer buffer) { |
118 | 119 | return _readStream.Read(bytes, 0, buffer.NumBytes()); |
119 | 120 | } |
120 | 121 |
|
121 | | - const int chunkSize = 0x400; // 1 KiB |
122 | | - bytes = new byte[chunkSize]; |
123 | 122 | var span = buffer.AsSpan(); |
124 | | - for (int pos = 0; pos < span.Length; pos += chunkSize) { |
125 | | - int toRead = Math.Min(chunkSize, span.Length - pos); |
126 | | - int hasRead = _readStream.Read(bytes, 0, toRead); |
127 | | - bytes.AsSpan(0, hasRead).CopyTo(span.Slice(pos)); |
128 | | - if (hasRead < toRead) return pos + hasRead; |
| 123 | + const int chunkSize = 0x1000; // 4 KiB, default buffer size of FileSteam |
| 124 | + bytes = ArrayPool<byte>.Shared.Rent(chunkSize); |
| 125 | + try { |
| 126 | + for (int pos = 0; pos < span.Length; pos += chunkSize) { |
| 127 | + int toRead = Math.Min(chunkSize, span.Length - pos); |
| 128 | + int hasRead = _readStream.Read(bytes, 0, toRead); |
| 129 | + bytes.AsSpan(0, hasRead).CopyTo(span.Slice(pos)); |
| 130 | + if (hasRead < toRead) return pos + hasRead; |
| 131 | + } |
| 132 | + } finally { |
| 133 | + ArrayPool<byte>.Shared.Return(bytes); |
129 | 134 | } |
130 | 135 | return span.Length; |
131 | 136 | #endif |
|
0 commit comments