SlideShare a Scribd company logo
PHP 7 performances
From PHP 5 in Symfony context
Hello everybody
 Julien PAULI
 SensioLabs Blackfire team
 Programming with PHP since early 2000s
 Now : Unix system programmer (C)
 PHP Internals programmer/reviewer
 PHP 5.5 & 5.6 Release Manager
 @julienpauli
 Tech blog at jpauli.github.io
 jpauli@php.net
What we'll cover together
 Profiling a simple SF2 app
 Under PHP 5
 Under PHP 7
 Compare profiles using Blackfire graph comparison
 Analyze numbers
 Dive into PHP 7 performances
 Structures optimizations
 New variable model (zval)
 New HashTable model
 String management with zend_string
 Other ideas...
Profiles
 Done on my laptop (not on prod env)
 LP64
 Done on app_dev.php (debug mode)
 Do not take numbers for real
 But relative measures
 Performed with Blackfire
 On PHP-5.6 latest
 On PHP-7.0.0RC8
Blackfire
 General profiler
 Not only PHP, but works best for PHP
 Free version exists
 Collects many metrics
 memory, CPU, IO, Network trafic, SQL ...
 Graphs useful info, trashes useless info
 Immediately spot your perf problems
 Nice graph comparison view
Blackfire collector
 Collector is a PHP extension
 ~ 5000 C lines
 Available for 5.3, 5.4, 5.5 and 5.6
 In beta for PHP 7, but soon to be released
 In beta for Windows platforms, but soon to be
released
 Collector impact is NULL if not triggered
 Collector works in prod environment
 It is highly optimized for performances
 It is finely optimized for each PHP version
Hello Hangman
Profiles
 Done on my laptop
 Done with app_dev.php
 Do not take numbers for real
 But relative measures
Which PHP ?
Which PHP ?
PHP 7
PHP 5
Which PHP ?
 PHP 7 is slower than PHP 5 ...
 When no OPCode cache is used !
 This is a 15% perf difference
 (Remember numbers target this SF2-based small
app)
PHP 5
PHP 7
PHP 7 changes
 PHP 7 now uses an AST based compiler
 The PHP 7 compiler is SLOWER than PHP 5's
 But much more well designed
 Creating and compiling an AST is slow
 The AST is hookable with PHP extensions
 The AST is hookable in userland using nikic/php-ast
 The compiler is more complex, it tries to optimize runtime
 ... But as you use an OPCode cache
 This is not a problem to you
 PHP 7 compiler generates better runtime OPCodes
 Your runtime will be better compared to PHP 5
Profiling with OPCache
PHP 7
PHP 5
Comparing view, with OPCache
 PHP 7 runs faster on this app by a factor of 23%
 PHP 7 CPU usage is 22% less than PHP 5
 PHP 7 memory footprint is 38% less than PHP 5
 ~ 3.85Mb less in our case
Comparing view, with OPCache
 Some components benefit more than others of PHP
7 performance optimizations
PHP 7 optimizations
Optimizing CPU time
 Latency Numbers Every Programmer Should Know
 http://lwn.net/Articles/250967/
 http://www.eecs.berkeley.edu/~rcs/research/interactive_l
atency.html
2016 numbers (may vary with chip)
---------------------------------------------------
L1 cache reference 1 ns
Branch mispredict 3 ns
L2 cache reference 4 ns 4x L1 cache
L3 cache reference 12 ns 3X L2 cache, 12x L1 cache
Main memory reference 100 ns 25x L2 cache, 100x L1 cache
SSD random read 16,000 ns
HDD random read(seek) 200,000,000 ns
Optimizing CPU cache efficiency
 If we can reduce payload size, the CPU will use its
caches more often
 CPU caches prefetch data on a "line" basis
 Improve data locality to improve cache efficiency
 https://software.intel.com/en-us/articles/optimize-data-
structures-and-memory-access-patterns-to-improve-
data-locality
 That means in C
 Reduce number of pointer indirections
 Stick data together (struct hacks, struct merges)
 Use smaller data sizes
Optimizing CPU cache efficiency
 If we can reduce payload size, the CPU will use its
