SlideShare a Scribd company logo
1 of 83
Download to read offline
PHP 7
What changed internally?
Nikita Popov
http://hhvm.com/blog/9293/lockdown-results-and-hhvm-performance
Memory optimization
Memory optimization
• Reduce number of allocations
Memory optimization
• Reduce number of allocations
Memory
Memory optimization
• Reduce number of allocations
Memory
Memory optimization
• Reduce number of allocations
• PHP 5 spends 20% of CPU time in the allocator
Memory
Memory optimization
• Reduce number of allocations
• Reduce memory usage
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU L1D
1ns 32KB
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU
4ns
L2
256 KB
L1D
1ns 32KB
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU L312ns
a few MB
4ns
L2
256 KB
L1D
1ns 32KB
RAM
100ns
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
CPU L312ns
a few MB
4ns
L2
256 KB
L1D
1ns 32KB
RAM
100ns
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Memory access has high latency
• Less data => more fits into the cache
CPU L312ns
a few MB
4ns
L2
256 KB
L1D
1ns 32KB
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Reduce indirection
Memory optimization
• Reduce number of allocations
• Reduce memory usage
• Reduce indirection
• Pointer points to pointer pointing to pointer pointing to pointer pointing to
pointer pointing to pointer pointing to what you actually want
What you
have
What you
want
Zvals: PHP 5
• Zval = Type + Value
value
ty
zval
Zvals: PHP 5
• Zval = Type + Value
value (simple):
null, bool, int, float
ty
zval
Zvals: PHP 5
• Zval = Type + Value
value (complex)
ty
zval
Complex data structure:
string, array, object
Zvals: PHP 5
• Zval = Type + Value
value (complex)
ty
zval
Complex data structure:
string, array, object
$a = [];Code:
Zvals: PHP 5
• Zval = Type + Value
value (complex)
ty
zval *
zval
$a:
Complex data structure:
string, array, object
$a = [];Code:
Zvals: PHP 5
• Zval = Type + Value
value (complex)
ty
zval *
zval
$a:
Complex data structure:
string, array, object
$a = [];
$b = $a;
Code:
Zvals: PHP 5
• Zval = Type + Value + Refcount
value (complex)
refcount = 1 ty
zval *
zval
$a:
Complex data structure:
string, array, object
$a = [];
$b = $a;
Code:
Zvals: PHP 5
• Zval = Type + Value + Refcount
value (complex)
refcount = 2 ty
zval *
zval
$a:
Complex data structure:
string, array, object
$a = [];
$b = $a;
Code:
zval *$b:
Zvals: PHP 7
• Zval = Type + Value
value (complex)
ty
zval *
zval
$a:
$a = [];
$b = $a;
Code:
zval *$b:
Complex data structure:
string, array, object
refcount = 2
Zvals: PHP 7
• Zval = Type + Value
zval *
zval
$a:
$a = [];
$b = $a;
Code:
zval *$b:
Complex data structure:
string, array, object
refcount = 2
value (complex)
type_info
value (complex)
type_info
value (complex)
type_info
Zvals: PHP 7
• Zval = Type + Value
$a:
Complex data structure:
string, array, object
$b:
refcount = 2
zval
zval
PHP 5 PHP 7
value (simple):
null, bool, int, float
refcount ty
gc_buffer
zval * value (simple): …
type_info
1 allocations
1 level of indirection
40 bytes
no allocations
no indirection
16 bytes
PHP 5 PHP 7
value (complex)
refcount ty
gc_root
zval *
Complex data structure:
string, array, object
value (complex)
type_info
Complex data structure:
string, array, object
refcount gc_info
2 allocations
2 levels of indirection
40 bytes
1 allocation
1 level of indirection
24 bytes
val (char *)
len (int)
A B C 0
zval value:
PHP 5
zend_string *
refcount gc_info
hash
len (size_t)
A B C 0
val (char *)
len (int)
A B C 0
zval value: zval value:
PHP 5 PHP 7
[0]: (empty)
[1]: (empty)
[2]: (empty)
[3]: (empty)
hash()
“xyz”
Arrays: PHP 5
[0]: (empty)
[1]:
[2]: (empty)
[3]: (empty)
hash()
“xyz”
data
key “xyz”
Arrays: PHP 5
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
data
key “foo”
hash()
“xyz”
data
key “xyz”
Arrays: PHP 5
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
data
key “foo”
hash()
“xyz”
data
key “xyz”
Arrays: PHP 5
“bar”
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
data
key “foo”
next
data
key “bar”
next = NULL
hash()
“bar”
“xyz”
data
key “xyz”
next = NULL
Arrays: PHP 5
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
hash()
“bar”
“xyz”
Arrays: PHP 5
head
tail
data
key “foo”
next
data
key “bar”
next = NULL
data
key “xyz”
next = NULL
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
hash()
“bar”
“xyz”
Arrays: PHP 7
data
key “foo”
next
data
key “bar”
next = NULL
data
key “xyz”
next = NULL
head
tail
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
hash()
“bar”
“xyz”
Arrays: PHP 7
data
key “foo”
next
data
key “bar”
next = NULL
data
key “xyz”
next = NULL
[0]: (empty)
[1]:
[2]: (empty)
[3]:
“foo”
hash()
“bar”
“xyz”
Arrays: PHP 7
Bucket *
uint32_t
data
key “foo”
next
data
key “bar”
next = NULL
data
key “xyz”
next = NULL
[0]: (empty)
[1]:
[2]: (empty)
[3]:
hash()
“bar”
“xyz”
Arrays: PHP 7
Bucket *
uint32_t
data
key “foo”
next
data
key “bar”
next = NULL
data
key “xyz”
next = NULL
[0]: (empty)
[1]:
[2]: (empty)
[3]:
hash()
“bar”
“xyz”
Arrays: PHP 7
Bucket *
uint32_t
TOMBSTONE
data
key “bar”
next = NULL
data
key “xyz”
next = NULL
PHP 5 PHP 7
Allocations: 2 + 2 per element 2 + 0 per element
Size: 72 + 112 per element 56 + 36 per element
Indirections (lookup): 4 2
Arrays: PHP 5 vs. PHP 7
(For arrays with unique values)
PHP 5 PHP 7
Allocations: 2 + 2 per element 2 + 0 per element
Size: 72 + 112 per element 56 + 36 per element
Indirections (lookup): 4 2
Arrays: PHP 5 vs. PHP 7
(For arrays with unique values)
Immutable arrays
$arrays = [];
for ($i = 0; $i < 1000000; ++$i) {
$arrays[] = [1, 2, 3, 4, 5, 6, 7, 8];
}
Immutable arrays
$arrays = [];
for ($i = 0; $i < 1000000; ++$i) {
$arrays[] = [1, 2, 3, 4, 5, 6, 7, 8];
}
PHP 5
Memory usage 1336 MB
Execution time (create) 0.77 s
Execution time (destroy) 1.45 s
Immutable arrays
$arrays = [];
for ($i = 0; $i < 1000000; ++$i) {
$arrays[] = [1, 2, 3, 4, 5, 6, 7, 8];
}
PHP 5 PHP 7
Memory usage 1336 MB 391 MB
Execution time (create) 0.77 s 0.29 s
Execution time (destroy) 1.45 s 0.05 s
Immutable arrays
$arrays = [];
for ($i = 0; $i < 1000000; ++$i) {
$arrays[] = [1, 2, 3, 4, 5, 6, 7, 8];
}
PHP 5 PHP 7 PHP 7 with opcache
Memory usage 1336 MB 391 MB 32 MB
Execution time (create) 0.77 s 0.29 s 0.03 s
Execution time (destroy) 1.45 s 0.05 s 0.002 s
Objects
Objects
zval
object store bucket
actual object
properties table
PHP 5
property value
Objects
zval
object store bucket
actual object
properties table
zval
actual object
properties table
(including values)
+
PHP 5 PHP 7
property value
PHP 5 PHP 7
Allocations: 2 + 1 per property 1 + 0 per property
Size: 96 + 40 per property 48 + 16 per property
Indirections (prop val): 4 1
Objects: PHP 5 vs. PHP 7
(For objects with unique properties)
Integers
Integers
64-bit integers
on
64-bit Windows
AST
<?php
$a = 42;
$b = 24;
echo $a + $b;
AST
<?php
$a = 42;
$b = 24;
echo $a + $b;
<?php T_OPEN_TAG
$a T_VARIABLE
=
42 T_LNUMBER
;
$b T_VARIABLE
=
24 T_LNUMBER
;
echo T_ECHO
$a T_VARIABLE
+
$b T_VARIABLE
;
lex
AST
<?php
$a = 42;
$b = 24;
echo $a + $b;
<?php T_OPEN_TAG
$a T_VARIABLE
=
42 T_LNUMBER
;
$b T_VARIABLE
=
24 T_LNUMBER
;
echo T_ECHO
$a T_VARIABLE
+
$b T_VARIABLE
;
ASSIGN $a 42
ASSIGN $b 24
ADD $a $b ~2
ECHO ~2
RETURN 1
lex
parse + compile
AST
<?php
$a = 42;
$b = 24;
echo $a + $b;
<?php T_OPEN_TAG
$a T_VARIABLE
=
42 T_LNUMBER
;
$b T_VARIABLE
=
24 T_LNUMBER
;
echo T_ECHO
$a T_VARIABLE
+
$b T_VARIABLE
;
ASSIGN $a 42
ASSIGN $b 24
ADD $a $b ~2
ECHO ~2
RETURN 1
lex parse
compile
stmts
assign
var 42
$a
assign
$b
echo
+
$a $b
var 24
AST
<?php
$a = 42;
$b = 24;
echo $a + $b;
<?php T_OPEN_TAG
$a T_VARIABLE
=
42 T_LNUMBER
;
$b T_VARIABLE
=
24 T_LNUMBER
;
echo T_ECHO
$a T_VARIABLE
+
$b T_VARIABLE
;
ASSIGN $a 42
ASSIGN $b 24
ADD $a $b ~0
ECHO ~0
RETURN 1
lex parse
compile
token_get_all()
nikic/php-ast
phpdbg -p
stmts
assign
var 42
$a
assign
$b
echo
+
$a $b
var 24
Virtual machine
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
SEND_VAL 1
SEND_VAL 2
DO_FCALL foo
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
{main}
foo()
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
SEND_VAL 1
SEND_VAL 2
DO_FCALL foo
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
{main}
foo()
…
VM stack
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
SEND_VAL 1
SEND_VAL 2
DO_FCALL foo
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
{main}
foo()
…
int(1)
int(2)
VM stack
arg[0]
arg[1]
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
SEND_VAL 1
SEND_VAL 2
DO_FCALL foo
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
{main}
foo()
…
int(1)
int(2)
execute_data
VM stack
arg[0]
arg[1]
~0
$a
$b
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
SEND_VAL 1
SEND_VAL 2
DO_FCALL foo
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
{main}
foo()
…
int(1)
int(2)
execute_data
int(1)
int(2)
VM stack
arg[0]
arg[1]
~0
$a
$b
Virtual machine – stack management (PHP 5)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
SEND_VAL 1
SEND_VAL 2
DO_FCALL foo
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
{main}
foo()
…
int(1)
int(2)
int(3)
execute_data
int(1)
int(2)
VM stack
arg[0]
arg[1]
~0
$a
$b
Virtual machine – stack management (PHP 7)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
{main}
foo()
…
VM stack
INIT_FCALL foo
SEND_VAL 1
SEND_VAL 2
DO_FCALL
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
Virtual machine – stack management (PHP 7)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
{main}
foo()
…
execute_data
VM stack
~0
$a
$b
INIT_FCALL foo
SEND_VAL 1
SEND_VAL 2
DO_FCALL
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
Virtual machine – stack management (PHP 7)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
{main}
foo()
…
execute_data
int(1)
int(2)
VM stack
~0
$a
$b
INIT_FCALL foo
SEND_VAL 1
SEND_VAL 2
DO_FCALL
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
Virtual machine – stack management (PHP 7)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
{main}
foo()
…
execute_data
int(1)
int(2)
VM stack
~0
$a
$b
INIT_FCALL foo
SEND_VAL 1
SEND_VAL 2
DO_FCALL
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
Virtual machine – stack management (PHP 7)
function foo($a, $b) {
return $a + $b;
}
foo(1, 2);
{main}
foo()
…
execute_data
int(1)
int(2)
int(3)
VM stack
~0
$a
$b
INIT_FCALL foo
SEND_VAL 1
SEND_VAL 2
DO_FCALL
RECV $a
RECV $b
ADD $a $b ~0
RETURN ~0
Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
• Only in global scope or for fully qualified calls
namespace foo;
echo strlen($str);
Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
• Only in global scope or for fully qualified calls
namespace foo;
echo strlen($str);
Is this strlen() or foostrlen()?
Virtual machine – inlined internal functions
• Functions with custom opcodes:
• strlen()
• is_*()
• defined()
• call_user_func()
• call_user_func_array()
• Only in global scope or for fully qualified calls
namespace foo;
echo strlen($str);
This is definitely strlen()!
Virtual machine – global registers
zend_execute_data* execute_data;
const zend_op* opline;
Virtual machine – global registers
register zend_execute_data* execute_data __asm__("%r14");
register const zend_op* opline __asm__("%r15");
Virtual machine – global registers
register zend_execute_data* execute_data __asm__("%r14");
register const zend_op* opline __asm__("%r15");
• Registers reserved in VM code
Virtual machine – global registers
register zend_execute_data* execute_data __asm__("%r14");
register const zend_op* opline __asm__("%r15");
• Registers reserved in VM code
• Avoid reloading from executor_globals / execute_data
Virtual machine – global registers
register zend_execute_data* execute_data __asm__("%r14");
register const zend_op* opline __asm__("%r15");
• Registers reserved in VM code
• Avoid reloading from executor_globals / execute_data
• Avoid save/restore during calls
@nikita_ppv
nikic@php.net
https://joind.in/15255

