Foreach argument CoW should be optimized
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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