Skip to content
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

Read file URLs in chunks #21466

Closed
jdm opened this issue Aug 20, 2018 · 10 comments
Closed

Read file URLs in chunks #21466

jdm opened this issue Aug 20, 2018 · 10 comments

Comments

@jdm
Copy link
Member

@jdm jdm commented Aug 20, 2018

The code to read from file URLs uses read_to_end to consume the entire file buffer and send it back all at once. This means the behaviour of HTTP URLs and file URLs for large files is very different.

@InquisitivePenguin
Copy link
Contributor

@InquisitivePenguin InquisitivePenguin commented Aug 23, 2018

I would like to work on this issue, if possible. Just to be clear, the goal is to read the file in chunks, rather than in one go?

@KiChjang
Copy link
Member

@KiChjang KiChjang commented Aug 23, 2018

@JacksonCoder This may be a little bit more involved. We should be using the FetchTaskTarget as the destination for the result of the file read, calling process_response_chunk when necessary. You can take a look at step 20 of main_fetch for more inspiration of how you can do so.

@InquisitivePenguin
Copy link
Contributor

@InquisitivePenguin InquisitivePenguin commented Aug 23, 2018

Since we're returning the contents of the entire file in the ResponseBody, how would reading the file in chunks improve memory usage? (which is what I assume is the goal of reading it in chunks)

@jdm
Copy link
Member Author

@jdm jdm commented Aug 24, 2018

The goal is not necessarily about reducing overall memory usage, but making the behaviour match for consumers of the network response - right now, users of the file protocol will observe a single process_response_chunk message that contains the entire file. This means that any code that can deal with incremental responses (like the HTML parser) don't get any chance to make use of the incomplete body.

@InquisitivePenguin
Copy link
Contributor

@InquisitivePenguin InquisitivePenguin commented Aug 27, 2018

I see. So I should incrementally read the file in chunks and call process_response_chunk with each chunk on the FetchTaskTarget instance?

@InquisitivePenguin
Copy link
Contributor

@InquisitivePenguin InquisitivePenguin commented Aug 27, 2018

What should the ideal size be for each chunk?

@jdm
Copy link
Member Author

@jdm jdm commented Aug 27, 2018

I would try 32kb.

@InquisitivePenguin
Copy link
Contributor

@InquisitivePenguin InquisitivePenguin commented Aug 28, 2018

Alright, I pushed my code to my fork of the repository, under the chunk-file branch.

@InquisitivePenguin
Copy link
Contributor

@InquisitivePenguin InquisitivePenguin commented Aug 31, 2018

Should I modify tests, or just make a PR?

@jdm
Copy link
Member Author

@jdm jdm commented Aug 31, 2018

I don't think there are any tests that could observe this.

@InquisitivePenguin InquisitivePenguin mentioned this issue Aug 31, 2018
4 of 5 tasks complete
bors-servo added a commit that referenced this issue Sep 6, 2018
Read file URL in chunks

<!-- Please describe your changes on the following line: -->
This is a very straightforward PR: essentially, I replaced a `read_to_end` call that occurs when processing a file URL fetch with a loop that reads the file in chunks (with a `FILE_CHUNK_SIZE` constant that specifies the chunk size, which I've put at 32KB), and then calls `target.process_response_chunk` to process the chunk. The chunk is then appended to the fetch result, and once the end of the file is reached, the result is returned.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #21466.

<!-- Either: -->
- [ ] There are tests for these changes OR
- [X] These changes do not require tests because there isn't really a way to observe this.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21560)
<!-- Reviewable:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.