Most of the content of this article is based on the RFC document of AST: https://wiki.php.net/rfc/abstract_syntax_tree, Excerpts from the source document are introduced for ease of understanding.
This article will not tell you what an abstract syntax tree is. This needs to be understood by yourself. This article only describes some changes that AST brings to PHP.
New execution process
An important change in the core of PHP7 is the addition of AST. In PHP5, the execution process from php scripts to opcodes is:
1. Lexing: lexical scanning analysis, converting source files into token streams;
2. Parsing: syntax analysis, Op arrays are generated at this stage.
3. In PHP7, op arrays are no longer directly generated during the syntax analysis stage, but AST is generated first, so there is one more step in the process:
4. Lexing: lexical scanning analysis, converting the source file into Convert to token stream;
5. Parsing: syntax analysis, generate abstract syntax tree from token stream;
6. Compilation: generate op arrays from abstract syntax tree.
Execution time and memory consumption
From the above steps, this is one more step than the previous process, so according to common sense, this will increase the program execution time and memory usage. But in fact, the memory usage has indeed increased, but the execution time has decreased.
The following results are obtained by testing three scripts: small (about 100 lines of code), medium (about 700 lines), and large (about 2800 lines). Test script: https ://gist.github.com/nikic/289b0c7538b46c2220bc
Execution time of compiling each file 100 times (note that the test result time of the article is 14 years, PHP7 is also called PHP-NG ):
php-ast | diff | ||
---|---|---|---|
0.180s | 0.160s | -12.5% | |
1.492s | 1.268s | -17.7% | |
6.703s | 5.736s | -16.9% |
Memory peak in a single compilation:
php-ast | diff | ||
---|---|---|---|
378kB | 414kB | 9.5% | |
507kB | 643kB | 26.8% | |
1084kB | 1857kB | 71.3% |
php-ng |
php-ast | diff | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
25.5ms | 22.8ms | -11.8% | ##MEMORY | |||||||||||||||
2482kB | 5.1% |
PHP5 | PHP7 | |
---|---|---|
$$foo['bar']['baz']
|
${$foo['bar']['baz']}
|
($$foo)['bar']['baz'] |
$foo->$bar['baz']
| ##$foo->{$bar['baz']}
| ($foo->$bar)['baz']
|
$foo->{$bar['baz']}() |
##($ foo->$bar)['baz']() |
##Foo::$bar['baz']() |
Foo::{$bar['baz']}()
|
(Foo::$bar)['baz']()
|
On the whole, the previous order is from right to left, now it is from left to right, and it also follows the principle that brackets do not affect behavior. These complex variable writing methods need to be paid attention to in actual development. |
The above is the detailed content of Changes brought to PHP7 by the new Abstract Syntax Tree (AST). For more information, please follow other related articles on the PHP Chinese website!