[php-src] Issue #11106: Use zval storage for php://memory stream
| From: | nicolas-grekas | Date: | Thu, 20 Apr 2023 08:36:10 +0000 |
| Subject: | [php-src] Issue #11106: Use zval storage for php://memory stream | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244235@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11106
Author: nicolas-grekas
### Description
The following code increases the peak memory usage of PHP:
```php
$content = str_repeat('hello', 50000);
$h = fopen('php://temp', 'r+');
fwrite($h, $content);
```
The reason is that PHP cannot currently use copy-on-write when writing to the stream.
This is something that can be worked around by using a custom stream wrapper that keeps data in a
zval, as done in e.g.
https://github.com/Nyholm/psr7/pull/230
Using this proved useful for strings longer than 200K. For smaller strings, using a custom stream
wrapper has a higher cost than the memcopy.
Could it be possible to improve
php://memory to make it us a zval storage to benefit
from copy-on-write?