More Related Content

What's hot

Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 

What's hot (20)

Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
 
SPL: The Undiscovered Library - DataStructures
SPL: The Undiscovered Library -  DataStructuresSPL: The Undiscovered Library -  DataStructures
SPL: The Undiscovered Library - DataStructures
 
Invertible-syntax 入門
Invertible-syntax 入門Invertible-syntax 入門
Invertible-syntax 入門
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
 
Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in Haskell
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
Solr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg DonovanSolr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg Donovan
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
Solr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene EuroconSolr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene Eurocon
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Living with garbage
Living with garbageLiving with garbage
Living with garbage
 
Generated Power: PHP 5.5 Generators
Generated Power: PHP 5.5 GeneratorsGenerated Power: PHP 5.5 Generators
Generated Power: PHP 5.5 Generators
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11
 

Similar to PHP 7 – What changed internally? (Forum PHP 2015)

Similar to PHP 7 – What changed internally? (Forum PHP 2015) (20)

Profiling php5 to php7
Profiling php5 to php7Profiling php5 to php7
Profiling php5 to php7
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
Cli the other sapi pbc11
Cli the other sapi pbc11Cli the other sapi pbc11
Cli the other sapi pbc11
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11
 
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)
 
Symfony live 2017_php7_performances
Symfony live 2017_php7_performancesSymfony live 2017_php7_performances
Symfony live 2017_php7_performances
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Perl at SkyCon'12
Perl at SkyCon'12Perl at SkyCon'12
Perl at SkyCon'12
 
