Slide 1

Slide 1 text

PHP OpCode PHP OpCode PHP OpCode What's That? What's That? What's That? Benoit Jacquemont Benoit Jacquemont Benoit Jacquemont @bjacquemont @bjacquemont @bjacquemont

Slide 2

Slide 2 text

What Happen To Our Code At Execution Time?

Slide 3

Slide 3 text

Lexing/Tokenizing Use the Tokenizer Extension

Slide 4

Slide 4 text

Parsing To Abstract Syntax Tree

Slide 5

Slide 5 text

Compilation: AST To Opcode Real value

Slide 6

Slide 6 text

Opcode, Opline And Oparray opcode: instruction to execute opline: instruction with operands (0-2) and optional return assignement oparray: sequence of oplines (function, closure, main) CONCAT (0x08) T2 = CONCAT string("Hello ") CV0 ASSIGN CV0 string("World") T2 = CONCAT string("Hello ") CV0 ECHO T2 RETURN int(1)

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Execution Of The Opcode By The Zend VM

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Opcodes Are The Instructions Of The VM Virtual Processor

Slide 25

Slide 25 text

PHP Version Zend Engine Version Opcodes count 4.x 1 ~130 5.x 2 167 7.x 3 172 8.x 4 207 Source: Zend/zend_vm_opcodes.h

Slide 26

Slide 26 text

Why A VM? Reason #1 Performances!

Slide 27

Slide 27 text

Why A VM? Reason #2 Abstraction Over The Architecture And Hardware

Slide 28

Slide 28 text

Let's Dive Into Let's Dive Into Let's Dive Into Opcode! Opcode! Opcode!

Slide 29

Slide 29 text

