php / php/php-src

Foreach argument CoW should be optimized

Open
#11,775 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

Currently, foreach does CoW on any foreach argument ($arr) modification. This is too conservative.

https://3v4l.org/ShQmT/rfc#vgit.master

The repro shows common usecase of modifying currently iterated item/index. CoW is not needed, as the currently iterated item/index will never be iterated again.

In general, any already iterated item/key does not need CoW when unset/modified.

Also $arr append does not need CoW neither as the last element of the original $arr can be easily tracked.

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 with the linked 3v4l.org/ShQmT reproduction and trace how PHP's foreach implementation handles copy-on-write for modified items, keys, unset operations, and appends. Done means demonstrating that already iterated elements and the append case avoid unnecessary CoW while preserving foreach behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend, performance
Issue type
Refactor
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.