SlideShare a Scribd company logo
Performances
considerations
with PHP 7
Hi
 Julien PAULI
 SensioLabs tech team (Blackfire - PHP)
 Programming with PHP since early 2000s
 Today working as Unix system programmer (C)
 PHP Internals programmer/contributor
 PHP 5.5 & 5.6 Release Manager
 @julienpauli
 Tech blog at http://jpauli.github.io
 jpauli@php.net
What we'll cover together
 PHP 7 new engine design
 What has changed inside PHP from PHP 5 ?
 PHP 7 new compiler
 Compiler optimizations
 PHP 7 new references mechanism
 PHP 7 new Hashtables (PHP arrays)
 PHP 7 new strings management
The PHP language
 Born in 1995
 Fully written using the C language
 Today
 822,000 C lines of code
 dozens of contributors around the world
PHP VERSIONS SURVEY
PHP code compilation
PHP 7 new compiler
 PHP 7 compiler is now based on an AST
 It has been fully rewritten, and can compute much
more things at compile time
 Every hash of every litteral string f.e
 Resolves every static/litteral expression
 Optimizes some function calls when result is known
at compile time
 defined(), strlen(), cufa(), is_{type}(), assert(), chr(), ord()
 Don't use namespaced calls but native_calls()
PHP 7 optimized compiled functions
namespace Foo;
class Bar
{
public function hello($str)
{
return "Hello" . strlen($str);
}
}
L7 #0 RECV 1 $str
L9 #1 INIT_NS_FCALL_BY_NAME "Foostrlen"
L9 #2 SEND_VAR_EX $str 1
L9 #3 DO_FCALL @0
L9 #4 CONCAT "Hello" @0 ~1
L9 #5 RETURN ~1
PHP 7 optimized compiled functions
namespace Foo;
class Bar
{
public function hello($str)
{
return "Hello" . strlen($str);
}
}
L7 #0 RECV 1 $str
L9 #1 STRLEN $str ~0
L9 #2 CONCAT "Hello" ~0 ~1
L9 #3 RETURN ~1
L10 #4 RETURN null
Namespaced function calls
 We have proven that the performance difference is
really tiny on real use cases.
namespace Foo;
class Bar
{
public function hello($str)
{
return "Hello" . strlen($str);
}
}
namespace Foo;
class Bar
{
public function hello($str)
{
return "Hello" . strlen($str);
}
}
VS
PHP 7 optimized compiled functions
$b = 'foo';
echo strlen($b);
L3 #0 ASSIGN $b "foo"
L5 #1 STRLEN $b ~1
L5 #2 ECHO ~1
echo strlen('foo');
L1 #0 ECHO 3
 Dynamic arg
 Static arg
PHP 7 new compiler
 PHP 7 compiler is usually slower than PHP 5's
 It optimizes more things
 It must walk an AST
 It is globally more complex
 It benefits from a better design
 It is hookable through PHP extensions
 Use OPCache to not suffer from compile time
PHP 7 compiler optim example, static arrays
 Arrays containg keys/vals that are static/litteral
 Such arrays are fully resolved at compile time
 They involve no runtime work at all
const FOO = ['bar', 'baz', 'foo', 34, [42, 'bar'=>'baz']];
Static arrays in PHP 5
 A lot of runtime is eaten to construct the same
array again and again
$a = ['bar', 'baz', 'foo', 34, [42, 'bar'=>'baz']];
3 0 E > INIT_ARRAY ~0 'bar'
1 ADD_ARRAY_ELEMENT ~0 'baz'
2 ADD_ARRAY_ELEMENT ~0 'foo'
3 ADD_ARRAY_ELEMENT ~0 34
4 INIT_ARRAY ~1 42
5 ADD_ARRAY_ELEMENT ~1 'baz', 'bar'
6 ADD_ARRAY_ELEMENT ~0 ~1
7 ASSIGN !0, ~0
Static arrays in PHP 7
 No runtime impact (but compile-time)
 You'd better use OPCache
$a = ['bar', 'baz', 'foo', 34, [42, 'bar'=>'baz']];
L3 #0 ASSIGN $a array(5)
PHP 7 new references mechanism
 In PHP 5, ref mismatching a function call triggered a
full zval copy the engine
 In PHP 7, the deep copy is postponed until COW
