php / php/php-src

Use zval storage for php://memory stream

Open
#11,106 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Streams Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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.
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 (see https://github.com/Nyholm/psr7/pull/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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the php://memory stream implementation and compare its storage behavior with the custom zval-backed wrapper described in the issue. Determine how php://temp should be handled and how the roughly 200K size trade-off affects the design. Done means the stream benefits from copy-on-write without regressing smaller-string performance.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.