You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reduce number of read calls when not asking for progress reporting. (#623)
* Reduce number of read calls when not asking for progress reporting.
Also, when asking for progress reporting, cap the number of updates at approximately 100 instead of fileSize/4k.
* Add readUntilEOF option to file reading primitive and set it to true by default
* Address review comments - change readUntilEOF to readUntilLength
// Takes an `Int` size and returns an `Int` to match `Data`'s count. If we are going to read more than Int.max, throws - because we won't be able to store it in `Data`.
/// Takes an `Int` size and returns an `Int` to match `Data`'s count. If we are going to read more than Int.max, throws - because we won't be able to store it in `Data`.
403
+
/// If `readUntilLength` is `false`, then we will end the read if we receive less than `length` bytes. This can be used to read from something like a socket, where the `length` simply represents the maximum size you can read at once.
// To report progress, we have to try reading in smaller chunks than the whole file. If we have a readingAttributes struct, we already know it. Otherwise, get one that makes sense for this destination.
// Getting zero here is weird, since it may imply unexpected end of file... If we do, return the number of bytes read so far (which is compatible with the way read() would work with just one call).
450
456
break
451
457
}else{
458
+
// Partial read
452
459
numBytesRemaining -=Int(clamping: numBytesRead)
460
+
if numBytesRemaining <0{
461
+
// Just in case; we do not want to have a negative amount of bytes remaining. We will just assume that is the end.
// Anytime we read less than actually requested, stop, since the length is considered "max" for socket calls
455
-
if numBytesRead < numBytesRequested {
465
+
466
+
// The `readUntilLength` argument controls if we should end early when `read` returns less than the amount requested, or if we should continue to loop until we have reached `length` bytes.
467
+
if !readUntilLength && numBytesRead < numBytesRequested {
0 commit comments