the secret of php7's performance

138
The Secret of PHP7’s Performance @ Laru ence

Upload: -

Post on 15-Jan-2017

2.778 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: The secret of PHP7's Performance

The Secret of PHP7’s Performance@Laruence

Page 2: The secret of PHP7's Performance

SELF INTRODUCTION

‣ Author of Yaf, Yar, Yac, Yaconf, Taint Projects

‣ Maintainer of Opcache, Msgpack, PHP-Lua Projects

‣ PHP core developer since 2011

‣ Zend consultant since 2013

‣ PHP7 core developer

‣ Chief software architect at lianjia since 2015

Page 3: The secret of PHP7's Performance

PHP BRIEF INTRO

‣ Created in 1994 by Rasmus Lerdorf

‣ 20+ years programming language

‣ Most popular web service program language

‣ PHP7 is released at 3 Dec 2015

‣ Latest version is PHP7.0.7

Page 4: The secret of PHP7's Performance

PHP7

‣ Improved Performance: PHP 7 is up to twice as fast as PHP 5.6

‣ Significantly reduced memory usage

‣ Abstract syntax tree

‣ Consistent 64-bit support

‣ Improved exception hierarchy

‣ Many fatal errrors converted to exceptions

‣ The null coalescing operator (??)

‣ Return & Scalar type declarations

‣ Anonymous slasses

‣ ….

Page 5: The secret of PHP7's Performance

PHP7

‣ 100 % performance improved in various apps

‣ Which optimization is most responsible?

‣ …………………………………………………..

Page 6: The secret of PHP7's Performance

JUST-IN-TIME COMPILER

‣ Once upon a time

‣ There comes HHVM

‣ Performance really matters

‣ A secret project in Zend

‣ Based on opcache of PHP5.5

‣ Invisible performance change in wordpress

‣ Why?

https://github.com/zendtech/php-src/tree/zend-jit

Page 7: The secret of PHP7's Performance

WORDPRESS PROFILING (PHP5.5)

‣ Wordpress:

‣ Typical PHP real-life application

‣ Callgrind:

‣ 28% CPU time is spent on Zend VM

‣ 25% CPU time is spent on Memory

‣ Top one is _zend_mm_alloct_int

Callgrind result on wordpress home page

Page 8: The secret of PHP7's Performance

WORDPRESS PROFILING (PHP5.5)

‣ We have too many allocations

‣ Thoughts:

‣ _strndup

‣ HashTable

‣ MAKE_STD_ZVAL

__mm_alloc_init callers graph (part)

Page 9: The secret of PHP7's Performance

`MEMORY` IS THE KEY

‣ `Memory` is the bootle-neck(25%) ‣ High memory usage ‣ High cache misses

‣ High TLB misses

‣ High page faults

‣ Too many allocation ‣ More CPU time

‣ Increase iTLB miss

‣ Increase branch-miss

‣ High level memory indirection ‣ Increase cache misses

Cache hierarchy latency

Page 10: The secret of PHP7's Performance

INSPECT ZVAL

‣ Total 24 bytes

‣ Value uses 16 bytes

‣ Thoughts: ‣ Most types use 8 bytes

‣ String uses 12 bytes

‣ Only Object uses 16 bytes

‣ Only a little types are ref

630

BOOL/LONG/DOUBLE/ARRAY/RES

refcount type

0

1

2is_ref

630

STRING

refcount type

0

1

2is_ref

LEN

630

OBJECT

refcount type

0

1

2is_ref

Scalar types

String Object

Page 11: The secret of PHP7's Performance

INSPECT ZVAL

‣ Not only 24 bytes ‣ GC info(for GC): Added 16 bytes

‣ Block info(for MM): Added 8 bytes

‣ Total 48 bytes

‣ Thoughts: ‣ Only array and object need gc info

‣ Block info?

‣ Stack allocating?

‣ New MM?

ZVAL size

630

zvalue_value

refcount type

0

1

2

is_ref

block_info

3

4

Page 12: The secret of PHP7's Performance

PROFILING WP

‣ String is the most used type

‣ Object is only used in 2%

‣ 40% types only used 8 bytes in zval.value

‣ Only 15% types are GC cared

‣ ~10% is reference type

‣ Thoughts: ‣ String needs to be optimized

‣ We don’t needs unified `zval`

‣ Reducing zval’s size should be possible

NULL 2798 4%

Bool 11894 17%

Double 6 ..

Long 4134 6%

Resource 25 ..

Array 8709 13%

Object 1582 2%

String 37564 56%

Types in one WP lifecycle

Page 13: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

Page 14: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_LONG

Page 15: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_LONG Can be kept in 64 bytes?

Page 16: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_LONG

Page 17: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_LONG

Page 18: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

Page 19: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_STRING

Page 20: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_STRING Can be kept in 64 bytes?

Page 21: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

value

type u2flags

0 8 32 63

IS_STRING

Page 22: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

0 32 6348

IS_STRING/IS_ARRAY/IS_OBJECT/IS_RESROUCE/...

refcount gc_infotype flags

value

type u2flags

0 8 32 63

IS_STRING

Page 23: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

0 32 6348

IS_STRING/IS_ARRAY/IS_OBJECT/IS_RESROUCE/...

refcount gc_infotype flags

value

type u2flags

0 8 32 63

IS_STRING

Page 24: The secret of PHP7's Performance

BRAND NEW ZVAL

‣ Total 16 bytes

‣ Copy instead of refcount for basic types

‣ Refcount is not against zval anymore

‣ External struct is used for complex types

‣ values can not be stored in size_t mem

‣ refcount

‣ gc_info

‣ value flags

ZVAL in PHP7

0 32 6348

IS_STRING/IS_ARRAY/IS_OBJECT/IS_RESROUCE/...

refcount gc_infotype flags

value

type u2flags

0 8 32 63

IS_STRING

Address

Page 25: The secret of PHP7's Performance

ZEND STRING

‣ Most used type in real world

‣ PHP5 ‣ C string

‣ int length

‣ Hash value needs to be calculated every time

‣ Interned string is distinguished by address

‣ PHP7 ‣ Brand new type: zend_string

‣ Size length

‣ Hash value is kept after being calculated

‣ Interned string is distinguished by flags

‣ COW instead of copying

zend_string

0 32 6348

unsigned long hash_value

refcount gc_infotype flags

size_t string_len

char[1] val

Page 26: The secret of PHP7's Performance

INSPECT HASHTABLE

‣ Total 72 bytes

‣ typeof bucket->pData is void **

‣ Thoughts:

‣ In most cases, zval are stored

‣ Reduce memory usage

‣ Reduce memory indirection ‣ pListNext ‣ HashTable -> Bucket

‣ Bucket -> ZVAL ** (void **)

HashTable struct

HashTablenTableMasknTableSize

0 8 32 63

pInternalPointer

nNumOfElemnNextFreeElement

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Buckethashval

nKeyLength

pDataPtrpData

pListNextpListPrev

pNextpLastarKey

Buckethashval

nKeyLength

pDataPtrpData(void **)

pListNextpListPrev

pNextpLastarKey

zval

zval**

Page 27: The secret of PHP7's Performance

ZEND ARRAY

‣ Total 56 bytes

‣ Key is zend_string

‣ Less memory indirection

‣ Bucket.val

‣ Bucket.val.zval

‣ Buckets are allocated together

zend_array struct

zend_arrayrefcounted

0 8 32 63

uarData

idxidx

Bucket 0nTableMask

nNumUsed nNumOfElemnTableSize InternalPointer

pNextFreeElementpDestructor

.......

hashvalkey

zval

Bucket 1hashval

key

zval

Bucket 2 ......

Page 28: The secret of PHP7's Performance

ILLUSTRATION PHP5

Page 29: The secret of PHP7's Performance

ILLUSTRATION PHP5

