+ All Categories
Home > Documents > PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct...

PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct...

Date post: 24-Jun-2020
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
51
PHP 7 - New Engine For The Good Old Train @laruence
Transcript
Page 1: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP 7 - New Engine For The Good Old Train

@laruence

Page 2: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 3: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

About PHP

•  20 years history

•  Most popular Web service program language

•  Over 82% sites are use PHP as server program language

Page 4: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP 7 New Features

Page 5: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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…

Page 6: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Abstract Syntax Tree

PHP Parser AST Opcodes Execution

PHP Parser Opcodes Execution

•  PHP5

•  PHP7

Page 7: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Int64 Improvement

•  >2GB string

•  >2GB file uploading

•  Fully 64bits intgers cross platforms

Page 8: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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']()

Page 9: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 10: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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) {}

}

Page 11: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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()

Page 12: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP NG (Next Generation)

Page 13: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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)

Page 14: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 15: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 16: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  Generate optimized codes based on run-time types inference

PHP Just In Time Compiler?

PHP Parser Opcodes

TypeInf JIT Bytecodes

Execution

Page 17: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 18: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Wordpress profile

Page 19: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  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

Page 20: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 21: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

ZVAL

•  Zval is changed

Page 22: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

ZVAL

•  No refcount for scalar types

•  zval are always pre-allcocated or allocated in stack

•  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 VMexecute data

Internal Functions

zval zval zval zval zval

Uesr landzend array zval zval zval zval zval

stack zvals zval zval zval zval zval

Page 23: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  IS_UNDEF

•  IS_NULL

•  IS_FALSE

•  IS_TRUE

•  IS_LONG

•  IS_DOUBLE

•  IS_STRING

•  IS_ARRAY

•  IS_OBJECT

•  IS_RESOURCE

•  IS_REFERENCE

ZVAL

•  IS_TYPE_CONSTANT

•  IS_TYPE_REFCOUNTED

•  IS_TYPE_COLLECTABLE

•  IS_TYPE_IMMUTABLE

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

Page 24: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  IS_NULL

•  IS_FALSE

•  IS_TRUE

•  IS_LONG

•  IS_DOUBLE

ZVAL NON REFCOUNED

value

type u2flags

0 8 32 63

Page 25: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  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

Page 26: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  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

Page 27: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  New Internal Type: Zend Array

IS_ARRAY

•  IS_ARRAY_IMMUTABLE

refcount gc_infotype

0 32 63

flags

48

nTableMasku

Bucket *arData

nNumUsed nNumOfElements

.....

Page 28: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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]

Page 29: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

•  New Internal Type: Zend Reference

•  Reference is type

IS_REFERENCE

refco0nt gc_infot2pe

0 3 �  63

flags

48

3val val0e

Page 30: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

HashTable – PHP 5

HashTablenTableMasknTableSize

0 8 32 63

pInternalPointer

nNumOfElemnNextFreeElement

Bucket *

pListHeadpListTailarBuckets

pDestructor

Bucket *Bucket *

.......

Buckethashval

nKeyLength

pDataPtrpData

pListNextpListPrev

pNext

pLastarKey

Buckethashval

nKeyLength

pDataPtrpData

pListNextpListPrev

pNext

pLastarKey

Zvalvaluetype

Page 31: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Zend Array – PHP 7

zend_arrayrefcounted

0 8 32 63

u

arData

idxidx

Bucket 0nTableMask

nNumUsed nNumOfElem

nTableSize InternalPointer

pNextFreeElementpDestructor

.......

hashvalkey

zval

Bucket 1hashval

key

zval

Bucket 2 ......

Page 32: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 33: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 34: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Function calling convention – PHP7

•  function add ($a, $b) {

return $a + $b;

}

add(1, 2); send_val 1

send_val 2

do_fcall 2call 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

Page 35: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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%

Page 36: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

•  …

Page 37: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Faster zend_qsort

•  Refactor zend_qsort for better performance

•  Hybrid Soring 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);

Page 38: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

New Memory Manager

•  Friendly to moder CPU cache

•  less CPU cache misses

•  Faster builtin types allocating

•  ~5% CPU time reduce in wordpress homepage

Page 39: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

•  ….

Page 40: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Wordpress profile (2015-04-14)

Page 41: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Wordpress profiled (2015-04-14)

•  >50% CPU IRs reduced

•  5% CPU time in meory manager

•  12% CPU time in hash tables operations

Page 42: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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)

Page 43: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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

Page 44: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP7 Performance (By Rasmus 2015-04-21)

Page 45: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP7 Performance (2015-04-21)

Page 46: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP7 Performance (2015-04-21)

Page 47: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

PHP7 Performance (2015-03-15)

Page 48: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Always Do Your Own Benchmark

Page 49: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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)

Page 50: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

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#/

Page 51: PHP 7 - gongfuxiang.comPHP 7? • PHP NG – Engine Refactor - performance improvements • Abstarct Syntanx Tree • Int64 Improvement • Uniform variable syntax • Native TLS •

Questions?

Thanks


Recommended