-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[PFA 4/n] Optimize constant pre-bound arguments #22829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arnaud-lb
wants to merge
8
commits into
php:master
Choose a base branch
from
arnaud-lb:partials-const-arg-opt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b98e2e4
Use a pointer to the declaring opline->lineno as unique addr, instead…
arnaud-lb 80cffd4
zend_partial_create(): Remove the dependency on the declaring op_array
arnaud-lb 9391556
Support for PFA in constant expressions
arnaud-lb b362aca
PFA: Optimize constant pre-bound arguments
arnaud-lb 4d5c2d3
Review
arnaud-lb c0587a8
UB
arnaud-lb 836631d
Fix tests
arnaud-lb 5b670e7
Fix tests
arnaud-lb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| --TEST-- | ||
| Constant argument optimization | ||
| --DESCRIPTION-- | ||
| Pre-bound arguments that are constant can be burned into the generated | ||
| op_array instead of being passed via the Closure's lexical vars. | ||
| --ENV-- | ||
| A=1 | ||
| --INI-- | ||
| opcache.enable=1 | ||
| opcache.enable_cli=1 | ||
| opcache.optimization_level=-1 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a, $b) { | ||
| var_dump([$a, $b]); | ||
| } | ||
|
|
||
| function g() { | ||
| return 3; | ||
| } | ||
|
|
||
| function h($a, $b, $c) { | ||
| var_dump([$a, $b, $c]); | ||
| } | ||
|
|
||
| function i($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, $a10, $a11, $a12, $a13, $a14, $a15, $a16, $a17, $a18, $a19, $a20, $a21, $a22, $a23, $a24, $a25, $a26, $a27, $a28, $a29, $a30, $a31, $a32, $a33) { | ||
| var_dump($a33); | ||
| } | ||
|
|
||
| function print_lexical_vars($f) { | ||
| $vars = new ReflectionFunction($f)->getClosureUsedVariables(); | ||
| if ($vars === []) { | ||
| echo "no lexical vars\n"; | ||
| } else { | ||
| $varNames = array_keys($vars); | ||
| echo 'lexical vars: ', implode(', ', $varNames), "\n"; | ||
| } | ||
| } | ||
|
|
||
| echo "# Non-constant pre-bound argument:\n"; | ||
| $f = f(getenv('A'), ?); | ||
| print_lexical_vars($f); | ||
| $f(2); | ||
|
|
||
| echo "# Constant pre-bound argument:\n"; | ||
| $f = f(2, ?); | ||
| print_lexical_vars($f); | ||
| $f(2); | ||
|
|
||
| echo "# Constant pre-bound argument (inverted):\n"; | ||
| $f = f(?, 2); | ||
| print_lexical_vars($f); | ||
| $f(1); | ||
|
|
||
| echo "# Inlined pre-bound argument:\n"; | ||
| $f = f(g(), ?); | ||
| print_lexical_vars($f); | ||
| $f(2); | ||
|
|
||
| echo "# Constexpr pre-bound argument:\n"; | ||
| const B = 4; | ||
| $f = f(B, ?); | ||
| print_lexical_vars($f); | ||
| $f(2); | ||
|
|
||
| echo "# Mixed arguments:\n"; | ||
| $f = h(5, getenv('A'), ?); | ||
| print_lexical_vars($f); | ||
| $f(2); | ||
|
|
||
| echo "# Constant array pre-bound argument:\n"; | ||
| $f = f(['foo' => 'bar'], ?); | ||
| print_lexical_vars($f); | ||
| $f(2); | ||
|
|
||
| echo "# Named arguments:\n"; | ||
| $f = h(1, c: ?, b: ?); | ||
| print_lexical_vars($f); | ||
| $f(2, 3); | ||
|
|
||
| echo "# Many arguments (optimization can not be applied for all args) :\n"; | ||
| $f = i(?, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33); | ||
| print_lexical_vars($f); | ||
| $f(33); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| # Non-constant pre-bound argument: | ||
| lexical vars: a | ||
| array(2) { | ||
| [0]=> | ||
| string(1) "1" | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| # Constant pre-bound argument: | ||
| no lexical vars | ||
| array(2) { | ||
| [0]=> | ||
| int(2) | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| # Constant pre-bound argument (inverted): | ||
| no lexical vars | ||
| array(2) { | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| # Inlined pre-bound argument: | ||
| no lexical vars | ||
| array(2) { | ||
| [0]=> | ||
| int(3) | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| # Constexpr pre-bound argument: | ||
| lexical vars: a | ||
| array(2) { | ||
| [0]=> | ||
| int(4) | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| # Mixed arguments: | ||
| lexical vars: b | ||
| array(3) { | ||
| [0]=> | ||
| int(5) | ||
| [1]=> | ||
| string(1) "1" | ||
| [2]=> | ||
| int(2) | ||
| } | ||
| # Constant array pre-bound argument: | ||
| no lexical vars | ||
| array(2) { | ||
| [0]=> | ||
| array(1) { | ||
| ["foo"]=> | ||
| string(3) "bar" | ||
| } | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| # Named arguments: | ||
| no lexical vars | ||
| array(3) { | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(3) | ||
| [2]=> | ||
| int(2) | ||
| } | ||
| # Many arguments (optimization can not be applied for all args) : | ||
| lexical vars: a33 | ||
| int(33) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --TEST-- | ||
| PFA in constexpr 001 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g("foo", ?)) { | ||
| return $a(1); | ||
| } | ||
|
|
||
| function g(string $a, int $b) { | ||
| return [$a, $b]; | ||
| } | ||
|
|
||
| var_dump(f()); | ||
| var_dump(f()); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| array(2) { | ||
| [0]=> | ||
| string(3) "foo" | ||
| [1]=> | ||
| int(1) | ||
| } | ||
| array(2) { | ||
| [0]=> | ||
| string(3) "foo" | ||
| [1]=> | ||
| int(1) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --TEST-- | ||
| PFA in constexpr: non-constexpr arg | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(new stdClass, $x, ?)) { | ||
| return $a(1); | ||
| } | ||
|
|
||
| function g(string $a, int $b) { | ||
| return [$a, $b]; | ||
| } | ||
|
|
||
| try { | ||
| var_dump(f()); | ||
| } catch (Error $e) { | ||
| echo $e::class, ": ", $e->getMessage(), " on line ", $e->getLine(); | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Constant expression contains invalid operations in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --TEST-- | ||
| PFA in constexpr: named args | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(c: ?, ...)) { | ||
| return $a(1.5, b: 3); | ||
| } | ||
|
|
||
| function g(string $a = 'hello', int $b = 2, float $c = 0) { | ||
| return [$a, $b, $c]; | ||
| } | ||
|
|
||
| var_dump(f()); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| array(3) { | ||
| [0]=> | ||
| string(5) "hello" | ||
| [1]=> | ||
| int(3) | ||
| [2]=> | ||
| float(1.5) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --TEST-- | ||
| PFA in constexpr: extra named args | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(?, foo: 'bar', ...)) { | ||
| return $a(1, bar: 'baz'); | ||
| } | ||
|
|
||
| function g(...$args) { | ||
| return [$args]; | ||
| } | ||
|
|
||
| var_dump(f()); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| array(1) { | ||
| [0]=> | ||
| array(3) { | ||
| [0]=> | ||
| int(1) | ||
| ["foo"]=> | ||
| string(3) "bar" | ||
| ["bar"]=> | ||
| string(3) "baz" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --TEST-- | ||
| PFA in constexpr: variadic placeholder must be after positional params | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(..., 1)) { | ||
| } | ||
|
|
||
| f(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Variadic placeholder must be last in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --TEST-- | ||
| PFA in constexpr: variadic placeholder must be after named params | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(..., foo: 1)) { | ||
| } | ||
|
|
||
| f(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Variadic placeholder must be last in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --TEST-- | ||
| PFA in constexpr: variadic placeholder may only appear once | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(..., ...)) { | ||
| } | ||
|
|
||
| f(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Variadic placeholder may only appear once in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --TEST-- | ||
| PFA in constexpr: variadic placeholder not allowed to be named | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function f($a = g(foo: ...)) { | ||
| } | ||
|
|
||
| f(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Parse error: syntax error, unexpected token "..." in %s on line %d |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.