perf: stream SFTP uploads/downloads instead of buffering whole file#195
Open
Yaminyam wants to merge 1 commit intolablup:mainfrom
Open
perf: stream SFTP uploads/downloads instead of buffering whole file#195Yaminyam wants to merge 1 commit intolablup:mainfrom
Yaminyam wants to merge 1 commit intolablup:mainfrom
Conversation
Upload (`upload_file`, `upload_dir_recursive`) used `tokio::fs::read` to load the entire local file into a `Vec<u8>` before calling `write_all`, and download (`download_file`, `download_dir_recursive`) used `read_to_end` into a pooled buffer + `clone()` to a separate `Vec` before writing locally. For multi-GB transfers this means peak RSS scales with file size and large files OOM the client. Replace each path with a small `stream_copy()` helper that loops on 256 KiB reads and writes through the existing `AsyncRead`/`AsyncWrite` implementations on `tokio::fs::File` and `russh_sftp::client::fs::File`. Buffer size matches the SFTP MAX_WRITE_LENGTH so each chunk maps to a single SFTP packet without further fragmentation. Verified locally on macOS arm64 against `bssh-server` v2.1.3 over loopback with a 1 GiB file: | Op | Build | real | RSS | |----------|------------|---------|----------| | upload | unpatched | 38.65s | 3.23 GB | | upload | streaming | 3.47s | 20 MB | | download | unpatched | 3.93s | 2.17 GB | | download | streaming | 3.41s | 16 MB | Peak RSS drops ~160x and uploads complete ~11x faster (a single multi-MB `write_all` apparently serializes much worse through the SFTP pipeline than 256 KiB chunked writes).
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Upload (
upload_file,upload_dir_recursive) usedtokio::fs::readto load the entire local file into aVec<u8>before callingwrite_all, and download (download_file,download_dir_recursive) usedread_to_endinto a pooled buffer +clone()to a separateVecbefore writing locally. For multi-GB transfers this means peak RSS scales with file size and large files OOM the client.Replace each path with a small
stream_copy()helper that loops on 256 KiB reads and writes through the existingAsyncRead/AsyncWriteimplementations ontokio::fs::Fileandrussh_sftp::client::fs::File. Buffer size matches the SFTPMAX_WRITE_LENGTHso each chunk maps to a single SFTP packet without further fragmentation.Measured impact
Verified locally on macOS arm64 against
bssh-serverv2.1.3 over loopback with a 1 GiB file:Peak RSS drops ~160x and uploads complete ~11x faster (a single multi-MB
write_allapparently serializes much worse through the SFTP pipeline than 256 KiB chunked writes).Test plan