Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP7 - The New Engine for old good train

30,069 views

Published on

PHP7 's New

Published in: Technology

PHP7 - The New Engine for old good train

  1. 1. PHP 7 - New Engine For The Good Old Train @laruence
  2. 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. 3. About PHP •  20 years history •  Most popular Web service program language •  Over 82% sites are use PHP as server program language
  4. 4. PHP 7 New Features
  5. 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. 6. Abstract Syntax Tree PHP Parser AST Opcodes Execution PHP Parser Opcodes Execution •  PHP5 •  PHP7
  7. 7. Int64 Improvement •  >2GB string •  >2GB file uploading •  Fully 64bits intgers cross platforms
  8. 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. 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. 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. 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. 12. PHP NG (Next Generation)
  13. 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. 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. 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. 16. •  Generate optimized codes based on run-time types inference PHP Just In Time Compiler? PHP Parser Opcodes TypeInf JIT Bytecodes Execution
  17. 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
  18. 18. Wordpress profile
  19. 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. 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
  21. 21. ZVAL •  Zval is changed
  22. 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. 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. 24. •  IS_NULL •  IS_FALSE •  IS_TRUE •  IS_LONG •  IS_DOUBLE ZVAL NON REFCOUNED value type u2flags 0 8 32 63
  25. 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. 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. 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. 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. 29. •  New Internal Type: Zend Reference •  Reference is type IS_REFERENCE refco0nt gc_infot2pe 0 3   63 flags 48 3val val0e
  30. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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 •  ….
  40. 40. Wordpress profile (2015-04-14)
  41. 41. Wordpress profiled (2015-04-14) •  >50% CPU IRs reduced •  5% CPU time in meory manager •  12% CPU time in hash tables operations
  42. 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. 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. 44. PHP7 Performance (By Rasmus 2015-04-21)
  45. 45. PHP7 Performance (2015-04-21)
  46. 46. PHP7 Performance (2015-04-21)
  47. 47. PHP7 Performance (2015-03-15)
  48. 48. Always Do Your Own Benchmark
  49. 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. 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#/
  51. 51. Questions? Thanks

×