breakage
function foo($arg) { }
$a = 'foo';
$b = &$a;
foo($a); /* full copy of the argument */
PHP 7 new references mechanism
 In PHP 7, the deep copy is postponed until COW
breakage
 If no COW breakage, then no copy happens at all
function foo($arg) { $arg = 'bar'; } /* full copy of the variable */
$a = 'foo';
$b = &$a;
foo($a);
$a = ['foo', 42, ['bar' , new stdclass], 'baz'];
$b = &$a;
if (count($a) == 8) { /* no zval copy here */
}
PHP 7 optimizations
from internal
Optimizing CPU time
 Latency Numbers Every Programmer Should Know
 http://lwn.net/Articles/250967/
 http://www.eecs.berkeley.edu/~rcs/research/interactive
_latency.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
PHP 7 cache efficiency
 If we can reduce payload size, the CPU will use its
caches more often
PHP 7.1.4-dev (debug)
128,883666 task-clock (msec)
9 context-switches
0 cpu-migrations
1 768 page-faults
340 930 642 cycles
810 206 077 instructions
100 639 058 branches
187 132 branch-misses
0,131802866 seconds time elapsed
PHP 5.6.31-dev (debug)
730,824226 task-clock (msec)
92 context-switches
1 cpu-migrations
74 691 page-faults
2 030 928 993 cycles
3 766 048 098 instructions
506 047 488 branches
356 931 branch-misses
0,773863158 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
 Hence, every variable usage in PHP 7 is more
optimized than in PHP 5
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 *
 Very less pressure on the heap allocator
 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 much more CPU cache efficient
New Memory Allocator
 PHP 7 has a fully new heap memory allocator
 Zend Memory Manager
 It now uses several allocator pools
 Huge
 Medium
 Small
 ... for better efficiency
 Uses mmap(), no more libc's malloc() overhead
 May use Kernel Huge Pages if told to
 Better CPU TLB usage
PHP 7 new hashtables
PHP 7 hashtables
 It has been fully rewritten, and reworked
 Nothing to say from PHP userland POV
 Except perhaps for the packed array case
Packed arrays
Packed arrays
 If your keys are integer only (no string key)
 If your keys are constantly increasing
 No matter if they don't follow each other with +1
 Then you'll benefit from packed arrays optimization
 Packed arrays will reduce memory size compared
to "normal" array
 Reduction of (table_size - 2) * 4 bytes
 ~ 4Kb for a 1000 entry table
 May be noticeable for BIG arrays
Packed arrays example
const N = 1024 * 1023;
for ($i=0; $i<N; $i++) {
$tab[] = random_bytes(3);
}
echo memory_get_usage();
const N = 1024 * 1023;
for ($i=0; $i<N; $i++) {
$tab[] = random_bytes(3);
}
$tab['foo'] = 'bar';
echo memory_get_usage();
const N = 1024 * 1023;
for ($i=0; $i<N; $i++) {
$tab[] = random_bytes(3);
}
unset($tab[1000]);
$tab[1000] = 1000;
echo memory_get_usage();
~67Mb
~71Mb
~71Mb
Packed arrays conditions (recalled)
 Do NOT use string keys
 Always use increasing integer-based keys
 Contiguous or not is not important
 If using the compiler, keep keys into the interval [0-
table-size] , table-size being rounded to the upper
power of two
 For example, if you need lists , then you'll benefit
from this optimisation
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*
HashTables in PHP 7 : go further
 http://jpauli.github.io/2016/04/08/hashtables.html
Strings
$a = "bar";
String management
 In PHP 5, strings don't have their own structure
 String management is hard
 Leads to many strings duplication
 And thus many memory access
 In PHP 7, strings share the zend_string structure
 They are refcounted, thus shareable
 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
Strings tip
 Don't forget to use OPCache
 OPCache shares interned strings buffer between
processes
 Don't forget to size interned strings buffer
according to your needs
 opcache.interned_strings_buffer
interned string shared memory
PHP master process
PHP child #1 PHP child #2 PHP child #3
Encapsed strings
Encapsed string optimisation
 Encapsed string are double-quoted strings that get
parsed
 They need to be analyzed for variables
 PHP 5 used to reallocate the string at each step