$a = 5

Page 30: The secret of PHP7's Performance

Symbol Table$azval **

ILLUSTRATION PHP5

$a = 5

Page 31: The secret of PHP7's Performance

Symbol Table$azval **

ILLUSTRATION PHP5

$a = 5

Page 32: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5

Page 33: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5

ref = 1

Page 34: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5

ref = 1

$b = $a

Page 35: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5

ref = 1

$b = $a$b

Page 36: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5

ref = 1

$b = $a$b

Page 37: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

Page 38: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

Page 39: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

Page 40: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

Page 41: The secret of PHP7's Performance

Symbol Table$azval **

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

Page 42: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

Page 43: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

ref = 1

Page 44: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

ref = 1

$n = $m

Page 45: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

ref = 1

$n = $m

$n

Page 46: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

ref = 1

$n = $m

$n

Page 47: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

Page 48: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

ref = 2

Page 49: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

ref = 2$l = &$n

Page 50: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

ref = 2$l = &$n

$l

Page 51: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

ref = 2$l = &$n

$l

Page 52: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

ref = 2$l = &$n

$l

0x7ffff7fd18e0

S 14

0x7ffff7fd18e0

Page 53: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

$l = &$n

$l

0x7ffff7fd18e0

S 14

0x7ffff7fd18e0

Page 54: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

$l = &$n

$l

0x7ffff7fd18e0

S 14

0x7ffff7fd18e0

Page 55: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

$l = &$n

$l

0x7ffff7fd18e0

S 14

0x7ffff7fd18e0

Page 56: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

$l = &$n

$l

0x7ffff7fd18e0

S 14

0x7ffff7fd18e0

ref = 1

Page 57: The secret of PHP7's Performance

Symbol Table$azval **

0x7ffff7fd1848

S 04

0x7ffff7fd1848

5

L 0

ILLUSTRATION PHP5

$a = 5$b = $a

$b

ref = 2

$m = “PHP5”

$m

$n = $m

$n

$l = &$n

$l

0x7ffff7fd18e0

S 14

0x7ffff7fd18e0

ref = 1 ref = 2

Page 58: The secret of PHP7's Performance

ILLUSTRATION PHP7

Page 59: The secret of PHP7's Performance

ILLUSTRATION PHP7

$a = 7

Page 60: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7

Page 61: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

Page 62: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b

Page 63: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7

Page 64: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

Page 65: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”

Page 66: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m

Page 67: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

Page 68: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

Page 69: The secret of PHP7's Performance

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

Page 70: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

Page 71: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

ref=1

Page 72: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

ref=1

$n = $m

Page 73: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

ref=1

$n = $m

$n

Page 74: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

ref=1

$n = $m

$n0x7ffff7fd18e0

Page 75: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

ref=1

$n = $m

$n0x7ffff7fd18e0

S

Page 76: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S

ref=1

$n = $m

$n0x7ffff7fd18e0

S

Page 77: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

Page 78: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

Page 79: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

Page 80: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l

Page 81: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l0x7ffff7fd1000

Page 82: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l0x7ffff7fd1000

R

Page 83: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l0x7ffff7fd1000

R

Page 84: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l0x7ffff7fd1000

R

Page 85: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l0x7ffff7fd1000

R

Page 86: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

S

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

Page 87: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

Page 88: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n0x7ffff7fd18e0

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

R

Page 89: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

R

Page 90: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

R

0x7ffff7fd1000

Page 91: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

R

0x7ffff7fd1000

Page 92: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=1

R

0x7ffff7fd1000

Page 93: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n

ref=2

$l = &$n

$l0x7ffff7fd1000

RR

0x7ffff7fd1000

Page 94: The secret of PHP7's Performance

hash = 3245185675147665086

S

4

'P', 'H', 'P', '\0'

0x7ffff7fd18e0zend_string

R

0x7ffff7fd18e0

S 14

zend_reference

Symbol Table$a

7

Lzval

ILLUSTRATION PHP7

