-
Notifications
You must be signed in to change notification settings - Fork 262
Avoid extra memory allocation when doing chunked upload for large file #529
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
Conversation
|
@MIchaelMainer @darrelmiller anyone can help review this? |
|
Hey @zsybupt, sorry for the delay. Unfortunately we are all tied up with other priorities at the moment. I did take a quick review of the PR and there are a number of things I am unsure about. I do acknowledge there is room for improvement in this code, however I'm not immediately seeing where that readbuffer is getting reallocated for each chunk. Also, creating an entire new Stream subclass for this scenario seems like a lot of risk unless that class is copied from some existing project. My experience has been getting stream subclasses right is hard. |
|
@darrelmiller Thanks for your quick reply.
For a 170MiB file BeforeAfter |
|
@zsybupt Ahh, that makes more sense and makes me feel much more comfortable about the new stream class. I will try and review this later today. /cc @MIchaelMainer |
|
Sorry that my PC language is Chinese, the original ChunkedUploadProvider consume about 113.4 MiB memory, with the ReadOnlySubStream the memory reduce to 13.9 MiB |
|
Any updates of this pr ? |
|
@zsybupt If it is okay, I will be merging this PR into the branch task/uploadTask so as to enable us to do more tests and build upon this work to improve the UploadProvider so as to provide more usability such as giving progress callbacks during upload. |
|
@andrueastman ok,Thanks |
Changes proposed in this pull request
ChunkedUploadProvider trys to help with large file uploading, it divides the large stream to chunk-size stream and upload each one by one. The current implementation allocate maxChunksize of byte[] buffer each time:
and an extra stream copy:
If there is a total 500MiB size file, open a FileStream with a 10MiB max chunk size, it will allocate a 10 * 1024 * 1024 size buffer 50 times (extra 500MiB memory allocation), and do 100 times buffer operations (50 read and 50 write).
Because the upload stream supports seek and read operation, this pull request provide a ReadOnlySubStream to represent a chunk of a upload stream to save memory when dealing with large file and remove the extra copy operations