$a = "foo and $b and $c";
3 0 E > ADD_STRING ~0 'foo+and+'
1 ADD_VAR ~0 ~0, !1
2 ADD_STRING ~0 ~0, '+and+'
3 ADD_VAR ~0 ~0, !2
4 ASSIGN !0, ~0
4 5 > RETURN 1
Encapsed string in PHP 5
$a = "foo and $b and $c";
3 0 E > ADD_STRING ~0 'foo+and+'
1 ADD_VAR ~0 ~0, !1
2 ADD_STRING ~0 ~0, '+and+'
3 ADD_VAR ~0 ~0, !2
4 ASSIGN !0, ~0
4 5 > RETURN 1
foo and
foo and b
foo and b and
foo and b and c
 Lot of pressure on the allocator
 Needs to find new chunk
 At every new allocation
 Browses through a free-chunk
linked-list
 Bad for performances
$b = 'b';
$c = 'c';
Encapsed string optimisation in PHP 7
 PHP 7 uses a "rope", and only reallocates memory
once, at the end
 https://en.wikipedia.org/wiki/Rope_(data_structure)
$a = "foo and $b and $c";
L3 #0 ROPE_INIT "foo and " ~1
L3 #1 ROPE_ADD ~1 $b ~1
L3 #2 ROPE_ADD ~1 " and " ~1
L3 #3 ROPE_END ~1 $c ~0
L3 #4 ASSIGN $a ~0
L3 #5 RETURN 1
Encapsed strings in PHP 7
$a = "foo and $b and $c";
L3 #0 ROPE_INIT "foo and " ~1
L3 #1 ROPE_ADD ~1 $b ~1
L3 #2 ROPE_ADD ~1 " and " ~1
L3 #3 ROPE_END ~1 $c ~0
L3 #4 ASSIGN $a ~0
L3 #5 RETURN 1
foo and
foo and b
foo and b and
foo and b and c
foo and b and c
INIT
ADD
ADD
ADD
END
 Keep every piece of string
as its own buffer
 Stack them
 At the end, merge them
as one operation
So ?
 So you'd better use encapsed strings
 Than concatenations
$a = "foo and $b and $c";
$a = 'foo and ' . $b . ' and ' . $c;
Future ?
Future of PHP
 PHP 7 branch keeps optimizing things
 PHP 7 branch keep preparing the massive JIT engine
move that should happen for PHP 8
 PHP 8 is not expected before 2020 at best
 Try at first to migrate to PHP 7 branch
 PHP 7.2 is on its way
 Nov - Dec 2017
 As usual, read wiki.php.net/rfc
 PHP 5.6 will die end of 2017 , and PHP 5 branch as well
Happy PHP 7 ing
Thank you for listening
Ad

Recommended

SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performances
julien pauli
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
julien pauli
 
Php extensions workshop
Php extensions workshop
julien pauli
 
Php7 extensions workshop
Php7 extensions workshop
julien pauli
 
Profiling php5 to php7
Profiling php5 to php7
julien pauli
 
Php and threads ZTS
Php and threads ZTS
julien pauli
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
julien pauli
 
PHP 7 new engine
PHP 7 new engine
julien pauli
 
PHP 7 OPCache extension review
PHP 7 OPCache extension review
julien pauli
 
Quick tour of PHP from inside
Quick tour of PHP from inside
julien pauli
 
Understanding PHP memory
Understanding PHP memory
julien pauli
 
When e-commerce meets Symfony
When e-commerce meets Symfony
Marc Morera
 
Building Custom PHP Extensions
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
The Php Life Cycle
The Php Life Cycle
Xinchen Hui
 
Understanding PHP objects
Understanding PHP objects
julien pauli
 
How PHP Works ?
How PHP Works ?
Ravi Raj
 
Php engine
Php engine
julien pauli
 
PHP5.5 is Here
PHP5.5 is Here
julien pauli
 
Writing and using php streams and sockets
Writing and using php streams and sockets
Elizabeth Smith
 
Php’s guts
Php’s guts
Elizabeth Smith
 
How PHP works
How PHP works
Atlogys Technical Consulting
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
Tim Bunce
 
PHP Internals and Virtual Machine
PHP Internals and Virtual Machine
julien pauli
 
Working with databases in Perl
Working with databases in Perl
Laurent Dami
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
Elizabeth Smith
 
PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012
Tim Bunce
 
typemap in Perl/XS
typemap in Perl/XS
charsbar
 
Socket programming with php
Socket programming with php
Elizabeth Smith
 
The secret of PHP7's Performance
The secret of PHP7's Performance
Xinchen Hui
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
Xinchen Hui
 

