Skip to content

Conversation

Rob-Hague
Copy link
Collaborator

@Rob-Hague Rob-Hague commented Sep 28, 2025

This change unifies the SFTP download implementations that exist via DownloadFile and
via SftpFileStream, by rewriting SftpFileStream to perform the same "read-aheads" as
DownloadFile. This brings the performance of downloads via SftpFileStream in line with
DownloadFile, such that the latter is now effectively SftpFileStream.CopyTo. It also
brings the recently added DownloadFileAsync up to speed since that was implemented via
SftpFileStream.CopyToAsync.

The methodology is a mix of the previous one and that within OpenSSH: the first call to
SftpFileStream.Read sends one read request to the server. The second sends two and when
not interrupted by Write or similar, the number of in-flight read requests continues to
scale up in this fashion.

I have measured CopyTo to be 3-20x faster than before, depending on file size and server
round-trip time. See #1705 (comment)

closes #1644
contributes to #1331
fixes #1633
fixes #1618

This change unifies the SFTP download implementations that exist via DownloadFile and
via SftpFileStream, by rewriting SftpFileStream to perform the same "read-aheads" as
DownloadFile. This brings the performance of downloads via SftpFileStream in line with
DownloadFile, such that the latter is now effectively SftpFileStream.CopyTo. It also
brings the recently added DownloadFileAsync up to speed since that was implemented via
SftpFileStream.CopyToAsync.

The methodology is a mix of the previous one and that within OpenSSH: the first call to
SftpFileStream.Read sends one read request to the server. The second sends two and when
not interrupted by Write or similar, the number of in-flight read requests continues to
scale up in this fashion.

I have measured CopyTo to be 3-20x faster than before, depending on file size and server
round-trip time.
@Rob-Hague Rob-Hague force-pushed the sftpfilestreamreader branch from 147a4bf to 83c4587 Compare October 19, 2025 21:38
@Rob-Hague
Copy link
Collaborator Author

Performance analysis:

Summary: probable ~10% regression in DownloadFile for small files on high-latency connections. Otherwise, no statistically significant differences for DownloadFile.

Tests were run for different file sizes against a geographically close server (Europe) and one far away (US West).

1MB, Europe The noisiest test. No statistically significant difference for DownloadFile.

1MB_Europe

1MB, West US The DownloadFile difference is somewhat consistent

1MB_WestUS

8MB, Europe

8MB_Europe

8MB, West US

8MB_WestUS

64MB, Europe

64MB_Europe

64MB, West US

64MB_WestUS

@Rob-Hague Rob-Hague marked this pull request as ready for review October 19, 2025 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants