-
Notifications
You must be signed in to change notification settings - Fork 0
Use chunked file reading to avoid loading entire files into memory #37
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
Open
JanTvrdik
wants to merge
1
commit into
main
Choose a base branch
from
chunked-file-reading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Nextras\MultiQueryParser; | ||
|
|
||
| use Iterator; | ||
| use Nextras\MultiQueryParser\Exception\RuntimeException; | ||
| use function fclose; | ||
| use function feof; | ||
| use function fopen; | ||
| use function fread; | ||
| use function preg_match; | ||
| use function strlen; | ||
| use function substr; | ||
|
|
||
|
|
||
| trait BufferedFileParseTrait | ||
| { | ||
| /** | ||
| * @param callable(array<int|string, string>): array{?string, ?string} $processMatch | ||
| * @return Iterator<int, string> | ||
| */ | ||
| private function parseFileBuffered(string $path, string $pattern, callable $processMatch): Iterator | ||
| { | ||
| $handle = @fopen($path, 'rb'); | ||
| if ($handle === false) { | ||
| throw new RuntimeException("Cannot open file '$path'."); | ||
| } | ||
|
|
||
| try { | ||
| $buffer = ''; | ||
| $offset = 0; | ||
| $eof = false; | ||
| $chunkSize = 65536; // 64 KiB | ||
|
|
||
| while (true) { | ||
| // Read more data if buffer is running low and file is not exhausted | ||
| while (!$eof && strlen($buffer) - $offset < $chunkSize) { | ||
| $chunk = fread($handle, $chunkSize); | ||
| if ($chunk === false || $chunk === '') { | ||
| $eof = feof($handle); | ||
| break; | ||
| } | ||
| $buffer .= $chunk; | ||
| $eof = feof($handle); | ||
| } | ||
|
|
||
| if ($offset >= strlen($buffer)) { | ||
| break; | ||
| } | ||
|
|
||
| if (preg_match($pattern, $buffer, $match, 0, $offset) !== 1) { | ||
| break; | ||
| } | ||
|
|
||
| $matchEnd = $offset + strlen($match[0]); | ||
|
|
||
| // Safety check: if the match reaches the end of the buffer and we're not at EOF, | ||
| // read more data and retry — prevents \z from falsely matching at a chunk boundary | ||
| if ($matchEnd >= strlen($buffer) && !$eof) { | ||
| $chunk = fread($handle, $chunkSize); | ||
| if ($chunk !== false && $chunk !== '') { | ||
| $buffer .= $chunk; | ||
| $eof = feof($handle); | ||
| continue; // retry the match with more data | ||
| } | ||
| $eof = true; | ||
| } | ||
|
|
||
| $offset = $matchEnd; | ||
|
|
||
| [$query, $newPattern] = $processMatch($match); | ||
|
|
||
| if ($newPattern !== null) { | ||
| $pattern = $newPattern; | ||
| } | ||
|
|
||
| if ($query !== null) { | ||
| yield $query; | ||
| } elseif ($newPattern === null) { | ||
| // No query and no pattern change means we hit the \z end-of-content branch | ||
| break; | ||
| } | ||
|
|
||
| // Trim consumed content from the buffer to free memory | ||
| if ($offset > $chunkSize) { | ||
| $buffer = substr($buffer, $offset); | ||
| $offset = 0; | ||
| } | ||
| } | ||
|
|
||
| if ($offset !== strlen($buffer)) { | ||
| throw new RuntimeException("Failed to parse file '$path', please report an issue."); | ||
| } | ||
| } finally { | ||
| fclose($handle); | ||
| } | ||
| } | ||
| } | ||
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
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
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.
The buffering logic will fail for queries larger than 64 KiB. After reading the first chunk (line 38), if the remaining buffer is >= chunkSize (line 37), the inner loop exits. If preg_match then fails because the query is incomplete (no delimiter found yet), the outer loop breaks (line 52), and a RuntimeException is thrown (line 92).
The fix should ensure that if preg_match fails and we're not at EOF, we continue reading more data instead of breaking. One approach would be to:
This is critical because the PR's goal is to handle large files without loading them entirely into memory, but it fails for any individual query exceeding the chunk size.