More Related Content

What's hot (20)

PHP 7 OPCache extension review
PHP 7 OPCache extension review
julien pauli
 
Quick tour of PHP from inside
Quick tour of PHP from inside
julien pauli
 
Understanding PHP memory
Understanding PHP memory
julien pauli
 
When e-commerce meets Symfony
When e-commerce meets Symfony
Marc Morera
 
Building Custom PHP Extensions
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
The Php Life Cycle
The Php Life Cycle
Xinchen Hui
 
Understanding PHP objects
Understanding PHP objects
julien pauli
 
How PHP Works ?
How PHP Works ?
Ravi Raj
 
Php engine
Php engine
julien pauli
 
PHP5.5 is Here
PHP5.5 is Here
julien pauli
 
Writing and using php streams and sockets
Writing and using php streams and sockets
Elizabeth Smith
 
Php’s guts
Php’s guts
Elizabeth Smith
 
How PHP works
How PHP works
Atlogys Technical Consulting
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
Tim Bunce
 
PHP Internals and Virtual Machine
PHP Internals and Virtual Machine
julien pauli
 
Working with databases in Perl
Working with databases in Perl
Laurent Dami
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
Elizabeth Smith
 
PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012
Tim Bunce
 
typemap in Perl/XS
typemap in Perl/XS
charsbar
 
Socket programming with php
Socket programming with php
Elizabeth Smith
 
PHP 7 OPCache extension review
PHP 7 OPCache extension review
julien pauli
 
Quick tour of PHP from inside
Quick tour of PHP from inside
julien pauli
 
Understanding PHP memory
Understanding PHP memory
julien pauli
 
When e-commerce meets Symfony
When e-commerce meets Symfony
Marc Morera
 
The Php Life Cycle
The Php Life Cycle
Xinchen Hui
 
Understanding PHP objects
Understanding PHP objects
julien pauli
 
How PHP Works ?
How PHP Works ?
Ravi Raj
 
Writing and using php streams and sockets
Writing and using php streams and sockets
Elizabeth Smith
 
PL/Perl - New Features in PostgreSQL 9.0
PL/Perl - New Features in PostgreSQL 9.0
Tim Bunce
 
PHP Internals and Virtual Machine
PHP Internals and Virtual Machine
julien pauli
 
Working with databases in Perl
Working with databases in Perl
Laurent Dami
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
Elizabeth Smith
 
PL/Perl - New Features in PostgreSQL 9.0 201012
PL/Perl - New Features in PostgreSQL 9.0 201012
Tim Bunce
 
typemap in Perl/XS
typemap in Perl/XS
charsbar
 
Socket programming with php
Socket programming with php
Elizabeth Smith
 

Similar to Symfony live 2017_php7_performances (20)

The secret of PHP7's Performance
The secret of PHP7's Performance
Xinchen Hui
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
Xinchen Hui
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
Nikita Popov
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
Nikita Popov
 
PHP in 2018 - Q1 - AFUP Limoges
PHP in 2018 - Q1 - AFUP Limoges
✅ William Pinaud
 
PHP7 - For Its Best Performance
PHP7 - For Its Best Performance
Xinchen Hui
 
New Features in PHP 5.3
New Features in PHP 5.3
Bradley Holt
 
PHP7: Hello World!
PHP7: Hello World!
Pavel Nikolov
 
TDC SP 2015 - PHP7: better & faster
TDC SP 2015 - PHP7: better & faster
Bruno Ricardo Siqueira
 
PHP7 Presentation
PHP7 Presentation
David Sanchez
 
What To Expect From PHP7
What To Expect From PHP7
Codemotion
 
The new features of PHP 7
The new features of PHP 7
Zend by Rogue Wave Software
 
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
Codemotion
 
Migrating to PHP 7
Migrating to PHP 7
John Coggeshall
 
Last train to php 7
Last train to php 7
Damien Seguy
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
julien pauli
 
PHP7.1 New Features & Performance
PHP7.1 New Features & Performance
Xinchen Hui
 
A brief to PHP 7.3
A brief to PHP 7.3
Xinchen Hui
 
Php 7 - YNS
Php 7 - YNS
Alex Amistad
 
Php 7.2 compliance workshop php benelux
Php 7.2 compliance workshop php benelux
Damien Seguy
 
