Upload
petra-barus
View
3.689
Download
0
Embed Size (px)
Citation preview
What’s New In PHP7Petra Novandi Barus
Chief Technology OfficerUrbanIndo.com
Contact Me!email: [email protected] twitter: @petrabarustelegram: petrabarusgithub: petrabarus
Deep insight Laravel, Magento, dan PHP720th February 2016
D.Lab, Jakarta
Who Am I?
- Chief Technology Officer, UrbanIndo.com- Master of Informatics Engineering ITB- Have been using PHP since 2003- Writes Yii2 libraries - Interests: Distributed systems, High
performance computing, Programming- Hobby: Sleeping, reading books
I really love reading about technologies and share the articles on my Twitter!
Follow my reading list at @petrabarus Let’s learn stuffs together!
What Am I Going To Talk About?
What’s New In PHP7?
What’s New In PHP7 (Internally)?What makes PHP7 much faster and
what can I learn from that?
How do the new PHP7 helps me to code?
So, what’s new in PHP7?
1. Scalar Type Hints
1. Scalar Type Hints Reasons
Type safety Detect bugs before run time Static analysis More robust code
Weak versus Strong Weak: coercive, will cast on different type Strong: strict, will throw TypeError.
2. Return Type Declaration
Similar reason with Type Hinting
3. Combined Comparison Operator (spaceship)
PHP 5PHP 7
3. Combined Comparison Operator (spaceship)
PHP 5
PHP 7
4. Null Coalesce Operator (??)
5. Anonymous Class Creates an object of a class without defining the class. Pass the value of outer class / object using the
constructor.
5. Anonymous Class
5. Anonymous Class
PHP 5
PHP 7
5. Anonymous Class
5. Anonymous Class Usage
Creating a very simple class, one time use. Quickly override one or few methods Implements a specific usage of an interface without defining new class (logger, event observer).
Mocking a class on the fly for testing purposes.
6. PerformanceAccording to HHVM…..
6. PerformanceAccording to Zend…..
What makes PHP 7 faster than PHP 5?
Reference:http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally-php-barcelona-2015http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally http://www.slideshare.net/jpauli/php-7-performances-from-php-5
CPU Cache & Memory Architecture
Size LatencyL1 Cache 32kB 1nsL2 Cache 256kB 4nsL3 Cache 4000kB 12nsMemory 2000+MB 100ns
A bit intro to low level optimization by utilizing CPU cache and memory architecture
CPU Cache & Memory Architecture
Much faster because using CPU cache
Consider these C codes
CPU Cache & Memory Architecture
Reference:https://software.intel.com/en-us/articles/how-to-use-loop-blocking-to-optimize-memory-use-on-32-bit-intel-architecture
Memory Optimization Reduce the number of allocations
PHP5 spends 20% of CPU time in allocator
Memory
Memory Optimization Reduce Memory Usage
Memory access has high latency Less data => more fits to cache
Memory Optimization Reduce Indirection
Remove the number of pointers use
Zval Zval is how PHP stores the variables (boolean, integer,
string, array). Zval is short of Zend Value. In PHP 7, Zval is optimized.
Less allocation Less indirection Less memory usage
Zval
PHP 5 PHP 7
ZvalPHP5- Zvals always
require heap allocation
- Zvals always reference counted and have cycle collection
PHP7- Zvals are no longer
heap-allocated and no ref count.
- Ref count in the complex value
- No double refcount. - Complex type can be
shared
ZVal PHP5
ZValPHP7
More Data Structure Optimization HashTable (array)
4 pointer indirections & 100~ byte memory to 2 pointer indirections & 32 byte memory.
String management Refcounted
AST PHP 7 introduces Abstract Syntax Tree Instead of single pass parse-compile, PHP7 will do
syntax analysis and then parse
ASTPHP5
PHP7
AST
PHP5
AST
PHP7
2. AST Well designed and well maintain Better syntax checking and consistency Slower to run but produce much better runtime
opcode Use opcode cache for this
Breaks backward compatibility
Reference http://
www.slideshare.net/colinodell/php-7-crash-course-phpworld-2015
http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally-php-barcelona-2015
http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally
https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html
Conclusion PHP7 is way better than PHP5
Much faster Use less memory Produce robust code (Check your backward compatibility due to AST change)
You should use PHP7!
Questions?