$a = 7$b = $a

$b7L

$m = “PHP7”$m0x7ffff7fd18e0

S$n = $m

$n

ref=2

$l = &$n

$l0x7ffff7fd1000

R

ref=2

R

0x7ffff7fd1000

Page 95: The secret of PHP7's Performance

ILLUSTRATION PHP5

Page 96: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

Page 97: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

Page 98: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

Page 99: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

Page 100: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

Page 101: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **

Page 102: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **

zval *

Page 103: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *

Page 104: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *

0

Page 105: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0

Page 106: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0

Page 107: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0 zval **

Page 108: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0 zval **

zval *

Page 109: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0 zval **zval *

Page 110: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0 zval **zval *

1

Page 111: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0 zval **zval *1

Page 112: The secret of PHP7's Performance

ILLUSTRATION PHP5

$arr = range(0, 5) foreach($arr as $val) { }

HashTable78

65

Bucket *

pListHeadpListTail

arBucketspDestructor

Bucket *Bucket *

.......

Bucket00

pData

pListNext

arKey = NULL

Bucket10

pData

pListNext

arKey = NULL

Bucket20

pData

pListNext

arKey = NULL

zval **zval *0 zval **zval *1

Page 113: The secret of PHP7's Performance

ILLUSTRATION PHP7

Page 114: The secret of PHP7's Performance

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 115: The secret of PHP7's Performance

refcount = 1 0H 0

7NaN

Bucket *arData

8 8

Zend Array

0zval(0)

L

1zval(1)

L

2zval(2)

L

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 116: The secret of PHP7's Performance

refcount = 1 0H 0

7NaN

Bucket *arData

8 8

Zend Array

0zval(0)

L

1zval(1)

L

2zval(2)

L

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 117: The secret of PHP7's Performance

refcount = 1 0H 0

7NaN

Bucket *arData

8 8

Zend Array

0zval(0)

L

1zval(1)

L

2zval(2)

L

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 118: The secret of PHP7's Performance

refcount = 1 0H 0

7NaN

Bucket *arData

8 8

Zend Array

0zval(0)

L

1zval(1)

L

2zval(2)

L

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 119: The secret of PHP7's Performance

refcount = 1 0H 0

7NaN

Bucket *arData

8 8

Zend Array

0zval(0)

L

1zval(1)

L

2zval(2)

L

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 120: The secret of PHP7's Performance

refcount = 1 0H 0

7NaN

Bucket *arData

8 8

Zend Array

0zval(0)

L

1zval(1)

L

2zval(2)

L

ILLUSTRATION PHP7

$arr = range(0, 7) foreach($arr as $val) { }

Page 121: The secret of PHP7's Performance

THERE COMES TROUBLES

Page 122: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

Page 123: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

Page 124: The secret of PHP7's Performance

Symbol Table$a $bzval **

Compiled vars0($a) "a"($b)

1

L 0

zval **

zval

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

Page 125: The secret of PHP7's Performance

Symbol Table$a $bzval **

Compiled vars0($a) "a"($b)

1

L 0

zval **

zval

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Page 126: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Page 127: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Symbol Table$a

1

L

$b"a"

Szval

Compiled vars0($a)

1

L

1($b)"a"

Szval

Page 128: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Symbol Table$a

1

L

$b"a"

Szval

Compiled vars0($a)

1

L

1($b)"a"

Szval

0xfffeee00

Page 129: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Symbol Table$a

1

L

$b"a"

Szval

Compiled vars0($a)

1

L

1($b)"a"

Szval

0xfffeee00

Page 130: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Symbol Table$a

1

L

$b"a"

Szval

Compiled vars0($a)

1

L

1($b)"a"

Szval

0xfffeee00

INDIRECT

Page 131: The secret of PHP7's Performance

THERE COMES TROUBLES