The secret of PHP7's Performance
The secret of PHP7's Performance
Xinchen Hui
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
Xinchen Hui
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
Nikita Popov
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
Nikita Popov
 
PHP in 2018 - Q1 - AFUP Limoges
PHP in 2018 - Q1 - AFUP Limoges
✅ William Pinaud
 
PHP7 - For Its Best Performance
PHP7 - For Its Best Performance
Xinchen Hui
 
New Features in PHP 5.3
New Features in PHP 5.3
Bradley Holt
 
What To Expect From PHP7
What To Expect From PHP7
Codemotion
 
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
Codemotion
 
Last train to php 7
Last train to php 7
Damien Seguy
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
julien pauli
 
PHP7.1 New Features & Performance
PHP7.1 New Features & Performance
Xinchen Hui
 
A brief to PHP 7.3
A brief to PHP 7.3
Xinchen Hui
 
Php 7.2 compliance workshop php benelux
Php 7.2 compliance workshop php benelux
Damien Seguy
 
Ad

More from julien pauli (14)

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

Recently uploaded (20)

Azure_Landing_Zone_Best_Practices_Visuals.pptx
Azure_Landing_Zone_Best_Practices_Visuals.pptx
fredsonbarbosa1
 
Paper: The World Game (s) Great Redesign.pdf
Paper: The World Game (s) Great Redesign.pdf
Steven McGee
 
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
taqyea
 
Transmission Control Protocol (TCP) and Starlink
Transmission Control Protocol (TCP) and Starlink
APNIC
 
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
taqyed
 
BASICS OF SAP _ ALL ABOUT SAP _WHY SAP OVER ANY OTHER ERP SYSTEM
BASICS OF SAP _ ALL ABOUT SAP _WHY SAP OVER ANY OTHER ERP SYSTEM
AhmadAli716831
 
The ARUBA Kind of new Proposal Umum .pptx
The ARUBA Kind of new Proposal Umum .pptx
andiwarneri
 
原版一样(ISM毕业证书)德国多特蒙德国际管理学院毕业证多少钱
原版一样(ISM毕业证书)德国多特蒙德国际管理学院毕业证多少钱
taqyed
 
history of internet in nepal Class-8 (sparsha).pptx
history of internet in nepal Class-8 (sparsha).pptx
SPARSH508080
 
BitRecover OST to PST Converter Software
BitRecover OST to PST Converter Software
antoniogosling01
 
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
AhmadAli716831
 
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
taqyed
 
Almos Entirely Correct Mixing with Apps to Voting
Almos Entirely Correct Mixing with Apps to Voting
gapati2964
 
BroadLink Cloud Service introduction.pdf
BroadLink Cloud Service introduction.pdf
DevendraDwivdi1
 
DDoS in India, presented at INNOG 8 by Dave Phelan
DDoS in India, presented at INNOG 8 by Dave Phelan
APNIC
 
Global Networking Trends, presented at the India ISP Conclave 2025
Global Networking Trends, presented at the India ISP Conclave 2025
APNIC
 
Logging and Automated Alerting Webinar.pdf
Logging and Automated Alerting Webinar.pdf
ControlCase
 
Q1 English3 Week5 [email protected]
Q1 English3 Week5 [email protected]
JenniferCawaling1
 
Clive Dickens RedTech Public Copy - Collaborate or Die
Clive Dickens RedTech Public Copy - Collaborate or Die
Clive Dickens
 
ChatGPT A.I. Powered Chatbot and Popularization.pdf
ChatGPT A.I. Powered Chatbot and Popularization.pdf
StanleySamson1
 
Azure_Landing_Zone_Best_Practices_Visuals.pptx
Azure_Landing_Zone_Best_Practices_Visuals.pptx
fredsonbarbosa1
 
Paper: The World Game (s) Great Redesign.pdf
Paper: The World Game (s) Great Redesign.pdf
Steven McGee
 
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
最新版美国特拉华大学毕业证(UDel毕业证书)原版定制
taqyea
 
Transmission Control Protocol (TCP) and Starlink
Transmission Control Protocol (TCP) and Starlink
APNIC
 
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
原版澳洲斯文本科技大学毕业证(SUT毕业证书)如何办理
taqyed
 
BASICS OF SAP _ ALL ABOUT SAP _WHY SAP OVER ANY OTHER ERP SYSTEM
BASICS OF SAP _ ALL ABOUT SAP _WHY SAP OVER ANY OTHER ERP SYSTEM
AhmadAli716831
 