How To Display Opcode VLD Extension (from Derick Rethans) (https://pecl.php.net/package/vld) php opcache debug (since PHP 7.1) $ php -dvld.active=1 test.php $ php -d opcache.opt_debug_level=0x10000 test.php

Slide 30

Slide 30 text

Hello World In Opcode! echo "Hello World !"; $_main: L0: ECHO string("Hello World!") L1: RETURN int(1)

Slide 31

Slide 31 text

What About Variables? $msg = "Hello World!"; echo $msg; $_main: L0: ASSIGN CV0($msg) string("Hello World!") L1: ECHO CV0($msg) L2: RETURN int(1)

Slide 32

Slide 32 text

More Complex Variables $a = 1; $b = 2; $result = $a + $b + 3; echo $result; $_main: L0: ASSIGN CV0($a) int(1) L1: ASSIGN CV1($b) int(2) L2: T5 = ADD CV0($a) CV1($b) L3: T6 = ADD T5 int(3) L4: ASSIGN CV2($result) T6 L5: ECHO CV2($result) L6: RETURN int(1)

Slide 33

Slide 33 text

Control Structures

Slide 34

Slide 34 text

If/Else $test = true; if ($test) { echo "Hello World!"; } else { echo "Bye World!"; } $_main: L0: ASSIGN CV0($test) bool(true) L1: JMPZ CV0($test) L4 L2: ECHO string("Hello World!") L3: JMP L5 L4: ECHO string("Bye World!") L5: RETURN int(1)

Slide 35

Slide 35 text

While $test = true; while ($test) { echo "Hello World!"; $test = false; } $_main: L0: ASSIGN CV0($test) bool(true) L1: JMP L4 L2: ECHO string("Hello World!") L3: ASSIGN CV0($test) bool(false) L4: JMPNZ CV0($test) L2 L5: RETURN int(1)

Slide 36

Slide 36 text

For for ($i = 0; $i < 10; $i++) { echo "Hello World!"; } $_main: L0: ASSIGN CV0($i) int(0) L1: JMP L5 L2: ECHO string("Hello World!") L3: T2 = POST_INC CV0($i) L4: FREE T2 L5: T3 = IS_SMALLER CV0($i) int(10) L6: JMPNZ T3 L2 L7: RETURN int(1)

Slide 37

Slide 37 text

All Control Structures Can Be Implemented By JMP And IS_* Opcodes

Slide 38

Slide 38 text

Why A VM? Reason #3 Keep PHP expressiveness while really executing a simpler language

Slide 39

Slide 39 text

A Hypothetical Control Structure: Forever New Language Feature, But No Need To Change The VM forever { echo "Hello World!"; } $_main: L0: ECHO string("Hello World!") L1: JMP L0

Slide 40

Slide 40 text

Internal Functions echo microtime(); $_main: L0: INIT_FCALL 0 string("microtime") L1: V0 = DO_ICALL L2: ECHO V0 L3: RETURN int(1)

Slide 41

Slide 41 text

Internal Functions With Parameters $subject = "Such a nice message!"; str_replace("nice", "cool", $subject); $_main: L0: ASSIGN CV0($subject) string("Such a nice message!") L1: INIT_FCALL 3 string("str_replace") L2: SEND_VAL string("nice") 1 L3: SEND_VAL string("cool") 2 L4: SEND_VAR CV0($subject) 3 L5: DO_ICALL L6: RETURN int(1)

Slide 42

Slide 42 text

User Function function hello($name) { echo "Hello $name!"; } hello("World"); $_main: L0: NOP L1: INIT_FCALL 1 string("hello") L2: SEND_VAL string("World") 1 L3: DO_UCALL L4: RETURN int(1) hello: L0: CV0($name) = RECV 1 L1: T1 = CONCAT string("Hello ") CV0($name) L2: T2 = CONCAT T1 string("!") L3: ECHO T2 L4: RETURN null

Slide 43

Slide 43 text

Inheritance abstract class MyParentClass { function helloWorld() { echo "Hello World"; } } class MyChildClass extends MyParentClass{} $object = new MyChildClass(); $object->helloWorld(); $_main: L0: V4 = NEW 0 string("MyChildClass") L1: DO_FCALL L2: ASSIGN CV0($object) V4 L3: INIT_METHOD_CALL 0 CV0($object) string("helloWorld") L4: DO_FCALL L5: RETURN int(1) MyParentClass::helloWorld L0: ECHO string("Hello World") L1: RETURN null

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Opcode Cache: Monitoring $status = opcache_get_status(); $status["memory_usage"]["used_memory"] $status["memory_usage"]["free_memory"] $status["opcache_statistics"]["num_cached_keys"] $status["opcache_statistics"]["max_cached_keys"] $status["opcache_statistics"]["opcache_hit_rate"]

Slide 54

Slide 54 text

Opcode Cache: Memory Settings default 64 MB increase when free_memory~0 opcache.memory_consumption

Slide 55

Slide 55 text

Opcode Cache: Num Keys Settings default 2000 inc when num_cached_keys~max_cached_keys The actual value used will be the rst number in the set of prime numbers {463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987} that is greater than or equal to the con gured value. opcache.max_accelerated_files

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

The Opcode Optimizer Multipass Static Analysis To Optimize The Generated Opcode

Slide 59

Slide 59 text

Optimizer Before And After Opcodes Before optimizer After optimizer $ php -d opcache.opt_debug_level=0x10000 test.php $ php -d opcache.opt_debug_level=0x20000 test.php

Slide 60

Slide 60 text

Optimizer - Before And After Before After if ("foo") { echo "Foo!"; } else { echo "Not Foo!"; } L0: JMPZ string("foo") L3 L1: ECHO string("Foo!") L2: JMP L4 L3: ECHO string("Not Foo!") L4: RETURN int(1) L0: ECHO string("Foo!") L1: RETURN int(1)

Slide 61

Slide 61 text

Optimizer - Before And After Before After for ($i = 0; $i < 10; $i++) { echo "Hello World!"; } L0: ASSIGN CV0($i) int(0) L1: JMP L5 L2: ECHO string("Hello World!") L3: T2 = POST_INC CV0($i) L4: FREE T2 L5: T3 = IS_SMALLER CV0($i) 10 L6: JMPNZ T3 L2 L7: RETURN int(1) L0: ASSIGN CV0($i) int(0) L1: JMP L4 L2: ECHO string("Hello World!") L3: PRE_INC CV0($i) L4: T1 = IS_SMALLER CV0($i) 10 L5: JMPNZ T1 L2 L6: RETURN int(1)

Slide 62

Slide 62 text

Optimizer - Performances?

Slide 63

Slide 63 text

What's Next? What's Next? What's Next?

Slide 64

Slide 64 text

PHP 8 Just In Time Compilation From opcodes, generating real CPU instructions to execute them directly on the hardware

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

PHP 8 JIT - Current State 5x speedup on number crunching bench.php, but... ... -0.01% performance on real world applications Main idea: allow PHP to enter areas where it's not used yet github.com/zendtech/php-src/tree/jit-dynasm/

Slide 70

Slide 70 text

PHP 7.4 Preload The Main Opcache Limitation Opcache caches opcode but at le level classes and functions still need to be reloaded into the process memory

Slide 71

Slide 71 text

PHP 7.4 Preload What It Will Do Permanently load in the server memory functions and classes Once loaded, these classes will be available directly, like builtins (\Exception)

Slide 72

Slide 72 text

PHP 7.4 Preload How It Will Do It At server start, runs a PHP script that will load the needed le

Slide 73

Slide 73 text

PHP 7.4 Preload What Can We Expect From It ZF2 Hello world app: 2x requests per second If application fully loaded, no need for autoload anymore

Slide 74

Slide 74 text

Key Takeaways opcodes are the instructions of the Zend virtual CPU use opcode cache! monitor your opcode cache check out preload for PHP 7.4 (ETA end of the year)

Slide 75

Slide 75 text

For More Information Nikita Popov's Blog (nikic.github.io) Julien Pauli's Blog (blog.jpauli.tech) Preload RFC PHP 7 Virtual Machine PHP's OPCache extension wiki.php.net/rfc/preload

Slide 76

Slide 76 text

Thank You! Questions? @bjacquemont www.akeneo.com