The document discusses optimizations made in PHP 7 to improve performance. Some key points:
- PHP 7 optimized how values and objects are stored in memory by removing unnecessary overhead like reference counting. This reduces memory usage and indirection.
- Arrays were optimized to reduce the number of allocations, size, and lookup indirections compared to PHP 5.
- Objects saw reduced allocations, size and indirection for property values in PHP 7.
- Other optimizations included using 64-bit integers, improving immutable array performance, and enhancing the virtual machine to better manage function call stacks.
11. Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU L1D
1ns 32KB
12. Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU
4ns
L2
256 KB
L1D
1ns 32KB
13. Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU L312ns
a few MB
4ns
L2
256 KB
L1D
1ns 32KB
14. RAM
100ns
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU L312ns
a few MB
4ns
L2
256 KB
L1D
1ns 32KB
15. RAM
100ns
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
• Less data => more fits into the cache
CPU L312ns
a few MB
4ns
L2
256 KB
L1D
1ns 32KB
17. Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Reduce indirection
• Pointer points to pointer pointing to pointer pointing to pointer pointing to
pointer pointing to pointer pointing to what you actually want
What you
have
What you
want
18. Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Reduce indirection
• Pointer points to pointer pointing to pointer pointing to pointer pointing to
pointer pointing to pointer pointing to what you actually want
• Fewer memory accesses
What you
have
What you
want
41. PHP 5 without zval PHP 7 with zval
Allocations: 2 + 1 per element 2
Size: 72 + 80 per element 56 + 36 per element
Indirections (lookup): 4 2
Arrays: comparison
42. PHP 5 without zval PHP 7 with zval
Allocations: 2 + 1 per element 2
Size: 72 + 80 per element 56 + 36 per element
Indirections (lookup): 4 2
PHP 5 w/ unique zvals
2 + 2 per element
72 + 112 per element
4
Arrays: comparison
70. Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
• Only in global scope or for fully qualified calls
namespace foo;
echo strlen($str);
71. Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
• Only in global scope or for fully qualified calls
namespace foo;
echo strlen($str);
Is this strlen() or foostrlen()?
72. Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
• Only in global scope or for fully qualified calls
namespace foo;
echo strlen($str);
This is definitely strlen()!
81. Opcache
• File cache
• Alternative (or addition) to SHM
• Shared hosting? PHP restart / cache reset?
82. Opcache
• File cache
• Alternative (or addition) to SHM
• Shared hosting? PHP restart / cache reset?
• Time for 500 sequential WP 4.1 requests
SHM 3 s
File cache 6 s
No cache 24 s