caches more often
PHP 5.6 (debug)
456,483483 task-clock # 0,974 CPUs utilized
1 405 context-switches # 0,003 M/sec
7 CPU-migrations # 0,000 M/sec
8 633 page-faults # 0,019 M/sec
1 163 771 607 cycles # 2,549 GHz
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
1 247 617 395 instructions # 1,07 insns per cycle
181 700 375 branches # 398,044 M/sec
5 257 940 branch-misses # 2,89% of all branches
9 085 235 cache-references # 20,787 M/sec
1 108 044 cache-misses # 12,196 % of all cache refs
0,468451813 seconds time elapsed
Optimizing CPU cache efficiency
 If we can reduce payload size, the CPU will use its
caches more often
PHP 7.0.0RC8 (debug)
306,006739 task-clock # 0,916 CPUs utilized
1 446 context-switches # 0,005 M/sec
2 CPU-migrations # 0,000 M/sec
4 330 page-faults # 0,014 M/sec
787 684 146 cycles # 2,574 GHz
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
817 673 456 instructions # 1,04 insns per cycle
121 452 445 branches # 396,895 M/sec
3 356 650 branch-misses # 2,76% of all branches
5 741 559 cache-references # 18,464 M/sec
873 581 cache-misses # 15,215 % of all cache refs
0,334226815 seconds time elapsed
PHP 7 cache efficiency
PHP 7.0.0RC8 (debug)
306,006739 task-clock
1 446 context-switches
4 330 page-faults
787 684 146 cycles
817 673 456 instructions
121 452 445 branches
3 356 650 branch-misses
5 741 559 cache-references
873 581 cache-misses
0,334226815 seconds time elapsed
PHP 5.6 (debug)
456,483483 task-clock
1 405 context-switches
8 633 page-faults
1 163 771 607 cycles
1 247 617 395 instructions
181 700 375 branches
5 257 940 branch-misses
9 085 235 cache-references
1 108 044 cache-misses
0,468451813 seconds time elapsed
PHP 7 optimizations
 Every variable in PHP is coded on a zval struct
 This struct has been reorganized in PHP 7
 Narrowed / shrinked
 separated
PHP 5 variables
value
refcount is_ref
type
gc_info
dval
str_val* str_len
hashtable*
object*
lval
ast*
zval
zval_value
...
...
HashTable
32 bytes
$a
8 bytes
zval *
XX bytes
 40 bytes + complex value size
 2 indirections
PHP 7 variables
value
type
internal_int
dval
zend_string*
object*
lval
...
zval
zval_value
...
...
HashTable
16 bytes
$a
zval
XX bytes
 16 bytes + complex value size
 1 indirection
hashtable*
gc_infos
refcount
infosflags
gc_infos
PHP 5 vs PHP 7 variable design
 zval container no longer stores GC infos
 No more need to heap allocate a zval *
 GC infos stored into each complex types
 each complex type may now be shared
 In PHP 5, we had to share the zval containing them
 PHP 7 variables are more CPU cache efficient
Hashtables (arrays)
 In PHP, HashTables are used to represent the PHP
array type
 But HashTables are also used internally
 Everywhere
 HashTables optimization in PHP 7 are well felt as
they are heavilly used internally
HashTables in PHP 5
 Each element needs
 4 pointer indirections
 72 bytes for a bucket + 32 bytes for a zval
zval
zval *
HashTable
$a
zval *
HashTable*
bucket *
zval
64 bytes
72 bytesbucket
HashTables in PHP 7
 Each element needs
 2 pointer indirections
 32 bytes for a bucket
zval
bucket
HashTable
$a
zval
HashTable*
zval
56 bytes
32 bytes
bucket*
PHP 7 Hash
 Memory layout is as contiguous as possible
hash"foo" 1234 | (-table_size) -3
nIndex
PHP 7 Hash
 Memory layout is as contiguous as possible
hash"foo" 1234 | (-table_size) -3
buckets*
arData
-1-2-3
2 X XX
-4 1 2
nIndex
nIndex idx
idx
hash
key
zval
bucket
PHP 7 HashTables memory layout
 $a['foo'] = 42;
