Skip to content

Fixes #1312 Optimize ReadFullAsync to minimize memory allocations #1314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions Minio/ApiEndpoints/ObjectOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,21 +1130,15 @@ internal async Task<ReadOnlyMemory<byte>> ReadFullAsync(Stream data, int current
var totalRead = 0;
while (totalRead < currentPartSize)
{
Memory<byte> curData = new byte[currentPartSize - totalRead];
var curRead = await data.ReadAsync(curData[..(currentPartSize - totalRead)]).ConfigureAwait(false);
var curData = result[totalRead..currentPartSize];
var curRead = await data.ReadAsync(curData).ConfigureAwait(false);
if (curRead == 0) break;
for (var i = 0; i < curRead; i++)
curData.Slice(i, 1).CopyTo(result[(totalRead + i)..]);
totalRead += curRead;
}

if (totalRead == 0) return null;

if (totalRead == currentPartSize) return result;

Memory<byte> truncatedResult = new byte[totalRead];
for (var i = 0; i < totalRead; i++)
result.Slice(i, 1).CopyTo(truncatedResult[i..]);
return truncatedResult;
// Return only the valid portion without allocating a new buffer
return result[..totalRead];
}
}