Compact ordered dict__k_lab_meeting_
Compact ordered dict__k_lab_meeting_Compact ordered dict__k_lab_meeting_
Compact ordered dict__k_lab_meeting_
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5The why and how of moving to php 5.4/5.5
The why and how of moving to php 5.4/5.5
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

PHP 7 – What changed internally? (Forum PHP 2015)

  • 1. PHP 7 What changed internally? Nikita Popov
  • 4. Memory optimization • Reduce number of allocations
  • 5. Memory optimization • Reduce number of allocations Memory
  • 6. Memory optimization • Reduce number of allocations Memory
  • 7. Memory optimization • Reduce number of allocations • PHP 5 spends 20% of CPU time in the allocator Memory
  • 8. Memory optimization • Reduce number of allocations • Reduce memory usage
  • 9. Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency
  • 10. Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency CPU
  • 11. Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency CPU L1D 1ns 32KB
  • 12. Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency CPU 4ns L2 256 KB L1D 1ns 32KB
  • 13. Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency CPU L312ns a few MB 4ns L2 256 KB L1D 1ns 32KB
  • 14. RAM 100ns Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency CPU L312ns a few MB 4ns L2 256 KB L1D 1ns 32KB
  • 15. RAM 100ns Memory optimization • Reduce number of allocations • Reduce memory usage • Memory access has high latency • Less data => more fits into the cache CPU L312ns a few MB 4ns L2 256 KB L1D 1ns 32KB
  • 16. Memory optimization • Reduce number of allocations • Reduce memory usage • Reduce indirection
  • 17. Memory optimization • Reduce number of allocations • Reduce memory usage • Reduce indirection • Pointer points to pointer pointing to pointer pointing to pointer pointing to pointer pointing to pointer pointing to what you actually want What you have What you want
  • 18. Zvals: PHP 5 • Zval = Type + Value value ty zval
  • 19. Zvals: PHP 5 • Zval = Type + Value value (simple): null, bool, int, float ty zval
  • 20. Zvals: PHP 5 • Zval = Type + Value value (complex) ty zval Complex data structure: string, array, object
  • 21. Zvals: PHP 5 • Zval = Type + Value value (complex) ty zval Complex data structure: string, array, object $a = [];Code:
  • 22. Zvals: PHP 5 • Zval = Type + Value value (complex) ty zval * zval $a: Complex data structure: string, array, object $a = [];Code:
  • 23. Zvals: PHP 5 • Zval = Type + Value value (complex) ty zval * zval $a: Complex data structure: string, array, object $a = []; $b = $a; Code:
  • 24. Zvals: PHP 5 • Zval = Type + Value + Refcount value (complex) refcount = 1 ty zval * zval $a: Complex data structure: string, array, object $a = []; $b = $a; Code:
  • 25. Zvals: PHP 5 • Zval = Type + Value + Refcount value (complex) refcount = 2 ty zval * zval $a: Complex data structure: string, array, object $a = []; $b = $a; Code: zval *$b:
  • 26. Zvals: PHP 7 • Zval = Type + Value value (complex) ty zval * zval $a: $a = []; $b = $a; Code: zval *$b: Complex data structure: string, array, object refcount = 2
  • 27. Zvals: PHP 7 • Zval = Type + Value zval * zval $a: $a = []; $b = $a; Code: zval *$b: Complex data structure: string, array, object refcount = 2 value (complex) type_info
  • 28. value (complex) type_info value (complex) type_info Zvals: PHP 7 • Zval = Type + Value $a: Complex data structure: string, array, object $b: refcount = 2 zval zval
  • 29. PHP 5 PHP 7 value (simple): null, bool, int, float refcount ty gc_buffer zval * value (simple): … type_info 1 allocations 1 level of indirection 40 bytes no allocations no indirection 16 bytes
  • 30. PHP 5 PHP 7 value (complex) refcount ty gc_root zval * Complex data structure: string, array, object value (complex) type_info Complex data structure: string, array, object refcount gc_info 2 allocations 2 levels of indirection 40 bytes 1 allocation 1 level of indirection 24 bytes
  • 31. val (char *) len (int) A B C 0 zval value: PHP 5
  • 32. zend_string * refcount gc_info hash len (size_t) A B C 0 val (char *) len (int) A B C 0 zval value: zval value: PHP 5 PHP 7
  • 33. [0]: (empty) [1]: (empty) [2]: (empty) [3]: (empty) hash() “xyz” Arrays: PHP 5
  • 34. [0]: (empty) [1]: [2]: (empty) [3]: (empty) hash() “xyz” data key “xyz” Arrays: PHP 5
  • 35. [0]: (empty) [1]: [2]: (empty) [3]: “foo” data key “foo” hash() “xyz” data key “xyz” Arrays: PHP 5
  • 36. [0]: (empty) [1]: [2]: (empty) [3]: “foo” data key “foo” hash() “xyz” data key “xyz” Arrays: PHP 5 “bar”
  • 37. [0]: (empty) [1]: [2]: (empty) [3]: “foo” data key “foo” next data key “bar” next = NULL hash() “bar” “xyz” data key “xyz” next = NULL Arrays: PHP 5
  • 38. [0]: (empty) [1]: [2]: (empty) [3]: “foo” hash() “bar” “xyz” Arrays: PHP 5 head tail data key “foo” next data key “bar” next = NULL data key “xyz” next = NULL
  • 39. [0]: (empty) [1]: [2]: (empty) [3]: “foo” hash() “bar” “xyz” Arrays: PHP 7 data key “foo” next data key “bar” next = NULL data key “xyz” next = NULL head tail
  • 40. [0]: (empty) [1]: [2]: (empty) [3]: “foo” hash() “bar” “xyz” Arrays: PHP 7 data key “foo” next data key “bar” next = NULL data key “xyz” next = NULL
  • 41. [0]: (empty) [1]: [2]: (empty) [3]: “foo” hash() “bar” “xyz” Arrays: PHP 7 Bucket * uint32_t data key “foo” next data key “bar” next = NULL data key “xyz” next = NULL
  • 42. [0]: (empty) [1]: [2]: (empty) [3]: hash() “bar” “xyz” Arrays: PHP 7 Bucket * uint32_t data key “foo” next data key “bar” next = NULL data key “xyz” next = NULL
  • 43. [0]: (empty) [1]: [2]: (empty) [3]: hash() “bar” “xyz” Arrays: PHP 7 Bucket * uint32_t TOMBSTONE data key “bar” next = NULL data key “xyz” next = NULL
  • 44. PHP 5 PHP 7 Allocations: 2 + 2 per element 2 + 0 per element Size: 72 + 112 per element 56 + 36 per element Indirections (lookup): 4 2 Arrays: PHP 5 vs. PHP 7 (For arrays with unique values)
  • 45. PHP 5 PHP 7 Allocations: 2 + 2 per element 2 + 0 per element Size: 72 + 112 per element 56 + 36 per element Indirections (lookup): 4 2 Arrays: PHP 5 vs. PHP 7 (For arrays with unique values)
  • 46. Immutable arrays $arrays = []; for ($i = 0; $i < 1000000; ++$i) { $arrays[] = [1, 2, 3, 4, 5, 6, 7, 8]; }
  • 47. Immutable arrays $arrays = []; for ($i = 0; $i < 1000000; ++$i) { $arrays[] = [1, 2, 3, 4, 5, 6, 7, 8]; } PHP 5 Memory usage 1336 MB Execution time (create) 0.77 s Execution time (destroy) 1.45 s
  • 48. Immutable arrays $arrays = []; for ($i = 0; $i < 1000000; ++$i) { $arrays[] = [1, 2, 3, 4, 5, 6, 7, 8]; } PHP 5 PHP 7 Memory usage 1336 MB 391 MB Execution time (create) 0.77 s 0.29 s Execution time (destroy) 1.45 s 0.05 s
  • 49. Immutable arrays $arrays = []; for ($i = 0; $i < 1000000; ++$i) { $arrays[] = [1, 2, 3, 4, 5, 6, 7, 8]; } PHP 5 PHP 7 PHP 7 with opcache Memory usage 1336 MB 391 MB 32 MB Execution time (create) 0.77 s 0.29 s 0.03 s Execution time (destroy) 1.45 s 0.05 s 0.002 s
  • 51. Objects zval object store bucket actual object properties table PHP 5 property value
  • 52. Objects zval object store bucket actual object properties table zval actual object properties table (including values) + PHP 5 PHP 7 property value
  • 53. PHP 5 PHP 7 Allocations: 2 + 1 per property 1 + 0 per property Size: 96 + 40 per property 48 + 16 per property Indirections (prop val): 4 1 Objects: PHP 5 vs. PHP 7 (For objects with unique properties)
  • 56. AST <?php $a = 42; $b = 24; echo $a + $b;
  • 57. AST <?php $a = 42; $b = 24; echo $a + $b; <?php T_OPEN_TAG $a T_VARIABLE = 42 T_LNUMBER ; $b T_VARIABLE = 24 T_LNUMBER ; echo T_ECHO $a T_VARIABLE + $b T_VARIABLE ; lex
  • 58. AST <?php $a = 42; $b = 24; echo $a + $b; <?php T_OPEN_TAG $a T_VARIABLE = 42 T_LNUMBER ; $b T_VARIABLE = 24 T_LNUMBER ; echo T_ECHO $a T_VARIABLE + $b T_VARIABLE ; ASSIGN $a 42 ASSIGN $b 24 ADD $a $b ~2 ECHO ~2 RETURN 1 lex parse + compile
  • 59. AST <?php $a = 42; $b = 24; echo $a + $b; <?php T_OPEN_TAG $a T_VARIABLE = 42 T_LNUMBER ; $b T_VARIABLE = 24 T_LNUMBER ; echo T_ECHO $a T_VARIABLE + $b T_VARIABLE ; ASSIGN $a 42 ASSIGN $b 24 ADD $a $b ~2 ECHO ~2 RETURN 1 lex parse compile stmts assign var 42 $a assign $b echo + $a $b var 24
  • 60. AST <?php $a = 42; $b = 24; echo $a + $b; <?php T_OPEN_TAG $a T_VARIABLE = 42 T_LNUMBER ; $b T_VARIABLE = 24 T_LNUMBER ; echo T_ECHO $a T_VARIABLE + $b T_VARIABLE ; ASSIGN $a 42 ASSIGN $b 24 ADD $a $b ~0 ECHO ~0 RETURN 1 lex parse compile token_get_all() nikic/php-ast phpdbg -p stmts assign var 42 $a assign $b echo + $a $b var 24
  • 62. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2);
  • 63. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2); SEND_VAL 1 SEND_VAL 2 DO_FCALL foo RECV $a RECV $b ADD $a $b ~0 RETURN ~0 {main} foo()
  • 64. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2); SEND_VAL 1 SEND_VAL 2 DO_FCALL foo RECV $a RECV $b ADD $a $b ~0 RETURN ~0 {main} foo() … VM stack
  • 65. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2); SEND_VAL 1 SEND_VAL 2 DO_FCALL foo RECV $a RECV $b ADD $a $b ~0 RETURN ~0 {main} foo() … int(1) int(2) VM stack arg[0] arg[1]
  • 66. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2); SEND_VAL 1 SEND_VAL 2 DO_FCALL foo RECV $a RECV $b ADD $a $b ~0 RETURN ~0 {main} foo() … int(1) int(2) execute_data VM stack arg[0] arg[1] ~0 $a $b
  • 67. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2); SEND_VAL 1 SEND_VAL 2 DO_FCALL foo RECV $a RECV $b ADD $a $b ~0 RETURN ~0 {main} foo() … int(1) int(2) execute_data int(1) int(2) VM stack arg[0] arg[1] ~0 $a $b
  • 68. Virtual machine – stack management (PHP 5) function foo($a, $b) { return $a + $b; } foo(1, 2); SEND_VAL 1 SEND_VAL 2 DO_FCALL foo RECV $a RECV $b ADD $a $b ~0 RETURN ~0 {main} foo() … int(1) int(2) int(3) execute_data int(1) int(2) VM stack arg[0] arg[1] ~0 $a $b
  • 69. Virtual machine – stack management (PHP 7) function foo($a, $b) { return $a + $b; } foo(1, 2); {main} foo() … VM stack INIT_FCALL foo SEND_VAL 1 SEND_VAL 2 DO_FCALL RECV $a RECV $b ADD $a $b ~0 RETURN ~0
  • 70. Virtual machine – stack management (PHP 7) function foo($a, $b) { return $a + $b; } foo(1, 2); {main} foo() … execute_data VM stack ~0 $a $b INIT_FCALL foo SEND_VAL 1 SEND_VAL 2 DO_FCALL RECV $a RECV $b ADD $a $b ~0 RETURN ~0
  • 71. Virtual machine – stack management (PHP 7) function foo($a, $b) { return $a + $b; } foo(1, 2); {main} foo() … execute_data int(1) int(2) VM stack ~0 $a $b INIT_FCALL foo SEND_VAL 1 SEND_VAL 2 DO_FCALL RECV $a RECV $b ADD $a $b ~0 RETURN ~0
  • 72. Virtual machine – stack management (PHP 7) function foo($a, $b) { return $a + $b; } foo(1, 2); {main} foo() … execute_data int(1) int(2) VM stack ~0 $a $b INIT_FCALL foo SEND_VAL 1 SEND_VAL 2 DO_FCALL RECV $a RECV $b ADD $a $b ~0 RETURN ~0
  • 73. Virtual machine – stack management (PHP 7) function foo($a, $b) { return $a + $b; } foo(1, 2); {main} foo() … execute_data int(1) int(2) int(3) VM stack ~0 $a $b INIT_FCALL foo SEND_VAL 1 SEND_VAL 2 DO_FCALL RECV $a RECV $b ADD $a $b ~0 RETURN ~0
  • 74. Virtual machine – inlined internal functions • Functions with custom opcodes: • strlen() • is_*() • defined() • call_user_func() • call_user_func_array()
  • 75. Virtual machine – inlined internal functions • Functions with custom opcodes: • strlen() • is_*() • defined() • call_user_func() • call_user_func_array() • Only in global scope or for fully qualified calls namespace foo; echo strlen($str);
  • 76. Virtual machine – inlined internal functions • Functions with custom opcodes: • strlen() • is_*() • defined() • call_user_func() • call_user_func_array() • Only in global scope or for fully qualified calls namespace foo; echo strlen($str); Is this strlen() or foostrlen()?
  • 77. Virtual machine – inlined internal functions • Functions with custom opcodes: • strlen() • is_*() • defined() • call_user_func() • call_user_func_array() • Only in global scope or for fully qualified calls namespace foo; echo strlen($str); This is definitely strlen()!
  • 78. Virtual machine – global registers zend_execute_data* execute_data; const zend_op* opline;
  • 79. Virtual machine – global registers register zend_execute_data* execute_data __asm__("%r14"); register const zend_op* opline __asm__("%r15");
  • 80. Virtual machine – global registers register zend_execute_data* execute_data __asm__("%r14"); register const zend_op* opline __asm__("%r15"); • Registers reserved in VM code
  • 81. Virtual machine – global registers register zend_execute_data* execute_data __asm__("%r14"); register const zend_op* opline __asm__("%r15"); • Registers reserved in VM code • Avoid reloading from executor_globals / execute_data
  • 82. Virtual machine – global registers register zend_execute_data* execute_data __asm__("%r14"); register const zend_op* opline __asm__("%r15"); • Registers reserved in VM code • Avoid reloading from executor_globals / execute_data • Avoid save/restore during calls