arData
-6-7
arData[0]
arData[1]
42 bucket
String management
 In PHP 5, strings don't have their own structure
 String management is hard
 Leads to many strings duplication
 Many more memory access
 In PHP 7, strings share the zend_string structure
 They are refcounted
 hashes are precomputed, often at compile time
 struct hack is used to compact memory
Strings in PHP
char * str
...
zval
gc_infos
int len
refcount is_ref zend_string *
...
zval
...
hash
gc_infos
char str[1]size_t len
...
zend_string
PHP 5 PHP 7
Strings in PHP 5
 $a = "foo";
3 0X00007FFFF7F8C708
foo0
Strings in PHP 7
 $a = "foo";
foo0
C struct hack
3
memory border
Comparing VM times for array operations
PHP 7
PHP 5
Other VM operation comparisons
 Casts
PHP 7
PHP 5
Other VM operation comparisons
 Concatenations
PHP 7
PHP 5
Other VM operation comparisons
 @ usage (error suppression)
PHP 7
PHP 5
PHP 7 is released :-)
get it now <3
Thank you for listening

More Related Content

PDF
Symfony live 2017_php7_performances
PDF
Php extensions workshop
PDF
SymfonyCon 2017 php7 performances
PDF
Profiling php5 to php7
PDF
Php7 extensions workshop
PDF
Mysqlnd, an unknown powerful PHP extension
PDF
PHP 7 new engine
PDF
When e-commerce meets Symfony
Symfony live 2017_php7_performances
Php extensions workshop
SymfonyCon 2017 php7 performances
Profiling php5 to php7
Php7 extensions workshop
Mysqlnd, an unknown powerful PHP extension
PHP 7 new engine
When e-commerce meets Symfony

What's hot (20)

PDF
Php and threads ZTS
ODP
Php in 2013 (Web-5 2013 conference)
PDF
Understanding PHP memory
PDF
Quick tour of PHP from inside
PDF
PHP 7 OPCache extension review
PPT
The Php Life Cycle
PDF
Building Custom PHP Extensions
PPT
How PHP Works ?
PDF
Understanding PHP objects
ODP
PHP5.5 is Here
PPTX
PHP 5.6 New and Deprecated Features
PDF
Php engine
PDF
PPTX
Writing and using php streams and sockets
PPTX
Php’s guts
PDF
PL/Perl - New Features in PostgreSQL 9.0
PDF
PL/Perl - New Features in PostgreSQL 9.0 201012
ODP
The why and how of moving to php 5.4
PPTX
Streams, sockets and filters oh my!
PDF
2021.laravelconf.tw.slides2
Php and threads ZTS
Php in 2013 (Web-5 2013 conference)
Understanding PHP memory
Quick tour of PHP from inside
PHP 7 OPCache extension review
The Php Life Cycle
Building Custom PHP Extensions
How PHP Works ?
Understanding PHP objects
PHP5.5 is Here
PHP 5.6 New and Deprecated Features
Php engine
Writing and using php streams and sockets
Php’s guts
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0 201012
The why and how of moving to php 5.4
Streams, sockets and filters oh my!
2021.laravelconf.tw.slides2
Ad

Viewers also liked (12)

PPTX
PHP Optimization
PPTX
Php internal architecture
PPTX
Internet of Things With PHP
PDF
PHP WTF
PDF
PHP, Under The Hood - DPC
PDF
Being functional in PHP (PHPDay Italy 2016)
PPTX
PPTX
Laravel Beginners Tutorial 1
PDF
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
KEY
Php 101: PDO
PDF
LaravelConf Taiwan 2017 開幕
PDF
Route 路由控制
PHP Optimization
Php internal architecture
Internet of Things With PHP
PHP WTF
PHP, Under The Hood - DPC
Being functional in PHP (PHPDay Italy 2016)
Laravel Beginners Tutorial 1
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
Php 101: PDO
LaravelConf Taiwan 2017 開幕
Route 路由控制
Ad

Similar to PHP 7 performances from PHP 5 (20)

KEY
Site Performance - From Pinto to Ferrari
PDF
php & performance
ODP
Caching and tuning fun for high scalability
PDF
PHP & Performance
PDF
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
PDF
MySQL Manchester TT - 5.7 Whats new
ODP
phptek13 - Caching and tuning fun tutorial
PDF
PHP7 - The New Engine for old good train
ODP
Caching and tuning fun for high scalability @ FOSDEM 2012
PPTX
Php 5.6 vs Php 7 performance comparison
PDF
Optimizing Python
PDF
Last train to php 7
PDF
Caching with Memcached and APC
PPTX
Zendcon scaling magento
PDF
Php memory-redux
PDF
Performance tuning with zend framework
PDF
Best Practices and Performance Studies for High-Performance Computing Clusters
PPTX
Sql sever engine batch mode and cpu architectures
PDF
Lightweight web frameworks
PDF
PHP 7X New Features
Site Performance - From Pinto to Ferrari
php & performance
Caching and tuning fun for high scalability
PHP & Performance
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
MySQL Manchester TT - 5.7 Whats new
phptek13 - Caching and tuning fun tutorial
PHP7 - The New Engine for old good train
Caching and tuning fun for high scalability @ FOSDEM 2012
Php 5.6 vs Php 7 performance comparison
Optimizing Python
Last train to php 7
Caching with Memcached and APC
Zendcon scaling magento
Php memory-redux
Performance tuning with zend framework
Best Practices and Performance Studies for High-Performance Computing Clusters
Sql sever engine batch mode and cpu architectures
Lightweight web frameworks
PHP 7X New Features

More from julien pauli (15)

PDF
Doctrine with Symfony - SymfonyCon 2019
PDF
PDF
PHP Internals and Virtual Machine
PDF
Basics of Cryptography - Stream ciphers and PRNG
PDF
Mastering your home network - Do It Yourself
PDF
PDF
PHP7 is coming
ODP
PHP Tips for certification - OdW13
PDF
Communications Réseaux et HTTP avec PHP
PDF
PHPTour-2011-PHP_Extensions
PDF
PHPTour 2011 - PHP5.4
ODP
Patterns and OOP in PHP
ODP
ZendFramework2 - Présentation
PDF
AlterWay SolutionsLinux Outils Industrialisation PHP
PDF
Apache for développeurs PHP
Doctrine with Symfony - SymfonyCon 2019
PHP Internals and Virtual Machine
Basics of Cryptography - Stream ciphers and PRNG
Mastering your home network - Do It Yourself
PHP7 is coming
PHP Tips for certification - OdW13
Communications Réseaux et HTTP avec PHP
PHPTour-2011-PHP_Extensions
PHPTour 2011 - PHP5.4
Patterns and OOP in PHP
ZendFramework2 - Présentation
AlterWay SolutionsLinux Outils Industrialisation PHP
Apache for développeurs PHP

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Modernizing your data center with Dell and AMD
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
Teaching material agriculture food technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
KodekX | Application Modernization Development
PDF
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
Modernizing your data center with Dell and AMD
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Teaching material agriculture food technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Monthly Chronicles - July 2025
Spectral efficient network and resource selection model in 5G networks
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
KodekX | Application Modernization Development
cuic standard and advanced reporting.pdf

PHP 7 performances from PHP 5

  • 1. PHP 7 performances From PHP 5 in Symfony context
  • 2. Hello everybody  Julien PAULI  SensioLabs Blackfire team  Programming with PHP since early 2000s  Now : Unix system programmer (C)  PHP Internals programmer/reviewer  PHP 5.5 & 5.6 Release Manager  @julienpauli  Tech blog at jpauli.github.io  jpauli@php.net
  • 3. What we'll cover together  Profiling a simple SF2 app  Under PHP 5  Under PHP 7  Compare profiles using Blackfire graph comparison  Analyze numbers  Dive into PHP 7 performances  Structures optimizations  New variable model (zval)  New HashTable model  String management with zend_string  Other ideas...
  • 4. Profiles  Done on my laptop (not on prod env)  LP64  Done on app_dev.php (debug mode)  Do not take numbers for real  But relative measures  Performed with Blackfire  On PHP-5.6 latest  On PHP-7.0.0RC8
  • 5. Blackfire  General profiler  Not only PHP, but works best for PHP  Free version exists  Collects many metrics  memory, CPU, IO, Network trafic, SQL ...  Graphs useful info, trashes useless info  Immediately spot your perf problems  Nice graph comparison view
  • 6. Blackfire collector  Collector is a PHP extension  ~ 5000 C lines  Available for 5.3, 5.4, 5.5 and 5.6  In beta for PHP 7, but soon to be released  In beta for Windows platforms, but soon to be released  Collector impact is NULL if not triggered  Collector works in prod environment  It is highly optimized for performances  It is finely optimized for each PHP version
  • 8. Profiles  Done on my laptop  Done with app_dev.php  Do not take numbers for real  But relative measures
  • 10. Which PHP ? PHP 7 PHP 5
  • 11. Which PHP ?  PHP 7 is slower than PHP 5 ...  When no OPCode cache is used !  This is a 15% perf difference  (Remember numbers target this SF2-based small app) PHP 5 PHP 7
  • 12. PHP 7 changes  PHP 7 now uses an AST based compiler  The PHP 7 compiler is SLOWER than PHP 5's  But much more well designed  Creating and compiling an AST is slow  The AST is hookable with PHP extensions  The AST is hookable in userland using nikic/php-ast  The compiler is more complex, it tries to optimize runtime  ... But as you use an OPCode cache  This is not a problem to you  PHP 7 compiler generates better runtime OPCodes  Your runtime will be better compared to PHP 5
  • 14. Comparing view, with OPCache  PHP 7 runs faster on this app by a factor of 23%  PHP 7 CPU usage is 22% less than PHP 5  PHP 7 memory footprint is 38% less than PHP 5  ~ 3.85Mb less in our case
  • 15. Comparing view, with OPCache  Some components benefit more than others of PHP 7 performance optimizations
  • 17. Optimizing CPU time  Latency Numbers Every Programmer Should Know  http://lwn.net/Articles/250967/  http://www.eecs.berkeley.edu/~rcs/research/interactive_l atency.html 2016 numbers (may vary with chip) --------------------------------------------------- L1 cache reference 1 ns Branch mispredict 3 ns L2 cache reference 4 ns 4x L1 cache L3 cache reference 12 ns 3X L2 cache, 12x L1 cache Main memory reference 100 ns 25x L2 cache, 100x L1 cache SSD random read 16,000 ns HDD random read(seek) 200,000,000 ns
  • 18. Optimizing CPU cache efficiency  If we can reduce payload size, the CPU will use its caches more often  CPU caches prefetch data on a "line" basis  Improve data locality to improve cache efficiency  https://software.intel.com/en-us/articles/optimize-data- structures-and-memory-access-patterns-to-improve- data-locality  That means in C  Reduce number of pointer indirections  Stick data together (struct hacks, struct merges)  Use smaller data sizes
  • 19. Optimizing CPU cache efficiency  If we can reduce payload size, the CPU will use its caches more often PHP 5.6 (debug) 456,483483 task-clock # 0,974 CPUs utilized 1 405 context-switches # 0,003 M/sec 7 CPU-migrations # 0,000 M/sec 8 633 page-faults # 0,019 M/sec 1 163 771 607 cycles # 2,549 GHz <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend 1 247 617 395 instructions # 1,07 insns per cycle 181 700 375 branches # 398,044 M/sec 5 257 940 branch-misses # 2,89% of all branches 9 085 235 cache-references # 20,787 M/sec 1 108 044 cache-misses # 12,196 % of all cache refs 0,468451813 seconds time elapsed
  • 20. Optimizing CPU cache efficiency  If we can reduce payload size, the CPU will use its caches more often PHP 7.0.0RC8 (debug) 306,006739 task-clock # 0,916 CPUs utilized 1 446 context-switches # 0,005 M/sec 2 CPU-migrations # 0,000 M/sec 4 330 page-faults # 0,014 M/sec 787 684 146 cycles # 2,574 GHz <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend 817 673 456 instructions # 1,04 insns per cycle 121 452 445 branches # 396,895 M/sec 3 356 650 branch-misses # 2,76% of all branches 5 741 559 cache-references # 18,464 M/sec 873 581 cache-misses # 15,215 % of all cache refs 0,334226815 seconds time elapsed
  • 21. PHP 7 cache efficiency PHP 7.0.0RC8 (debug) 306,006739 task-clock 1 446 context-switches 4 330 page-faults 787 684 146 cycles 817 673 456 instructions 121 452 445 branches 3 356 650 branch-misses 5 741 559 cache-references 873 581 cache-misses 0,334226815 seconds time elapsed PHP 5.6 (debug) 456,483483 task-clock 1 405 context-switches 8 633 page-faults 1 163 771 607 cycles 1 247 617 395 instructions 181 700 375 branches 5 257 940 branch-misses 9 085 235 cache-references 1 108 044 cache-misses 0,468451813 seconds time elapsed
  • 22. PHP 7 optimizations  Every variable in PHP is coded on a zval struct  This struct has been reorganized in PHP 7  Narrowed / shrinked  separated
  • 23. PHP 5 variables value refcount is_ref type gc_info dval str_val* str_len hashtable* object* lval ast* zval zval_value ... ... HashTable 32 bytes $a 8 bytes zval * XX bytes  40 bytes + complex value size  2 indirections
  • 24. PHP 7 variables value type internal_int dval zend_string* object* lval ... zval zval_value ... ... HashTable 16 bytes $a zval XX bytes  16 bytes + complex value size  1 indirection hashtable* gc_infos refcount infosflags gc_infos
  • 25. PHP 5 vs PHP 7 variable design  zval container no longer stores GC infos  No more need to heap allocate a zval *  GC infos stored into each complex types  each complex type may now be shared  In PHP 5, we had to share the zval containing them  PHP 7 variables are more CPU cache efficient
  • 26. Hashtables (arrays)  In PHP, HashTables are used to represent the PHP array type  But HashTables are also used internally  Everywhere  HashTables optimization in PHP 7 are well felt as they are heavilly used internally
  • 27. HashTables in PHP 5  Each element needs  4 pointer indirections  72 bytes for a bucket + 32 bytes for a zval zval zval * HashTable $a zval * HashTable* bucket * zval 64 bytes 72 bytesbucket
  • 28. HashTables in PHP 7  Each element needs  2 pointer indirections  32 bytes for a bucket zval bucket HashTable $a zval HashTable* zval 56 bytes 32 bytes bucket*
  • 29. PHP 7 Hash  Memory layout is as contiguous as possible hash"foo" 1234 | (-table_size) -3 nIndex
  • 30. PHP 7 Hash  Memory layout is as contiguous as possible hash"foo" 1234 | (-table_size) -3 buckets* arData -1-2-3 2 X XX -4 1 2 nIndex nIndex idx idx hash key zval bucket
  • 31. PHP 7 HashTables memory layout  $a['foo'] = 42; arData -6-7 arData[0] arData[1] 42 bucket
  • 32. String management  In PHP 5, strings don't have their own structure  String management is hard  Leads to many strings duplication  Many more memory access  In PHP 7, strings share the zend_string structure  They are refcounted  hashes are precomputed, often at compile time  struct hack is used to compact memory
  • 33. Strings in PHP char * str ... zval gc_infos int len refcount is_ref zend_string * ... zval ... hash gc_infos char str[1]size_t len ... zend_string PHP 5 PHP 7
  • 34. Strings in PHP 5  $a = "foo"; 3 0X00007FFFF7F8C708 foo0
  • 35. Strings in PHP 7  $a = "foo"; foo0 C struct hack 3 memory border
  • 36. Comparing VM times for array operations PHP 7 PHP 5
  • 37. Other VM operation comparisons  Casts PHP 7 PHP 5
  • 38. Other VM operation comparisons  Concatenations PHP 7 PHP 5
  • 39. Other VM operation comparisons  @ usage (error suppression) PHP 7 PHP 5
  • 40. PHP 7 is released :-) get it now <3
  • 41. Thank you for listening