-
Notifications
You must be signed in to change notification settings - Fork 8k
opcache: re-enable PASS_15 (constant collection) by default #22007
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
Closed
wilaak
wants to merge
2
commits into
php:master
from
wilaak:fix/inline-file-scope-const-declarations-master
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,43 @@ | ||
| --TEST-- | ||
| const at file scope is inlined by the optimizer, define() is not | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --INI-- | ||
| opcache.enable_cli=1 | ||
| opcache.opt_debug_level=0x20000 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| const CONST_VAL = 1; | ||
| define('DEFINE_VAL', 2); | ||
|
|
||
| function use_const() { | ||
| return CONST_VAL; | ||
| } | ||
|
|
||
| function use_define() { | ||
| return DEFINE_VAL; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 DECLARE_CONST string("CONST_VAL") int(1) | ||
| 0001 DECLARE_CONST string("DEFINE_VAL") int(2) | ||
| 0002 RETURN int(1) | ||
|
|
||
| use_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(1) | ||
|
|
||
| use_define: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 T%d = FETCH_CONSTANT string("DEFINE_VAL") | ||
| 0001 RETURN %s | ||
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,86 @@ | ||
| --TEST-- | ||
| Global and namespace const declarations are inlined by the optimizer | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --INI-- | ||
| opcache.enable_cli=1 | ||
| opcache.opt_debug_level=0x20000 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| const GLOBAL_CONST = 42; | ||
|
|
||
| function use_global_const() { | ||
| return GLOBAL_CONST; | ||
| } | ||
|
|
||
| function use_global_const_fqn() { | ||
| return \GLOBAL_CONST; | ||
| } | ||
|
|
||
| class MyClass { | ||
| const CLASS_CONST = 99; | ||
| final const FINAL_CLASS_CONST = 77; | ||
|
|
||
| public function use_class_const() { | ||
|
wilaak marked this conversation as resolved.
|
||
| return self::CLASS_CONST; | ||
| } | ||
|
|
||
| public function use_static_class_const() { | ||
| return static::CLASS_CONST; | ||
| } | ||
|
|
||
| public function use_static_final_class_const() { | ||
| return static::FINAL_CLASS_CONST; | ||
| } | ||
| } | ||
|
|
||
| function use_class_const_static() { | ||
| return MyClass::CLASS_CONST; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 DECLARE_CONST string("GLOBAL_CONST") int(42) | ||
| 0001 RETURN int(1) | ||
|
|
||
| use_global_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(42) | ||
|
|
||
| use_global_const_fqn: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(42) | ||
|
|
||
| use_class_const_static: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(99) | ||
|
|
||
| MyClass::use_class_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(99) | ||
|
|
||
| MyClass::use_static_class_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 T%d = FETCH_CLASS_CONSTANT (static) (exception) string("CLASS_CONST") | ||
| 0001 RETURN %s | ||
|
|
||
| MyClass::use_static_final_class_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(77) | ||
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,41 @@ | ||
| --TEST-- | ||
| Namespace const declarations are inlined by the optimizer | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --INI-- | ||
| opcache.enable_cli=1 | ||
| opcache.opt_debug_level=0x20000 | ||
| --FILE-- | ||
| <?php | ||
| namespace MyNS; | ||
|
|
||
| const NS_CONST = 100; | ||
|
|
||
| function use_ns_const() { | ||
| return NS_CONST; | ||
| } | ||
|
|
||
| function use_ns_const_fqn() { | ||
| return \MyNS\NS_CONST; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 DECLARE_CONST string("MyNS\\NS_CONST") int(100) | ||
| 0001 RETURN int(1) | ||
|
|
||
| MyNS\use_ns_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(100) | ||
|
|
||
| MyNS\use_ns_const_fqn: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(100) |
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-- | ||
| define() followed by const redeclaration is not inlined | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --INI-- | ||
| opcache.enable_cli=1 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| define('FOO', 'first'); | ||
| const FOO = 'second'; | ||
|
|
||
| function get_foo() { | ||
| return FOO; | ||
| } | ||
|
|
||
| var_dump(get_foo()); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Warning: Constant FOO already defined, this will be an error in PHP 9 in %s on line %d | ||
| string(5) "first" |
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,2 @@ | ||
| <?php | ||
| const EXTERNAL_CONST = 42; |
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,38 @@ | ||
| --TEST-- | ||
| const declared only in a separate required file is not inlined by the optimizer | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --INI-- | ||
| opcache.enable_cli=1 | ||
| opcache.opt_debug_level=0x20000 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| require __DIR__ . '/pass1_const_inline_005.inc'; | ||
|
|
||
| function use_external_const() { | ||
| return EXTERNAL_CONST; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 INCLUDE_OR_EVAL (require) string("%s") | ||
| 0001 RETURN int(1) | ||
|
|
||
| use_external_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 T%d = FETCH_CONSTANT string("EXTERNAL_CONST") | ||
| 0001 RETURN %s | ||
|
|
||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 DECLARE_CONST string("EXTERNAL_CONST") int(42) | ||
| 0001 RETURN 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,2 @@ | ||
| <?php | ||
| const SHARED_CONST = 42; |
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,50 @@ | ||
| --TEST-- | ||
| const redeclared after being declared in a separate required file: optimizer inlines local value, runtime warning | ||
| --EXTENSIONS-- | ||
| opcache | ||
| --INI-- | ||
| opcache.enable_cli=1 | ||
| opcache.opt_debug_level=0x20000 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| require __DIR__ . '/pass1_const_inline_006.inc'; | ||
|
|
||
| const SHARED_CONST = 99; | ||
|
|
||
| function use_shared_const() { | ||
| return SHARED_CONST; | ||
| } | ||
|
|
||
| var_dump(use_shared_const()); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 INCLUDE_OR_EVAL (require) string("%s") | ||
| 0001 DECLARE_CONST string("SHARED_CONST") int(99) | ||
| 0002 INIT_FCALL 1 %d string("var_dump") | ||
| 0003 INIT_FCALL 0 %d string("use_shared_const") | ||
| 0004 T%d = DO_UCALL | ||
| 0005 SEND_VAL T%d 1 | ||
| 0006 DO_ICALL | ||
| 0007 RETURN int(1) | ||
|
|
||
| use_shared_const: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 RETURN int(99) | ||
|
|
||
| $_main: | ||
| ; (lines=%d, args=%d, vars=%d, tmps=%d) | ||
| ; (after optimizer) | ||
| ; %s | ||
| 0000 DECLARE_CONST string("SHARED_CONST") int(42) | ||
| 0001 RETURN int(1) | ||
|
|
||
| Warning: Constant SHARED_CONST already defined, this will be an error in PHP 9 in %s on line %d | ||
| int(99) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also add tests for what happens if the constant is already declared in a separate file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the constant is only declared in the separate file the optimizer can't inline it since the value is unknown at compile time, so it emits FETCH_CONSTANT.
If you also redeclare it locally the optimizer inlines the local value, but at runtime the required declaration wins and the local one warns. You get different results with and without opcache, so there's a real divergence there!