The ARUBA Kind of new Proposal Umum .pptx
The ARUBA Kind of new Proposal Umum .pptx
andiwarneri
 
原版一样(ISM毕业证书)德国多特蒙德国际管理学院毕业证多少钱
原版一样(ISM毕业证书)德国多特蒙德国际管理学院毕业证多少钱
taqyed
 
history of internet in nepal Class-8 (sparsha).pptx
history of internet in nepal Class-8 (sparsha).pptx
SPARSH508080
 
BitRecover OST to PST Converter Software
BitRecover OST to PST Converter Software
antoniogosling01
 
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
PROCESS FOR CREATION OF BUSINESS PARTNER IN SAP
AhmadAli716831
 
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
最新版加拿大奎斯特大学毕业证(QUC毕业证书)原版定制
taqyed
 
Almos Entirely Correct Mixing with Apps to Voting
Almos Entirely Correct Mixing with Apps to Voting
gapati2964
 
BroadLink Cloud Service introduction.pdf
BroadLink Cloud Service introduction.pdf
DevendraDwivdi1
 
DDoS in India, presented at INNOG 8 by Dave Phelan
DDoS in India, presented at INNOG 8 by Dave Phelan
APNIC
 
Global Networking Trends, presented at the India ISP Conclave 2025
Global Networking Trends, presented at the India ISP Conclave 2025
APNIC
 
Logging and Automated Alerting Webinar.pdf
Logging and Automated Alerting Webinar.pdf
ControlCase
 
Clive Dickens RedTech Public Copy - Collaborate or Die
Clive Dickens RedTech Public Copy - Collaborate or Die
Clive Dickens
 
ChatGPT A.I. Powered Chatbot and Popularization.pdf
ChatGPT A.I. Powered Chatbot and Popularization.pdf
StanleySamson1
 

