-
Notifications
You must be signed in to change notification settings - Fork 735
Fix reading overly large files #954
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
Merged
Leonard Hecker (lhecker)
merged 1 commit into
main
from
dev/lhecker/read-large-file-fix
Sep 21, 2026
+62
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -853,6 +853,23 @@ impl TextBuffer { | |
| first_chunk_len: usize, | ||
| done: bool, | ||
| ) -> io::Result<()> { | ||
| // Get the length of the file. 0 = not a file. | ||
| let file_len = if done { | ||
| // But if the first 4KiB read already contains the entire file, we won't need | ||
| // the file length below (we early return). The value here doesn't matter. | ||
| 0 | ||
| } else { | ||
| // We can't acquire the length on pipes, for instance. | ||
| file.metadata().ok().and_then(|m| m.len().try_into().ok()).unwrap_or(0) | ||
| }; | ||
|
Comment on lines
+856
to
+864
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this and the changes below prevent that we ever (typically) run into these out of memory situations in the first place. |
||
|
|
||
| // If we have a file length, reserve enough space for it. | ||
| // The call is a no-op for small files (currently <4GiB). | ||
| if file_len > 0 { | ||
| self.buffer.try_reserve(file_len); | ||
| } | ||
|
|
||
| // Handle the first chunk we already read for encoding detection. | ||
| { | ||
| let mut first_chunk = unsafe { buf[..first_chunk_len].assume_init_ref() }; | ||
| if first_chunk.starts_with(b"\xEF\xBB\xBF") { | ||
|
|
@@ -862,27 +879,22 @@ impl TextBuffer { | |
|
|
||
| self.buffer.replace(0..0, first_chunk); | ||
| } | ||
|
|
||
| if done { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| // If we don't have file metadata, the input may be a pipe or a socket. | ||
| // Every read will have the same size until we hit the end. | ||
| let mut chunk_size = 128 * KIBI; | ||
| let mut extra_chunk_size = 128 * KIBI; | ||
|
|
||
| if let Ok(m) = file.metadata() { | ||
| // Usually the next read of size `chunk_size` will read the entire file, | ||
| // but if the size has changed for some reason, then `extra_chunk_size` | ||
| // should be large enough to read the rest of the file. | ||
| // 4KiB is not too large and not too slow. | ||
| let len = m.len() as usize; | ||
| chunk_size = len.saturating_sub(first_chunk_len); | ||
| extra_chunk_size = 4 * KIBI; | ||
| } | ||
|
|
||
| loop { | ||
| let chunk_size = if file_len > 0 { | ||
| // If we know the file length: | ||
| // * Read the file until the end | ||
| // * And if we're still reading at that point, read in 4KiB chunks (e.g. if someone wrote | ||
| // to the file concurrently; typically this won't happen, so the chunk size is small). | ||
| file_len.checked_sub(self.text_length()).unwrap_or(4 * KIBI) | ||
| } else { | ||
| // For pipes, sockets, etc., read in 128KiB chunks, because anything smaller has poor perf. | ||
| 128 * KIBI | ||
| }; | ||
|
|
||
| let gap = self.buffer.allocate_gap(self.text_length(), chunk_size, 0); | ||
| if gap.is_empty() { | ||
| break; | ||
|
|
@@ -894,7 +906,6 @@ impl TextBuffer { | |
| } | ||
|
|
||
| self.buffer.commit_gap(read); | ||
| chunk_size = extra_chunk_size; | ||
| } | ||
|
|
||
| Ok(()) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this change (and the removed if condition below), reading files larger than the max. memory would read in chunks of 4KiB.