SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
PHP 7
- New Engine For The Good Old Train
@laruence
About Me
•  Author of Yaf, Yar, Yac, Taint, Lua, etc
•  Maintainer of APC, Zend Opcache, Msgpack, etc
•  Chief software architect At Weibo since 2012
•  PHP core developer since 2011
•  Zend consultant since 2013
•  Core author of PHP7
About PHP
•  20 years history
•  Most popular Web service program language
•  Over 82% sites are use PHP as server program language
PHP 7 New Features
PHP 7?
•  PHP NG – Engine Refactor - performance improvements
•  Abstarct Syntanx Tree
•  Int64 Improvement
•  Uniform variable syntax
•  Native TLS
•  Consistently foreach behaviors
•  New <=>, **, ?? operaters
•  Return Type Declarations
•  Scalar Type Declarations
•  Exceptions in Engine
•  And Dozens features…
Abstract Syntax Tree
PHP	
 Parser	
 AST	
 Opcodes	
 Execution	
PHP	
 Parser	
 Opcodes	
 Execution	
•  PHP5
•  PHP7
Int64 Improvement
•  >2GB string
•  >2GB file uploading
•  Fully 64bits intgers cross platforms
Uniform Variables Syntanx
•  $foo()['bar']()
•  $foo['bar']::$baz
•  foo()() – (foo())()
•  $foo->bar()::baz()
•  (function() { ... })()
•  $this->{$name}()
•  [$obj, 'method']()
•  Foo::$bar['baz']()
PHP5: Foo::{$bar['baz']}()
PHP7: (Foo::$bar)['baz']()
Return Type Declarations
•  function foo(): array {
return [];
}
•  interface A {
static function make(): A;
}
•  function foo(): DateTime {
return null;
}
PHP Fatal error: Return value of foo() must be an instance of DateTime, null returned
Scalar Type Declarations
•  function foo(int num)
•  function bar (string name)
•  function foobar() : float {}
•  function add(int l, int r) : int {}
•  class A {
public function start (bool start) {}
}
Exceptions in Engine
•  Use of exceptions in Engine
try {
non_exists_func();
} catch (EngineException $e) {
echo "Exception: {$e->getMessage()}n";
}
 
Exception: Call to undefined function non_exists_func()
•  Uncaught Exception result to FATAL ERROR
non_exists_func();
PHP Fatal error: Call to undefined function non_exists()
PHP NG (Next Generation)
PHP Performance Evaluation
2.991
3.045
3.102
3.762
5.318
6.527
13.424
0 2 4 6 8 10 12 14 16
PHP-5.6
PHP-5.5
PHP-5.4
PHP-5.3
PHP-5.2
PHP-5.1
PHP-5.0
bench.php(sec)
PHP Performance Evaluation
112
110
107
82
75
0
0
0 20 40 60 80 100 120
PHP-5.6
PHP-5.5
PHP-5.4
PHP-5.3
PHP-5.2
PHP-5.1
PHP-5.0
Wordpress 3.6 home page qps
PHP Performance Evaluation
•  ~5 times faster from 5.0 to 5.6 in bench
•  ~2 times faster from 5.0 to 5.6 in real-life apps
•  No big performance improvement after 5.4
•  Zend VM is already highly optimized
•  Generate optimized codes based on run-time types inference
PHP Just In Time Compiler?
PHP	
 Parser	
 Opcodes	
TypeInf	
 JIT	
 Bytecodes