function func() { $a = 1; $b = "a"; $$b = 2; //build symbol table var_dump($a); }

This is not a problem in PHP5

But this is a problem now

Symbol Table$a

1

L

$b"a"

Szval

Compiled vars0($a)

1

L

1($b)"a"

Szval

0xfffeee00

INDIRECT

This is why IS_INDRECT was born

Page 132: The secret of PHP7's Performance

‣ 2M(4M) Page Size ‣ Not swappable

‣ Reduce TLB misses ‣ 64 * 4k = 256K

‣ 8 * 2M = 16M

‣ size php binary(o2) text size ~= 10M

‣ opcache.huge_code_pages(iTLB)

‣ shared memory(dTLB)

‣ regular memory(dTLB)

‣ Hugepage is not always good:

‣ SIGBUS on OOM after fork

‣ Hugepage on NUMA

Huge Pages

Translation Lookaside Buffer

VAddress TLB

0x7fff0100 0xbee100

0x7fffe100 0xbee200

0x7fff0000 0xbee300

VAddress PAddress

L2 TLB

0x7fff0100 0xbee100

0x7fffe100 0xbee200

0x7fff0000 0xbee300

VAddress PAddress

RAM

Page Table

PAddress

MISS MISS

Limited size

>60ns1ns 4ns

Wordpresss homepage 100 runs PHP5.5 iTLB stat PHP7 (with huge_code_page) iTLB stat

Page 133: The secret of PHP7's Performance

‣ New memory manager

‣ Memory is allocated in pages

‣ Pages are fixed sizes in one chunk

‣ Chunk is 2M aligned

‣ Block info is unnecessary anymore:

‣ Chunk = Address & ~(2M - 1)

‣ Page = Address & (2M - 1)

‣ efree_size

‣ Similar size mem are probably allocated nearly

PHP7 MM

PHP7 memory manager

Chunk

Heap

prev

huge_list

main_chunk

free_slot

next

free_pages

free_map

pages

map

Chunkprev

next

free_pages

free_map

pages

map

FRUN

LRUN > 3072

SRUN

8

16

24

32

40

...

512

....

3072

Huge >~2M

0 1 2 29

2M Aligned

Page 134: The secret of PHP7's Performance

‣ Basically

‣ Memory is reduced almost by half

‣ Cache misses is significant reduced

‣ TLB misses is significant reduced

‣ Memory indirection is significant reduced

`MEMORY` IS THE KEY

Page 135: The secret of PHP7's Performance

‣ Zend VM refactor

‣ Supper global registers

‣ Huge Pages

‣ File based opcache ‣ No refcount for scalar types

‣ Function calling convention improved

‣ zvals are always pre-allcocated or allocated in stack(no more MAKE_STD_ZVAL and ALLOC_ZVAL)

‣ Faster string comparing also

‣ New HashTable iteration AP

‣ Array duplication optimization

‣ PGO supported

‣ Reference-counting instead of copying

‣ call_user_function(_array) => ZEND_INIT_USER_CALL

‣ Is_int/string/array/* etc => ZEND_TYPE_CHECK

‣ strlen => ZEND_STRLEN

‣ defined => ZEND+DEFINED

‣ Faster sorting algo

‣ Immutable array

‣ Fast arguments parsing API

‣ Optimized strings concatenation.

‣ ……… ‣ ……..

NOT ONLY, BUT ALSO (TL;DR)

Page 136: The secret of PHP7's Performance

PHP7 PROFILING

‣ 100% performance increased

‣ 60% IR reduced

‣ 40% memory usage reduced

‣ 20% branches reduced

‣ 15% iTLB misse reduced

‣ What a great life :)

Page 137: The secret of PHP7's Performance

PHP7 PERFORMANCE NEXT

‣ PHP 7.1

‣ DFA optimization

‣ Type inference

‣ Type specific opcode handlers

‣ Faster static vars binding

‣ Dozens small improvements

‣ 30% performance improvement in bench.php already

‣ Significant performance improvement in reallife application

‣ Alpha will be released in July 2016

Page 138: The secret of PHP7's Performance

Q&A