Skip to content

[PFA 4/n] Optimize constant pre-bound arguments - #22829

Merged
arnaud-lb merged 5 commits into
php:masterfrom
arnaud-lb:partials-const-arg-opt
Jul 27, 2026
Merged

[PFA 4/n] Optimize constant pre-bound arguments #22829
arnaud-lb merged 5 commits into
php:masterfrom
arnaud-lb:partials-const-arg-opt

Conversation

@arnaud-lb

@arnaud-lbarnaud-lb commented Jul 20, 2026

Copy link
Copy Markdown
Member

The branch includes GH-22785, but only the commit "PFA: Optimize constant pre-bound arguments" is relevant for this PR. (Edit: branch was rebased and doesn't include GH-22785 anymore.)


Currently PFA pre-bound arguments are bound to the generated closure's lexical vars:

functionf($a, $b) {}
$f = f(1, ?);
// Generates:$tmp = 1;
$f = function ($b) use ($tmp) {
returnf($tmp, $b);
};

In this PR we detect which pre-bound arguments are constant and burn them into the generated closure instead:

functionf($a, $b) {}
$f = f(1, ?);
// Generates:$f = function ($b) {
returnf(1, $b);
};

This reduces the overhead during both instantiation and invocation of PFAs.

Comment threadZend/Optimizer/dfa_pass.c Outdated
Comment threadZend/tests/partial_application/const_arg_opt.phpt

@iliaaliliaal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Pre-bound arguments are bound to the generated closure's lexical vars:
```
function f($a, $b) {}
$f = f(1, ?);
// Generates:
$tmp = 1;
$f = function ($b) use ($tmp) {
return f($tmp, $b);
};
```
Detect which pre-bound arguments are constant and burn them into the generated
closure instead:
```
function f($a, $b) {}
$f = f(1, ?);
// Generates:
$f = function ($b) {
return f(1, $b);
};
```
@arnaud-lb
arnaud-lbforce-pushed the partials-const-arg-opt branch from 5b670e7 to 62324a8CompareJuly 27, 2026 16:00
@arnaud-lb
arnaud-lb merged commit edc169e into php:masterJul 27, 2026
18 checks passed
Comment threadZend/zend_ast.c
Comment threadZend/Optimizer/dfa_pass.c
Comment threadZend/tests/partial_application/const_arg_opt.phpt
Comment threadZend/tests/partial_application/const_arg_opt.phpt
arnaud-lb added a commit that referenced this pull request Jul 28, 2026
arnaud-lb added a commit that referenced this pull request Aug 14, 2026
GH-22829 added an optimization to burn literal arguments into the generated closure. An overlooked side effect is that these arguments are not checked anymore by zp_bind() (GH-22789).
Fix by checking these arguments earlier.
Bug found by Ryan @ Calif.io.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@arnaud-lb@iliaal@TimWolla