Execution
PHP Just In Time Compiler?
•  We created a POC of JIT compiler based on LLVM for PHP-5.5 in 2013
•  ~8 times speedup on bench.php
•  Negligible speedup on real-life apps (1% on Wordpress)
•  https://github.com/zendtech/php-src/tree/zend-jit
Wordpress profile
•  21% CPU time in meory manager
•  12% CPU time in hash tables operations
•  30% CPU time in internal functions
•  25% CPU time in VM
Wordpress profile
New Generation
•  It’s a refactoring
•  Main goal — achieve new performance level and make base for future
improvements
•  No new features for users (only internals)
•  Keep 100% compatibility in PHP behavior
•  May 2014 we opened the project
ZVAL
•  Zval is changed
ZVAL
•  No refcount for scalar types
•  zvals are always pre-allcocated or allocated in stack(nomore
MAKE_STD_ZVAL and ALLOC_ZVAL)
•  String using refcout instead of copy (zend_string)
•  gc_info, temporary_variables, should_free_var, cache_slot all in zval
•  New types: IS_TRUE, IS_FALSE, IS_REFERENCE, IS_INDIRECT
Zend VM
execute data
Internal Functions
zval zval zval zval zval
Uesr land
zend array
zval zval zval zval zval
stack zvals
zval zval zval zval zval
•  IS_UNDEF
•  IS_NULL
•  IS_FALSE
•  IS_TRUE
•  IS_LONG
•  IS_DOUBLE
•  IS_STRING
•  IS_ARRAY
•  IS_OBJECT
•  IS_RESOURCE
•  IS_REFERENCE
ZVAL
v28u4
tAp4 u2f826s
0 8 32 63
IS_S.RING/IS_ARRA0/IS_OBJEC./IS_RESRO/CE/...
r4f3ount 63_infotAp4
0 32 63
f826s
48
•  IS_NULL
•  IS_FALSE
•  IS_TRUE
•  IS_LONG
•  IS_DOUBLE
ZVAL NON REFCOUNED
value
type u2flags
0 8 32 63
•  IS_STRING
•  IS_ARRAY
•  IS_OBJECT
•  IS_RESOURCE
•  IS_REFERENCE
ZVAL REFCOUNED
IS_S.RING/IS_ARRA0/IS_OBJEC./IS_RESRO/CE/...
r4f3ount 63_infotyp4
0 32 63
f826s
48
•  New Internal Type: Zend String
IS_STRING
•  IS_STRING_PERSISTENT
•  IS_STR_INTERNED
•  IS_STR_PERMANENT
•  IS_STR_CONSTANT
un3igned l0ng ha3h_6alue
2efc0un4 gc_inf04y1e
0 32 63
flag3
48
3i8e_4 342ing_len
cha2[  ] 6al
•  New Internal Type: Zend Array
IS_ARRAY
•  IS_ARRAY_IMMUTABLE
refcount gc_infotype
0 32 63
flags
48
nTableMasku
Bucket *arData
nNumUsed nNumOfElements
.....
IS_OBJECT
•  Zend Object
•  IS_OBJ_APPLY_COUNT
•  IS_OBJ_DESTRUCTOR_CALLED
•  IS_OBJ_FREE_CALLED
zend_class_entry *ce
refcount gc_infotype
0 32 63
flags
48
zend_object_handlers *handlers
zend_array *properties
zend_array *guarders
zval *properties_table[1]
•  New Internal Type: Zend Reference
•  Reference is type
IS_REFERENCE
refco0nt gc_infot2pe
0 3   63
flags
48
3val val0e
HashTable – PHP 5
HashTable
nTableMasknTableSize
0 8 32 63
pInternalPointer
nNumOfElem
nNextFreeElement
Bucket *
pListHead
pListTail
arBuckets
pDestructor
Bucket *
Bucket *
.......
Bucket
hashval
nKeyLength
pDataPtr
pData
pListNext
pListPrev
pNext
pLast
arKey
Bucket
hashval
nKeyLength
pDataPtr
pData
pListNext
pListPrev
pNext
pLast
arKey
Zval
value
type
Zend Array – PHP 7
zend_array
refcounted
0 8 32 63
u
arData
idx
idx
Bucket 0
nTableMask
nNumUsed nNumOfElem
nTableSize InternalPointer
pNextFreeElement
pDestructor
.......
hashval
key
zval
Bucket 1
hashval
key
zval
Bucket 2 ......
Zend Array(HashTable)
•  Values of arrays are zval by default
•  HashTable size reduced from 72 to 56 bytes
•  Bucket size reduced from 72 to 32 bytes
•  Memory for all Buckets is allocated at once
•  Bucket.key now is a pointer to zend_string
•  Values of array elements are embedded into the Buckets
•  Improved data locality => less CPU cache misses
Function calling convention – PHP5
•  function add ($a, $b) {
return $a + $b;
}
add(1, 2);
send_val 1
send_val 2
do_fcall add (2)
1
2
call frame(add)
opcodes(main):
call frame(main)
recv 1 $a
recv 2 $b
zend_add $a, $b ~0
opcodes(add):
zend_return ~0
1
2
3
vm stack
Function calling convention – PHP7
•  function add ($a, $b) {
return $a + $b;
}
add(1, 2);
send_val 1
send_val 2
do_fcall 2
call frame(add)
init_fcall add
call frame(main)
recv 1 $a
recv 2 $b
zend_add $a, $b ~0
opcodes(add):
zend_return ~0
1
2
3
vm stack
Fast Parameters Parsing APIs
•  ~5% of the CPU time is spent in zend_parse_parameters()
•  For some simple functions the overhead of zend_parse_parameters() is
over 90%
Inline Frequently used simple functions
•  call_user_function(_array) => ZEND_INIT_USER_CALL
•  is_int/string/array/* etc => ZEND_TYPE_CHECK
•  strlen => ZEND_STRLEN
•  defined => ZEND+DEFINED
•  …
Faster zend_qsort
•  Refactor zend_qsort for better performance
•  Hybrid Sorting Algo(Quick Sort and Selection Sort)
•  <16 elements do stable sorting
•  $array = array(0 => 0, 1=>0); asort($array);
•  PHP5: array(1=>0, 0=>0);
•  PHP7: array(0=>1, 1=>0);
New Memory Manager
•  Friendly to moder CPU cache
•  less CPU cache misses
•  Faster builtin types allocating
•  ~5% CPU time reduce in wordpress homepage
Dozens of other improvements
•  Fast HashTable iteration APIs
•  Immutable array
•  Array duplication optimization
•  PCRE with JIT
•  BIND_GLOBAL instead of FETCH and ASSIGN_REF
•  More specifical DO_UCALL and DO_ICALL
•  Global registers for execute_data and opline(GCC-4.8+)
•  ZEND_ROPE_* for faster string concating
•  ZEND_CALL_TRAMPOLINE for faster__call/__callstatic
•  Dozens logic optimizations
•  ….
Wordpress profile (2015-04-14)
Wordpress profiled (2015-04-14)
•  >50% CPU IRs reduced
•  5% CPU time in meory manager
•  12% CPU time in hash tables operations
PHP7 Performance – Benchmark (2014-04-14)
1.186
2.991
3.045
3.102
3.762
5.318
6.527
13.424
0 2 4 6 8 10 12 14 16
PHP-7
PHP-5.6
PHP-5.5
PHP-5.4
PHP-5.3
PHP-5.2
PHP-5.1
PHP-5.0
bench.php(sec)
PHP7 Performance – Reallife App (2015-04-14)
311
112
110
107
82
75
0
0
0 50 100 150 200 250 300 350
PHP-7
PHP-5.6
PHP-5.5
PHP-5.4
PHP-5.3
PHP-5.2
PHP-5.1
PHP-5.0
wordpress 3.0.1 home page qps
PHP7 Performance (By Rasmus 2015-04-21)
PHP7 Performance (2015-04-21)
PHP7 Performance (2015-04-21)
PHP7 Performance (2015-03-15)
Always Do Your Own Benchmark
PHP 7 Next
•  Keep 99.99% compatible with PHP5
•  Keep Improving performance
•  Port common used PECL extensions (memcached, redis etc)
•  Release PHP 7 (oct 2015)
•  Restart JIT (Sep 2014)
Links
•  phpng:_Refactored_PHP_Engine_with_Big_Performance_Improveme
nt: http://news.php.net/php.internals/73888
•  PHPNG RFC: https://wiki.php.net/phpng
•  PHPNG Implementation details: https://wiki.php.net/phpng-int
•  Upgrading PHP extensions from PHP5 to PHPNG:
https://wiki.php.net/phpng-upgrading
•  Zeev <Benchmarking PHPNG>:
http://zsuraski.blogspot.co.il/2014/07/benchmarking-phpng.html
•  Rasmus <SPEEDING UP THE WEB WITH PHP 7>:
http://talks.php.net/fluent15#/
Questions?
Thanks

Más contenido relacionado

La actualidad más candente

Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Muhamad Al Imran
 

La actualidad más candente (20)

Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architecture
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 
PSR-7 - Middleware - Zend Expressive
PSR-7 - Middleware - Zend ExpressivePSR-7 - Middleware - Zend Expressive
PSR-7 - Middleware - Zend Expressive
 
Zend expressive workshop
Zend expressive workshopZend expressive workshop
Zend expressive workshop
 
Doctrine 2.0 Enterprise Persistence Layer for PHP
Doctrine 2.0 Enterprise Persistence Layer for PHPDoctrine 2.0 Enterprise Persistence Layer for PHP
Doctrine 2.0 Enterprise Persistence Layer for PHP
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
Stacking Up Middleware
Stacking Up MiddlewareStacking Up Middleware
Stacking Up Middleware
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
 
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
 
Last train to php 7
Last train to php 7Last train to php 7
Last train to php 7
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 
30 Minutes To CPAN
30 Minutes To CPAN30 Minutes To CPAN
30 Minutes To CPAN
 
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
 

Destacado

Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...
Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...
Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...
Badoo Development
 
Weaving aspects in PHP with the help of Go! AOP library
Weaving aspects in PHP with the help of Go! AOP libraryWeaving aspects in PHP with the help of Go! AOP library
Weaving aspects in PHP with the help of Go! AOP library
Alexander Lisachenko
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
 

Destacado (20)

PHP7.1 New Features & Performance
PHP7.1 New Features & PerformancePHP7.1 New Features & Performance
PHP7.1 New Features & Performance
 
給你一個使用 Laravel 的理由
給你一個使用 Laravel 的理由給你一個使用 Laravel 的理由
給你一個使用 Laravel 的理由
 
errors in php 7
errors in php 7errors in php 7
errors in php 7
 
PHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB ServerPHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB Server
 
關聯式資料庫系統的規劃
關聯式資料庫系統的規劃關聯式資料庫系統的規劃
關聯式資料庫系統的規劃
 
微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)微博Lamp性能优化之路(2014)
微博Lamp性能优化之路(2014)
 
JSON-lD
JSON-lDJSON-lD
JSON-lD
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Mitos do PHP
Mitos do PHPMitos do PHP
Mitos do PHP
 
PHP tutorial | ptutorial
PHP tutorial | ptutorialPHP tutorial | ptutorial
PHP tutorial | ptutorial
 
Weibo lamp improvements
Weibo lamp improvementsWeibo lamp improvements
Weibo lamp improvements
 
HTML CSS JavaScript 行動應用程式解決方案
HTML CSS JavaScript 行動應用程式解決方案HTML CSS JavaScript 行動應用程式解決方案
HTML CSS JavaScript 行動應用程式解決方案
 
PHP7 - For Its Best Performance
PHP7 - For Its Best PerformancePHP7 - For Its Best Performance
PHP7 - For Its Best Performance
 
PHP 7 – What changed internally?
PHP 7 – What changed internally?PHP 7 – What changed internally?
PHP 7 – What changed internally?
 
Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...
Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...
Badoo в облаках. Решение для запуска cli-скриптов в облаке собственной разраб...
 
PHP 7
PHP 7PHP 7
PHP 7
 
PHP 語法基礎與物件導向
PHP 語法基礎與物件導向PHP 語法基礎與物件導向
PHP 語法基礎與物件導向
 
Weaving aspects in PHP with the help of Go! AOP library
Weaving aspects in PHP with the help of Go! AOP libraryWeaving aspects in PHP with the help of Go! AOP library
Weaving aspects in PHP with the help of Go! AOP library
 
工作坊簡介
工作坊簡介工作坊簡介
工作坊簡介
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 

Similar a PHP7 - The New Engine for old good train

これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
goccy
 
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
Nexcess.net LLC
 

Similar a PHP7 - The New Engine for old good train (20)

PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
 
Top ten-list
Top ten-listTop ten-list
Top ten-list
 
Ipc mysql php
Ipc mysql php Ipc mysql php
Ipc mysql php
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress Applications
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourWAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend framework
 
Automating your php infrastructure with the zend server api
Automating your php infrastructure with the zend server apiAutomating your php infrastructure with the zend server api
Automating your php infrastructure with the zend server api
 
Exploring the replication in MongoDB
Exploring the replication in MongoDBExploring the replication in MongoDB
Exploring the replication in MongoDB
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
 
Damien seguy php 5.6
Damien seguy php 5.6Damien seguy php 5.6
Damien seguy php 5.6
 
Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
Review unknown code with static analysis Zend con 2017
Review unknown code with static analysis  Zend con 2017Review unknown code with static analysis  Zend con 2017
Review unknown code with static analysis Zend con 2017
 
Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...
Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...
Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...
 
They why behind php frameworks
They why behind php frameworksThey why behind php frameworks
They why behind php frameworks
 
High Performance Rails with MySQL
High Performance Rails with MySQLHigh Performance Rails with MySQL
High Performance Rails with MySQL
 
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
 
Continuous feature-development
Continuous feature-developmentContinuous feature-development
Continuous feature-development
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

PHP7 - The New Engine for old good train

  • 1. PHP 7 - New Engine For The Good Old Train @laruence
  • 2. About Me •  Author of Yaf, Yar, Yac, Taint, Lua, etc •  Maintainer of APC, Zend Opcache, Msgpack, etc •  Chief software architect At Weibo since 2012 •  PHP core developer since 2011 •  Zend consultant since 2013 •  Core author of PHP7
  • 3. About PHP •  20 years history •  Most popular Web service program language •  Over 82% sites are use PHP as server program language
  • 4. PHP 7 New Features
  • 5. PHP 7? •  PHP NG – Engine Refactor - performance improvements •  Abstarct Syntanx Tree •  Int64 Improvement •  Uniform variable syntax •  Native TLS •  Consistently foreach behaviors •  New <=>, **, ?? operaters •  Return Type Declarations •  Scalar Type Declarations •  Exceptions in Engine •  And Dozens features…
  • 6. Abstract Syntax Tree PHP Parser AST Opcodes Execution PHP Parser Opcodes Execution •  PHP5 •  PHP7
  • 7. Int64 Improvement •  >2GB string •  >2GB file uploading •  Fully 64bits intgers cross platforms
  • 8. Uniform Variables Syntanx •  $foo()['bar']() •  $foo['bar']::$baz •  foo()() – (foo())() •  $foo->bar()::baz() •  (function() { ... })() •  $this->{$name}() •  [$obj, 'method']() •  Foo::$bar['baz']() PHP5: Foo::{$bar['baz']}() PHP7: (Foo::$bar)['baz']()
  • 9. Return Type Declarations •  function foo(): array { return []; } •  interface A { static function make(): A; } •  function foo(): DateTime { return null; } PHP Fatal error: Return value of foo() must be an instance of DateTime, null returned
  • 10. Scalar Type Declarations •  function foo(int num) •  function bar (string name) •  function foobar() : float {} •  function add(int l, int r) : int {} •  class A { public function start (bool start) {} }
  • 11. Exceptions in Engine •  Use of exceptions in Engine try { non_exists_func(); } catch (EngineException $e) { echo "Exception: {$e->getMessage()}n"; }   Exception: Call to undefined function non_exists_func() •  Uncaught Exception result to FATAL ERROR non_exists_func(); PHP Fatal error: Call to undefined function non_exists()
  • 12. PHP NG (Next Generation)
  • 13. PHP Performance Evaluation 2.991 3.045 3.102 3.762 5.318 6.527 13.424 0 2 4 6 8 10 12 14 16 PHP-5.6 PHP-5.5 PHP-5.4 PHP-5.3 PHP-5.2 PHP-5.1 PHP-5.0 bench.php(sec)
  • 14. PHP Performance Evaluation 112 110 107 82 75 0 0 0 20 40 60 80 100 120 PHP-5.6 PHP-5.5 PHP-5.4 PHP-5.3 PHP-5.2 PHP-5.1 PHP-5.0 Wordpress 3.6 home page qps
  • 15. PHP Performance Evaluation •  ~5 times faster from 5.0 to 5.6 in bench •  ~2 times faster from 5.0 to 5.6 in real-life apps •  No big performance improvement after 5.4 •  Zend VM is already highly optimized
  • 16. •  Generate optimized codes based on run-time types inference PHP Just In Time Compiler? PHP Parser Opcodes TypeInf JIT Bytecodes Execution
  • 17. PHP Just In Time Compiler? •  We created a POC of JIT compiler based on LLVM for PHP-5.5 in 2013 •  ~8 times speedup on bench.php •  Negligible speedup on real-life apps (1% on Wordpress) •  https://github.com/zendtech/php-src/tree/zend-jit
  • 19. •  21% CPU time in meory manager •  12% CPU time in hash tables operations •  30% CPU time in internal functions •  25% CPU time in VM Wordpress profile
  • 20. New Generation •  It’s a refactoring •  Main goal — achieve new performance level and make base for future improvements •  No new features for users (only internals) •  Keep 100% compatibility in PHP behavior •  May 2014 we opened the project
  • 22. ZVAL •  No refcount for scalar types •  zvals are always pre-allcocated or allocated in stack(nomore MAKE_STD_ZVAL and ALLOC_ZVAL) •  String using refcout instead of copy (zend_string) •  gc_info, temporary_variables, should_free_var, cache_slot all in zval •  New types: IS_TRUE, IS_FALSE, IS_REFERENCE, IS_INDIRECT Zend VM execute data Internal Functions zval zval zval zval zval Uesr land zend array zval zval zval zval zval stack zvals zval zval zval zval zval
  • 23. •  IS_UNDEF •  IS_NULL •  IS_FALSE •  IS_TRUE •  IS_LONG •  IS_DOUBLE •  IS_STRING •  IS_ARRAY •  IS_OBJECT •  IS_RESOURCE •  IS_REFERENCE ZVAL v28u4 tAp4 u2f826s 0 8 32 63 IS_S.RING/IS_ARRA0/IS_OBJEC./IS_RESRO/CE/... r4f3ount 63_infotAp4 0 32 63 f826s 48
  • 24. •  IS_NULL •  IS_FALSE •  IS_TRUE •  IS_LONG •  IS_DOUBLE ZVAL NON REFCOUNED value type u2flags 0 8 32 63
  • 25. •  IS_STRING •  IS_ARRAY •  IS_OBJECT •  IS_RESOURCE •  IS_REFERENCE ZVAL REFCOUNED IS_S.RING/IS_ARRA0/IS_OBJEC./IS_RESRO/CE/... r4f3ount 63_infotyp4 0 32 63 f826s 48
  • 26. •  New Internal Type: Zend String IS_STRING •  IS_STRING_PERSISTENT •  IS_STR_INTERNED •  IS_STR_PERMANENT •  IS_STR_CONSTANT un3igned l0ng ha3h_6alue 2efc0un4 gc_inf04y1e 0 32 63 flag3 48 3i8e_4 342ing_len cha2[  ] 6al
  • 27. •  New Internal Type: Zend Array IS_ARRAY •  IS_ARRAY_IMMUTABLE refcount gc_infotype 0 32 63 flags 48 nTableMasku Bucket *arData nNumUsed nNumOfElements .....
  • 28. IS_OBJECT •  Zend Object •  IS_OBJ_APPLY_COUNT •  IS_OBJ_DESTRUCTOR_CALLED •  IS_OBJ_FREE_CALLED zend_class_entry *ce refcount gc_infotype 0 32 63 flags 48 zend_object_handlers *handlers zend_array *properties zend_array *guarders zval *properties_table[1]
  • 29. •  New Internal Type: Zend Reference •  Reference is type IS_REFERENCE refco0nt gc_infot2pe 0 3   63 flags 48 3val val0e
  • 30. HashTable – PHP 5 HashTable nTableMasknTableSize 0 8 32 63 pInternalPointer nNumOfElem nNextFreeElement Bucket * pListHead pListTail arBuckets pDestructor Bucket * Bucket * ....... Bucket hashval nKeyLength pDataPtr pData pListNext pListPrev pNext pLast arKey Bucket hashval nKeyLength pDataPtr pData pListNext pListPrev pNext pLast arKey Zval value type
  • 31. Zend Array – PHP 7 zend_array refcounted 0 8 32 63 u arData idx idx Bucket 0 nTableMask nNumUsed nNumOfElem nTableSize InternalPointer pNextFreeElement pDestructor ....... hashval key zval Bucket 1 hashval key zval Bucket 2 ......
  • 32. Zend Array(HashTable) •  Values of arrays are zval by default •  HashTable size reduced from 72 to 56 bytes •  Bucket size reduced from 72 to 32 bytes •  Memory for all Buckets is allocated at once •  Bucket.key now is a pointer to zend_string •  Values of array elements are embedded into the Buckets •  Improved data locality => less CPU cache misses
  • 33. Function calling convention – PHP5 •  function add ($a, $b) { return $a + $b; } add(1, 2); send_val 1 send_val 2 do_fcall add (2) 1 2 call frame(add) opcodes(main): call frame(main) recv 1 $a recv 2 $b zend_add $a, $b ~0 opcodes(add): zend_return ~0 1 2 3 vm stack
  • 34. Function calling convention – PHP7 •  function add ($a, $b) { return $a + $b; } add(1, 2); send_val 1 send_val 2 do_fcall 2 call frame(add) init_fcall add call frame(main) recv 1 $a recv 2 $b zend_add $a, $b ~0 opcodes(add): zend_return ~0 1 2 3 vm stack
  • 35. Fast Parameters Parsing APIs •  ~5% of the CPU time is spent in zend_parse_parameters() •  For some simple functions the overhead of zend_parse_parameters() is over 90%
  • 36. Inline Frequently used simple functions •  call_user_function(_array) => ZEND_INIT_USER_CALL •  is_int/string/array/* etc => ZEND_TYPE_CHECK •  strlen => ZEND_STRLEN •  defined => ZEND+DEFINED •  …
  • 37. Faster zend_qsort •  Refactor zend_qsort for better performance •  Hybrid Sorting Algo(Quick Sort and Selection Sort) •  <16 elements do stable sorting •  $array = array(0 => 0, 1=>0); asort($array); •  PHP5: array(1=>0, 0=>0); •  PHP7: array(0=>1, 1=>0);
  • 38. New Memory Manager •  Friendly to moder CPU cache •  less CPU cache misses •  Faster builtin types allocating •  ~5% CPU time reduce in wordpress homepage
  • 39. Dozens of other improvements •  Fast HashTable iteration APIs •  Immutable array •  Array duplication optimization •  PCRE with JIT •  BIND_GLOBAL instead of FETCH and ASSIGN_REF •  More specifical DO_UCALL and DO_ICALL •  Global registers for execute_data and opline(GCC-4.8+) •  ZEND_ROPE_* for faster string concating •  ZEND_CALL_TRAMPOLINE for faster__call/__callstatic •  Dozens logic optimizations •  ….
  • 41. Wordpress profiled (2015-04-14) •  >50% CPU IRs reduced •  5% CPU time in meory manager •  12% CPU time in hash tables operations
  • 42. PHP7 Performance – Benchmark (2014-04-14) 1.186 2.991 3.045 3.102 3.762 5.318 6.527 13.424 0 2 4 6 8 10 12 14 16 PHP-7 PHP-5.6 PHP-5.5 PHP-5.4 PHP-5.3 PHP-5.2 PHP-5.1 PHP-5.0 bench.php(sec)
  • 43. PHP7 Performance – Reallife App (2015-04-14) 311 112 110 107 82 75 0 0 0 50 100 150 200 250 300 350 PHP-7 PHP-5.6 PHP-5.5 PHP-5.4 PHP-5.3 PHP-5.2 PHP-5.1 PHP-5.0 wordpress 3.0.1 home page qps
  • 44. PHP7 Performance (By Rasmus 2015-04-21)
  • 48. Always Do Your Own Benchmark
  • 49. PHP 7 Next •  Keep 99.99% compatible with PHP5 •  Keep Improving performance •  Port common used PECL extensions (memcached, redis etc) •  Release PHP 7 (oct 2015) •  Restart JIT (Sep 2014)
  • 50. Links •  phpng:_Refactored_PHP_Engine_with_Big_Performance_Improveme nt: http://news.php.net/php.internals/73888 •  PHPNG RFC: https://wiki.php.net/phpng •  PHPNG Implementation details: https://wiki.php.net/phpng-int •  Upgrading PHP extensions from PHP5 to PHPNG: https://wiki.php.net/phpng-upgrading •  Zeev <Benchmarking PHPNG>: http://zsuraski.blogspot.co.il/2014/07/benchmarking-phpng.html •  Rasmus <SPEEDING UP THE WEB WITH PHP 7>: http://talks.php.net/fluent15#/