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

Use zval storage for php://memory stream #11106

Open
nicolas-grekas opened this issue Apr 20, 2023 · 0 comments
Open

Use zval storage for php://memory stream #11106

nicolas-grekas opened this issue Apr 20, 2023 · 0 comments

Comments

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Apr 20, 2023

Description

The following code increases the peak memory usage of PHP twice:

$content = str_repeat('hello', 50000);
$h = fopen('php://memory', '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.
Nyholm/psr7#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 (see Nyholm/psr7#241).

Could it be possible to improve php://memory (and possibly php://temp) to make it use a zval storage to benefit from copy-on-write?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants