SlideShare uma empresa Scribd logo
1 de 41
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

Mais conteúdo relacionado

Mais procurados

Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memory
julien pauli
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
Xinchen Hui
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
julien pauli
 

Mais procurados (20)

Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memory
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
PHP 7 OPCache extension review
PHP 7 OPCache extension reviewPHP 7 OPCache extension review
PHP 7 OPCache extension review
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
PHP 5.6 New and Deprecated Features
PHP 5.6  New and Deprecated FeaturesPHP 5.6  New and Deprecated Features
PHP 5.6 New and Deprecated Features
 
Php engine
Php enginePhp engine
Php engine
 
How PHP works
How PHP works How PHP works
How PHP works
 
Writing and using php streams and sockets
Writing and using php streams and socketsWriting and using php streams and sockets
Writing and using php streams and sockets
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
 
PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 

Destaque

Destaque (12)

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

Semelhante a PHP 7 performances from PHP 5

Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
Joseph Scott
 
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Fwdays
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
Xinchen Hui
 

Semelhante a PHP 7 performances from PHP 5 (20)

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

Mais de julien pauli

PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
julien pauli
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHP
julien pauli
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensions
julien pauli
 
PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4
julien pauli
 
Patterns and OOP in PHP
Patterns and OOP in PHPPatterns and OOP in PHP
Patterns and OOP in PHP
julien pauli
 

Mais de julien pauli (15)

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Dns
DnsDns
Dns
 
PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNG
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourself
 
Tcpip
TcpipTcpip
Tcpip
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHP
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensions
 
PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4
 
Patterns and OOP in PHP
Patterns and OOP in PHPPatterns and OOP in PHP
Patterns and OOP in PHP
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
 
AlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHPAlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHP
 
Apache for développeurs PHP
Apache for développeurs PHPApache for développeurs PHP
Apache for développeurs PHP
 

Último

Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 

Último (20)

Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 

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