Optimize destructuring of array literals to elide intermediate array
Open
Nobody has claimed this yet.
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:
<?php
$a = 1;
$b = 2;
[$a, $b] = [$b, $a];
var_dump($a, $b);
currently compiles as:
$_main:
; (lines=14, args=0, vars=2, tmps=2)
; (after optimizer)
; test6.php:1-9
0000 ASSIGN CV0($a) int(1)
0001 ASSIGN CV1($b) int(2)
0002 T2 = INIT_ARRAY 2 (packed) CV1($b) NEXT
0003 T2 = ADD_ARRAY_ELEMENT CV0($a) NEXT
0004 T3 = FETCH_LIST_R T2 int(0)
0005 ASSIGN CV0($a) T3
0006 T3 = FETCH_LIST_R T2 int(1)
0007 ASSIGN CV1($b) T3
0008 FREE T2
0009 INIT_FCALL 2 112 string("var_dump")
0010 SEND_VAR CV0($a) 1
0011 SEND_VAR CV1($b) 2
0012 DO_ICALL
0013 RETURN int(1)
LIVE RANGES:
2: 0003 - 0008 (tmp/var)
it should be optimized to something like:
0000 ASSIGN CV0($a) int(1)
0001 ASSIGN CV1($b) int(2)
0002 T1 = CV0($a)
0003 T2 = CV0($b)
0004 ASSIGN CV0($a) T2
0005 ASSIGN CV1($b) T1
0006 INIT_FCALL 2 112 string("var_dump")
0007 SEND_VAR CV0($a) 1
0008 SEND_VAR CV1($b) 2
0009 DO_ICALL
0010 RETURN int(1)
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
Reproduce the PHP snippet and inspect its generated opcodes before and after optimization. Trace the compiler optimizer path that handles array destructuring and packed-array temporaries; done means the intermediate array and related fetch/free operations are elided while the variable swap remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100