This talk discusses various issues of low-level PHP performance, such as: When is it more efficient to use arrays or objects? What causes catastrophic garbage collection? Does adding type annotations make PHP faster or slower?
I will answer these types of question with a (shallow) dive into PHP internals, touching on various topics like value representation, bytecode optimization and GC.
19. Value Caching
●
Only about completely static data here…
– Say a composer class map
●
APCU very inefficient for large data
– Requires unserialization and copying
●
(Ab)use opcache as a data cache
35. Caveat: Properties table
●
Some operations materialize properties table
– foreach ($object as $propName => $value)
– (array) $object
– var_dump($object)
– …
●
You pay the price for both object and array
●
No way to remove once created
39. Garbage Collection
●
Reference Counting
– Count how many times a value is used
– Destroy when count is zero
$x = "foobar"; // refcount=1
$y = $x; // refcount=2
unset($x); // refcount=1
unset($y); // refcount=0 ==> Destroy!
40. Garbage Collection
●
Reference Counting
– Count how many times a value is used
– Destroy when count is zero
$x = []; // refcount=1
$x[0] =& $x; // refcount=2
unset($x); // refcount=1
// Will never reach 0 due to cycle!
42. Garbage Collection
●
Cycle Collector
– Mark & sweep algorithm
●
Start from "roots"
●
Simulate what would happen if they were released
●
If simulation results in refcount=0, actually destroy
43. Garbage Collection
●
Cycle Collector
– Mark & sweep algorithm
– PHP <= 7.2: Fixed root buffer with 10000 entries
●
10000 objects should be enough for everyone!
44. Garbage Collection
●
Cycle Collector
– Mark & sweep algorithm
– PHP <= 7.2: Fixed root buffer with 10000 entries
●
10000 objects should be enough for everyone!
●
Cycle collector runs every time root buffer full
●
May walk graph with millions of objects each time
50. Type Declarations
●
Do they make PHP slower or faster?
●
Type declarations need to be checked
●
Type declarations allow more optimizations
51. Type Optimization
<?php
function powi(float $base, int $power): float {
if ($power == 0) return 1.0;
$result = $base;
for ($i = 1; $i < $power; $i++) {
$result *= $base;
}
return $result;
}
52. Type Optimization
<?php
function powi(float $base, int $power): float {
if ($power == 0) return 1.0;
$result = $base;
for ($i = 1; $i < $power; $i++) {
$result *= $base;
}
return $result;
}
Type inference: int
Type inference: float
53. Type Optimization
<?php
function powi(float $base, int $power): float {
if ($power == 0) return 1.0;
$result = $base;
for ($i = 1; $i < $power; $i++) {
$result *= $base;
}
return $result;
}
Type inference: int
Type inference: float
→ Eliminated return type check
54. Type Optimization
<?php
function powi(float $base, int $power): float {
if ($power == 0) return 1.0;
$result = $base;
for ($i = 1; $i < $power; $i++) {
$result *= $base;
}
return $result;
}
Use specialized
ZEND_PRE_INC_LONG
Use specialized ZEND_MUL_DOUBLE
+ eliminate compound operation
Use specialized
ZEND_IS_SMALLER_LONG
55. Type Optimization
<?php
function powi(float $base, int $power): float {
if ($power == 0) return 1.0;
$result = $base;
for ($i = 1; $i < $power; $i++) {
$result *= $base;
}
return $result;
}
Type inference: int
Type inference: float
56. Type Optimization
<?php
function powi( $base, $power) {
if ($power == 0) return 1.0;
$result = $base;
for ($i = 1; $i < $power; $i++) {
$result *= $base;
}
return $result;
}
Type inference: int|float
Type inference: mixed
57. Type Optimization
●
For this example:
– Without opcache: Performance ~same with and
without types
– With opcache: With types 2.5x faster
– Type check cost: Once
– Type optimization benefit: Multiple loop iterations
58. Type Optimization
●
For this example:
– Without opcache: Performance ~same with and
without types
– With opcache: With types 2.5x faster
– Type check cost: Once
– Type optimization benefit: Multiple loop iterations
●
But: Does not happen often in practice.
60. Global Namespace Fallback
<?php
namespace Foo;
var_dump(strlen("foobar"));
// Might be strlen()
// Might be Foostrlen()
Actual function cached on first call
→ Not particularly expensive
61. Specialized Functions
●
Some functions have optimized VM instruction
– strlen() and count()
– is_null() etc
– intval() etc
– defined()
– call_user_func() and call_user_func_array()
– in_array() and array_key_exists()
– get_class(), get_called_class() and gettype()
– func_num_args() and func_get_args()
62. Specialized Functions
●
Some functions have optimized VM instruction
●
Can only be used if function known
●
Requires fully qualified name or "use function"
63. Compile-time evaluation
<?php
namespace Foo;
function doSomething() {
if (version_compare(PHP_VERSION, '7.3', '>=')) {
// PHP 7.3 implementation
} else {
// Fallback implementation
}
}
Can't evaluate due to
namespace fallback
64. Compile-time evaluation
<?php
namespace Foo;
function doSomething() {
if (version_compare(PHP_VERSION, '7.3', '>=')) {
// PHP 7.3 implementation
} else {
// Fallback implementation
}
}
Evaluated to
true/false by opcache
65. Compile-time evaluation
<?php
namespace Foo;
use function version_compare;
use const PHP_VERSION;
function doSomething() {
if (version_compare(PHP_VERSION, '7.3', '>=')) {
// PHP 7.3 implementation
} else {
// Fallback implementation
}
}
Evaluated to
true/false by opcache