Symfony live 2017_php7_performances

  • 2. Hi  Julien PAULI  SensioLabs tech team (Blackfire - PHP)  Programming with PHP since early 2000s  Today working as Unix system programmer (C)  PHP Internals programmer/contributor  PHP 5.5 & 5.6 Release Manager  @julienpauli  Tech blog at http://jpauli.github.io  [email protected]
  • 3. What we'll cover together  PHP 7 new engine design  What has changed inside PHP from PHP 5 ?  PHP 7 new compiler  Compiler optimizations  PHP 7 new references mechanism  PHP 7 new Hashtables (PHP arrays)  PHP 7 new strings management
  • 4. The PHP language  Born in 1995  Fully written using the C language  Today  822,000 C lines of code  dozens of contributors around the world
  • 7. PHP 7 new compiler  PHP 7 compiler is now based on an AST  It has been fully rewritten, and can compute much more things at compile time  Every hash of every litteral string f.e  Resolves every static/litteral expression  Optimizes some function calls when result is known at compile time  defined(), strlen(), cufa(), is_{type}(), assert(), chr(), ord()  Don't use namespaced calls but native_calls()
  • 8. PHP 7 optimized compiled functions namespace Foo; class Bar { public function hello($str) { return "Hello" . strlen($str); } } L7 #0 RECV 1 $str L9 #1 INIT_NS_FCALL_BY_NAME "Foostrlen" L9 #2 SEND_VAR_EX $str 1 L9 #3 DO_FCALL @0 L9 #4 CONCAT "Hello" @0 ~1 L9 #5 RETURN ~1
  • 9. PHP 7 optimized compiled functions namespace Foo; class Bar { public function hello($str) { return "Hello" . strlen($str); } } L7 #0 RECV 1 $str L9 #1 STRLEN $str ~0 L9 #2 CONCAT "Hello" ~0 ~1 L9 #3 RETURN ~1 L10 #4 RETURN null
  • 10. Namespaced function calls  We have proven that the performance difference is really tiny on real use cases. namespace Foo; class Bar { public function hello($str) { return "Hello" . strlen($str); } } namespace Foo; class Bar { public function hello($str) { return "Hello" . strlen($str); } } VS
  • 11. PHP 7 optimized compiled functions $b = 'foo'; echo strlen($b); L3 #0 ASSIGN $b "foo" L5 #1 STRLEN $b ~1 L5 #2 ECHO ~1 echo strlen('foo'); L1 #0 ECHO 3  Dynamic arg  Static arg
  • 12. PHP 7 new compiler  PHP 7 compiler is usually slower than PHP 5's  It optimizes more things  It must walk an AST  It is globally more complex  It benefits from a better design  It is hookable through PHP extensions  Use OPCache to not suffer from compile time
  • 13. PHP 7 compiler optim example, static arrays  Arrays containg keys/vals that are static/litteral  Such arrays are fully resolved at compile time  They involve no runtime work at all const FOO = ['bar', 'baz', 'foo', 34, [42, 'bar'=>'baz']];
  • 14. Static arrays in PHP 5  A lot of runtime is eaten to construct the same array again and again $a = ['bar', 'baz', 'foo', 34, [42, 'bar'=>'baz']]; 3 0 E > INIT_ARRAY ~0 'bar' 1 ADD_ARRAY_ELEMENT ~0 'baz' 2 ADD_ARRAY_ELEMENT ~0 'foo' 3 ADD_ARRAY_ELEMENT ~0 34 4 INIT_ARRAY ~1 42 5 ADD_ARRAY_ELEMENT ~1 'baz', 'bar' 6 ADD_ARRAY_ELEMENT ~0 ~1 7 ASSIGN !0, ~0
  • 15. Static arrays in PHP 7  No runtime impact (but compile-time)  You'd better use OPCache $a = ['bar', 'baz', 'foo', 34, [42, 'bar'=>'baz']]; L3 #0 ASSIGN $a array(5)
  • 16. PHP 7 new references mechanism  In PHP 5, ref mismatching a function call triggered a full zval copy the engine  In PHP 7, the deep copy is postponed until COW breakage function foo($arg) { } $a = 'foo'; $b = &$a; foo($a); /* full copy of the argument */
  • 17. PHP 7 new references mechanism  In PHP 7, the deep copy is postponed until COW breakage  If no COW breakage, then no copy happens at all function foo($arg) { $arg = 'bar'; } /* full copy of the variable */ $a = 'foo'; $b = &$a; foo($a); $a = ['foo', 42, ['bar' , new stdclass], 'baz']; $b = &$a; if (count($a) == 8) { /* no zval copy here */ }
  • 19. Optimizing CPU time  Latency Numbers Every Programmer Should Know  http://lwn.net/Articles/250967/  http://www.eecs.berkeley.edu/~rcs/research/interactive _latency.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
  • 20. 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
  • 21. PHP 7 cache efficiency  If we can reduce payload size, the CPU will use its caches more often PHP 7.1.4-dev (debug) 128,883666 task-clock (msec) 9 context-switches 0 cpu-migrations 1 768 page-faults 340 930 642 cycles 810 206 077 instructions 100 639 058 branches 187 132 branch-misses 0,131802866 seconds time elapsed PHP 5.6.31-dev (debug) 730,824226 task-clock (msec) 92 context-switches 1 cpu-migrations 74 691 page-faults 2 030 928 993 cycles 3 766 048 098 instructions 506 047 488 branches 356 931 branch-misses 0,773863158 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  Hence, every variable usage in PHP 7 is more optimized than in PHP 5
  • 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 *  Very less pressure on the heap allocator  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 much more CPU cache efficient
  • 26. New Memory Allocator  PHP 7 has a fully new heap memory allocator  Zend Memory Manager  It now uses several allocator pools  Huge  Medium  Small  ... for better efficiency  Uses mmap(), no more libc's malloc() overhead  May use Kernel Huge Pages if told to  Better CPU TLB usage
  • 27. PHP 7 new hashtables
  • 28. PHP 7 hashtables  It has been fully rewritten, and reworked  Nothing to say from PHP userland POV  Except perhaps for the packed array case
  • 30. Packed arrays  If your keys are integer only (no string key)  If your keys are constantly increasing  No matter if they don't follow each other with +1  Then you'll benefit from packed arrays optimization  Packed arrays will reduce memory size compared to "normal" array  Reduction of (table_size - 2) * 4 bytes  ~ 4Kb for a 1000 entry table  May be noticeable for BIG arrays
  • 31. Packed arrays example const N = 1024 * 1023; for ($i=0; $i<N; $i++) { $tab[] = random_bytes(3); } echo memory_get_usage(); const N = 1024 * 1023; for ($i=0; $i<N; $i++) { $tab[] = random_bytes(3); } $tab['foo'] = 'bar'; echo memory_get_usage(); const N = 1024 * 1023; for ($i=0; $i<N; $i++) { $tab[] = random_bytes(3); } unset($tab[1000]); $tab[1000] = 1000; echo memory_get_usage(); ~67Mb ~71Mb ~71Mb
  • 32. Packed arrays conditions (recalled)  Do NOT use string keys  Always use increasing integer-based keys  Contiguous or not is not important  If using the compiler, keep keys into the interval [0- table-size] , table-size being rounded to the upper power of two  For example, if you need lists , then you'll benefit from this optimisation
  • 33. 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
  • 34. 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*
  • 35. HashTables in PHP 7 : go further  http://jpauli.github.io/2016/04/08/hashtables.html
  • 37. String management  In PHP 5, strings don't have their own structure  String management is hard  Leads to many strings duplication  And thus many memory access  In PHP 7, strings share the zend_string structure  They are refcounted, thus shareable  hashes are precomputed, often at compile time  struct hack is used to compact memory
  • 38. 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
  • 39. Strings in PHP 5  $a = "foo"; 3 0X00007FFFF7F8C708 foo0
  • 40. Strings in PHP 7  $a = "foo"; foo0 C struct hack 3 memory border
  • 41. Strings tip  Don't forget to use OPCache  OPCache shares interned strings buffer between processes  Don't forget to size interned strings buffer according to your needs  opcache.interned_strings_buffer interned string shared memory PHP master process PHP child #1 PHP child #2 PHP child #3
  • 43. Encapsed string optimisation  Encapsed string are double-quoted strings that get parsed  They need to be analyzed for variables  PHP 5 used to reallocate the string at each step $a = "foo and $b and $c"; 3 0 E > ADD_STRING ~0 'foo+and+' 1 ADD_VAR ~0 ~0, !1 2 ADD_STRING ~0 ~0, '+and+' 3 ADD_VAR ~0 ~0, !2 4 ASSIGN !0, ~0 4 5 > RETURN 1
  • 44. Encapsed string in PHP 5 $a = "foo and $b and $c"; 3 0 E > ADD_STRING ~0 'foo+and+' 1 ADD_VAR ~0 ~0, !1 2 ADD_STRING ~0 ~0, '+and+' 3 ADD_VAR ~0 ~0, !2 4 ASSIGN !0, ~0 4 5 > RETURN 1 foo and foo and b foo and b and foo and b and c  Lot of pressure on the allocator  Needs to find new chunk  At every new allocation  Browses through a free-chunk linked-list  Bad for performances $b = 'b'; $c = 'c';
  • 45. Encapsed string optimisation in PHP 7  PHP 7 uses a "rope", and only reallocates memory once, at the end  https://en.wikipedia.org/wiki/Rope_(data_structure) $a = "foo and $b and $c"; L3 #0 ROPE_INIT "foo and " ~1 L3 #1 ROPE_ADD ~1 $b ~1 L3 #2 ROPE_ADD ~1 " and " ~1 L3 #3 ROPE_END ~1 $c ~0 L3 #4 ASSIGN $a ~0 L3 #5 RETURN 1
  • 46. Encapsed strings in PHP 7 $a = "foo and $b and $c"; L3 #0 ROPE_INIT "foo and " ~1 L3 #1 ROPE_ADD ~1 $b ~1 L3 #2 ROPE_ADD ~1 " and " ~1 L3 #3 ROPE_END ~1 $c ~0 L3 #4 ASSIGN $a ~0 L3 #5 RETURN 1 foo and foo and b foo and b and foo and b and c foo and b and c INIT ADD ADD ADD END  Keep every piece of string as its own buffer  Stack them  At the end, merge them as one operation
  • 47. So ?  So you'd better use encapsed strings  Than concatenations $a = "foo and $b and $c"; $a = 'foo and ' . $b . ' and ' . $c;
  • 49. Future of PHP  PHP 7 branch keeps optimizing things  PHP 7 branch keep preparing the massive JIT engine move that should happen for PHP 8  PHP 8 is not expected before 2020 at best  Try at first to migrate to PHP 7 branch  PHP 7.2 is on its way  Nov - Dec 2017  As usual, read wiki.php.net/rfc  PHP 5.6 will die end of 2017 , and PHP 5 branch as well
  • 50. Happy PHP 7 ing
  • 51. Thank you for listening