This document discusses static optimization of PHP bytecode. It describes optimizations like constant propagation, dead code elimination, inlining, and specialization that have been implemented in PHP. It also discusses challenges to optimization from features like references, eval(), and variable variables. Type inference using static single assignment form is explained. Metrics on performance improvements from optimizations in libraries and applications like WordPress are provided. Current and future work on additional optimizations in PHP is mentioned.
23. 23
$x = 42;
if (cond) {
$x = 42.0;
var_dump($x);
} else {
$x = "42";
var_dump($x);
}
var_dump($x);
Static Single Assignment (SSA) Form
24. 24
$x = 42;
if (cond) {
$x = 42.0;
var_dump($x);
} else {
$x = "42";
var_dump($x);
}
var_dump($x);
Static Single Assignment (SSA) Form
Type of $x:
int|float|string
25. 25
$x = 42;
if (cond) {
$x = 42.0;
var_dump($x);
} else {
$x = "42";
var_dump($x);
}
var_dump($x);
Static Single Assignment (SSA) Form
Type of $x here:
int
Type of $x here:
float
Type of $x here:
string
Type of $x here:
float|string
26. 26
$x = 42;
if (cond) {
$x = 42.0;
var_dump($x);
} else {
$x = "42";
var_dump($x);
}
var_dump($x);
Static Single Assignment (SSA) Form
27. 27
Static Single Assignment (SSA) Form
$x_1 = 42;
if (cond) {
$x_2 = 42.0;
var_dump($x_2);
} else {
$x_3 = "42";
var_dump($x_3);
}
var_dump($x_?);
28. 28
Static Single Assignment (SSA) Form
$x_1 = 42;
if (cond) {
$x_2 = 42.0;
var_dump($x_2);
} else {
$x_3 = "42";
var_dump($x_3);
}
$x_4 = phi($x_2, $x_3);
var_dump($x_4);
43. 43
Type Inference
$a = 2**62;
$b = 2**62;
var_dump($a + $b);
// float(9.2233720368548E+18)
Accurate type inference requires value range inference!
44. 44
Type Inference
$a = 2**62;
$b = 2**62;
var_dump($a + $b);
// float(9.2233720368548E+18)
Accurate type inference requires value range inference!
Same basic concept as type inference,
but technically more involved…
57. 57
function test() {
$obj = new stdClass;
$obj->prop = 42;
// Code not using $obj in any way
var_dump($obj->prop); // ???
}
58. 58
function test() {
$obj = new stdClass;
$obj->prop = 42;
// Code not using $obj in any way
var_dump($obj->prop); // ???
}
set_error_handler(function($_1, $_2, $_3, $_4, $scope) {
$scope['obj']->prop = "foobar";
});
59. 59
function test() {
$obj = new stdClass;
$obj->prop = 42;
// Code not using $obj in any way
var_dump($obj->prop); // ???
}
set_error_handler(function($_1, $_2, $_3, $_4, $scope) {
$scope['obj']->prop = "foobar";
});
Could generate warning
60. 60
function test() {
$obj = new stdClass;
$obj->prop = 42;
// Code not using $obj in any way
var_dump($obj->prop); // ???
}
set_error_handler(function($_1, $_2, $_3, $_4, $scope) {
$scope['obj']->prop = "foobar";
});
Could generate warning
95% of instructions have
some error condition
62. • Constant Propagation, Dead Code Elimination, etc. only really
effective with inlining
62
Inlining
63. • Constant Propagation, Dead Code Elimination, etc. only really
effective with inlining
• Inlining only works if callee is known
• Only within single file (thanks opcache)
• Non-private/final instance methods can be overridden
63
Inlining
64. • Constant Propagation, Dead Code Elimination, etc. only really
effective with inlining
• Inlining only works if callee is known
• Only within single file (thanks opcache)
• Non-private/final instance methods can be overridden
• Backtraces change
64
Inlining
70. 70
State
• SSA + Type Inference in PHP 7.1
• Specialization in PHP 7.1
71. 71
State
• SSA + Type Inference in PHP 7.1
• Specialization in PHP 7.1
• Inlining, Constant Propagation, DCE, etc. not in PHP 7.1
72. 72
State
• SSA + Type Inference in PHP 7.1
• Specialization in PHP 7.1
• Inlining, Constant Propagation, DCE, etc. not in PHP 7.1
• Currently work underway on dynasm JIT using SSA + type
inference framework
73. 73
State
• SSA + Type Inference in PHP 7.1
• Specialization in PHP 7.1
• Inlining, Constant Propagation, DCE, etc. not in PHP 7.1
• Currently work underway on dynasm JIT using SSA + type
inference framework
Nikita Popov, Biagio Cosenza, Ben Juurlink, and Dmitry Stogov.
Static optimization in PHP 7. In CC'17, pages 65-75. ACM, 2017.
http://nikic.github.io/pdf/cc17_static_optimization.pdf