Skip to content

Commit 69f4d75

Browse files
pbolducebozduman
andauthored
Fixes #1312 Optimize ReadFullAsync to minimize memory allocations (#1314)
Co-authored-by: ebozduman <[email protected]>
1 parent fb0af28 commit 69f4d75

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

Minio/ApiEndpoints/ObjectOperations.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,21 +1148,15 @@ internal static async Task<ReadOnlyMemory<byte>> ReadFullAsync(Stream data, int
11481148
var totalRead = 0;
11491149
while (totalRead < currentPartSize)
11501150
{
1151-
Memory<byte> curData = new byte[currentPartSize - totalRead];
1152-
var curRead = await data.ReadAsync(curData[..(currentPartSize - totalRead)]).ConfigureAwait(false);
1151+
var curData = result[totalRead..currentPartSize];
1152+
var curRead = await data.ReadAsync(curData).ConfigureAwait(false);
11531153
if (curRead == 0) break;
1154-
for (var i = 0; i < curRead; i++)
1155-
curData.Slice(i, 1).CopyTo(result[(totalRead + i)..]);
11561154
totalRead += curRead;
11571155
}
11581156

11591157
if (totalRead == 0) return null;
11601158

1161-
if (totalRead == currentPartSize) return result;
1162-
1163-
Memory<byte> truncatedResult = new byte[totalRead];
1164-
for (var i = 0; i < totalRead; i++)
1165-
result.Slice(i, 1).CopyTo(truncatedResult[i..]);
1166-
return truncatedResult;
1159+
// Return only the valid portion without allocating a new buffer
1160+
return result[..totalRead];
11671161
}
11681162
}

0 commit comments

Comments
 (0)