[php-src] Issue #12390: ```fgets``` memory usage
| From: | AurelienPillevesse | Date: | Mon, 09 Oct 2023 06:37:40 +0000 |
| Subject: | [php-src] Issue #12390: ```fgets``` memory usage | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245520@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12390
Author: AurelienPillevesse
### Description
The following code:
```php
<?php
$stream = fopen($filename, 'r');
if ($stream === false) {
throw new \Exception('Failed to open file: ' . $filename);
}
$list = [];
while (($line = fgets($stream)) !== false) {
if (preg_match($beginningPattern, $line, $matches)) {
$tmp = $line;
while (($line = fgets($stream)) !== false) {
$tmp .= $line;
if (preg_match($endingPattern, $line)) {
$list[] = $tmp;
break;
}
}
}
}
```
The downloaded file used is the script is this one : https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
Resulted in this output:
```
string(29) "Memory usage: 31.68 MB bytes"
string(34) "Peak memory usage: 36.87 MB bytes"
```
It seems that it takes a lot of memory resource to iterate over the file and I can see this in the
function :
https://github.com/php/php-src/blob/master/ext/standard/file.c#L912
Maybe an improvement is possible with this
TODO or something else ?
### PHP Version
8.1
### Operating System
Windows