SlideShare a Scribd company logo
1 of 44
What's up with OPCache ?
PHP 7 OPCode Cache tour
Hello everybody
 Julien PAULI
 Programming in PHP since ~20y (~1999-2000)
 Programming in C for PHP Internals
 PHP 5.5 and 5.6 Release Manager
 Working at SensioLabs in Paris
 http://www.phpinternalsbook.com co-author
 @julienpauli - http://jpauli.tech - jpauli@php.net
What we'll cover together
 Reminder on how PHP works
 Introduction to OPCodes
 The need of an OPCode cache
 OPCache in deep
 OPCache configuration settings
What is PHP ?
 PHP
 Programming language
 "Scripting language"
 No *manual* compilation needed
 Fire and forget
 Automatic memory management
 Facultative strong typing. Type juggler
 Provides OOP features
 Highly dynamic, highly extensible
How does PHP work ?
Parsing
(Lexer + parser)
Compiling
Executing
opcodes
PHP code
AST nodes
Result
Zend Virtual Machine
Parsing
(Lexer + parser)
Compiling
Executing
 "VirtualMachine"
 Its purpose is to provide a platform-
independent programming
environment that abstracts away
details of the underlying hardware or
operating system, and allows a
program to execute in the same way
on any platform (Wikipedia)
The PHP way
 Compile, execute, forget - Compile, execute, forget -
Compile, execute, forget - Compile, execute, forget -
Compile, execute, forget - Compile, execute, forget -
Compile, execute, forget - Compile, execute, forget …
 By default (by design), PHP discards all the code it just
executed
 Request n+1 knows nothing about request n
The PHP Way
 If your pages get hit several times without changing (most
likely)
 → compile the same scripts several times
 → it still produces the same OPCodes
Parsing
(Lexer + parser)
Compiling
Compiling VS Executing
 Compile, then execute
 Which one is the longest ?
 Depends on your scripts
Compile
Execute
Execute
Compile
Compiling VS Executing
 Compilation can really take time
 Usually less than execution, but...
Understanding compil. / exec.
 Classes usually require much more compile time than exec
time
 include / require / eval() = compile + execute
 autoload = compile + execute
 .. deffered at execution
Compile Execute
include/require ?
eval() ?
autoload ?
B
N
Y
E
OPCode cache roles
Parsing
(Lexer + parser)
Compiling
Executing
opcodes
PHP code
AST nodes
Result
Caching
opcodes
Save at first run
Compiling
Executing
opcodes
AST nodes
Caching
opcodesShared
Memory
opcodes
save
time
File
System
OR
Load (same script) after
OPCode cache
Executing
opcodes
PHP code
opcodes
load
Shared
Memory
File
System
OR
Optimize
 Why not optimize the OPCode array ?
opcodes
Optimizing
opcodes
opcodes
Caching
Compiling
save
Executing
opcodes
Shared
Memory
File
System
OR
PHP OPCache
OPCodes explained
OPCode
 In computer science, an opcode (operation code) is the
portion of a machine language instruction that specifies the
operation to be performed. (Wikipedia)
 Opcodes can also be found in so called byte codes and
other representations intended for a software interpreter
rather than a hardware device. (Wikipedia)
Zend OPCodes
Example of OPCode
<?php
const DEF = "default";
if (isset($argv[1])) {
$b = (array)$argv[1];
} else {
$b = DEF;
}
var_dump($b);
Example of OPCode
<?php
const DEF = "default";
if (isset($argv[1])) {
$b = (array)$argv[1];
} else {
$b = DEF;
}
var_dump($b);
L0 (3): DECLARE_CONST string("DEF") string("default")
L1 (5): T2 = ISSET_ISEMPTY_DIM_OBJ (isset) CV0($argv) int(1)
L2 (5): JMPZ T2 L7
L3 (6): T3 = FETCH_DIM_R CV0($argv) int(1)
L4 (6): T4 = CAST (array) T3
L5 (6): ASSIGN CV1($b) T4
L6 (6): JMP L9
L7 (8): T6 = FETCH_CONSTANT (unqualified) string("DEF")
L8 (8): ASSIGN CV1($b) T6
L9 (11): INIT_FCALL 1 96 string("var_dump")
L10 (11): SEND_VAR CV1($b) 1
L11 (11): DO_ICALL
L12 (12): RETURN int(1)
3
5
6
7
8
11
Example of OPCode
<?php
const DEF = "default";
if (isset($argv[1])) {
$b = (array)$argv[1];
} else {
$b = DEF;
}
var_dump($b);
Execution
Compilation
 The more code to parse, the longer the compilation phase
 The more OPCodes generated, the longer the execution
 Depends on OPCodes, some are long to run, some are very fast
DECLARE_CONST
ISSET_ISEMPTY_DIM_OBJ
JMPZ
FETCH_DIM_R
CAST
ASSIGN
JMP
FETCH_CONSTANT
ASSIGN
INIT_FCALL
SEND_VAR
DO_ICALL
RETURN
Optimize execution
 Some OPCodes are heavier than
others
 OPCodes may be run several
times (in fact, yes they do)
 The optimizer can play a role
Optimizing
Caching
Compiling
Executing
OPCache
 "OPCache" is PHP OPCode cache
 It is both a cache, and an optimizer
 It is bundled by defaut with PHP
 But it may not be activated by default
Optimizer
 Tries to simplify/optimize the OPCodes so that there are
less of them and they are more efficient
 Works on code branches (if, switch, try …)
 Optimize constant expressions - Look for dead code / code reuse
Try the optimizer
php 
-dzend_extension=opcache 
-dopcache.enable_cli=1 
-dopcache.opt_debug_level=0x30000 
-dopcache.optimization_level=0x7FFFFFFF 
/tmp/php.php
Optimizer impact
 The optimizer is very heavy, and burns tons of CPU cycles
 At compilation
 But compilation only happens once, as opcodes will be
cached
 So they'd better be cached optimized, than raw
 The very first run of a big application should pay a big perf
penalty
 Prime your caches
 Use OPCache file-based cache
Interned strings optimized
 In computer science, string interning is a method of storing
only one copy of each distinct string value, which must be
immutable. Interning strings makes some string processing
tasks more time- or space-efficient at the cost of requiring
more time when the string is created or interned. The
distinct values are stored in a string intern pool
Wikipedia
Interned strings concept
 Anytime a static string occurs in compiler, memorize it,
and reuse its pointer when needed.
 OPCache optimizes them even more
class foo
{
public function bar($a = "foo")
{
}
}
$foo = new foo;
$foo->bar("foo");
No Interned strings
class foo
{
public function bar($a = "foo")
{
}
}
$foo = new foo;
$foo->bar("foo");
foo bar a foo foo foo foo bar foo
memory
Interned strings
class foo
{
public function bar($a = "foo")
{
}
}
$foo = new foo;
$foo->bar("foo");
foo bar a
memory
OPCache & interned strings
 OPCache interns strings from all PHP processes of the same
PHP pool
 Nice memory savings on big pools
interned string mem interned string mem interned string mem
interned string mem
Tune your OPCache string buffer
php -dzend_extension=opcache 
-dopcache.memory_consumption=1024 
-dopcache.interned_strings_buffer=64 
/tmp/foo.php
Mb
Comments are interned strings
 How many interned string bytes for this script ?
<?php
/**
* @param $a string
*/
function foo(string $a)
{
}
PHPDoc comments are interned strings
 How many interned string bytes for this script ?
 opcache.save_comments = 0 disables saving of PHPDoc
comments to shared memory
<?php
/**
* @param $a string
*/
function foo(string $a)
{
}
28 + 3 + 1 + {garbage}
0 ending strings in C + some alignment bytes
PHP OPCache
OPCache API, settings and tuning
OPCache settings
 memory_consumption
 Size of Shared Memory
 Don't forget to increase according to your needs
 Buy more RAM, NEVER have a cache full, have margin
 max_accelerated_files (=2000)
 Max number of KEYS to be stored
 KEYS are paths to files : ./foo.php, ../foo.php, etc...
 One file usually is ~= 2 to 3 KEYS
 Aligned at next prime number
 Slots are preallocated : don't give too high number
 Monitor
OPCache explained
 memory_consumption=128M
 max_accelerated_files = 3000
 Next prime : 3907
 Cached files : 1878
 Cached keys : 2722 / 3907
OPCache settings
 validate_timestamps (=true)
 Check for file update to invalidate it
 Checks are triggered every revalidate_freq seconds
 revalidate_freq (=2)
 How often check for file updates to invalidate them
 0 = every time
 Revalidate = syscall cached by PHP's realpath cache
 revalidate_paths (=0)
 1 : only the resolved realpath is used as cache key
 0 : the unresolved path is also added as a cache key
 use_cwd (=1)
 prepend cwd to every cache key for relative paths
OPCache memory details
 When a script changes, it is
recompiled (if validate_timestamps
= 1)
 Its old memory space is then
considered as "wasted" (it is then
NOT freed)
 When cache is full, if max_wasted_percentage is reached :
a cache restart is triggered
 Cache restart = empty cache and recompile all
 Cache is never restarted if not full
OPcache other settings
 optimization_level (=0x7fffffff)
 Enable/disable some optimizations.
 enable_file_override (=0)
 Redefines file_exists(), is_readable() and is_file() to look into cache
before : recommanded
 blacklist_filename (=NULL)
 Prevents caching some files you decide
 consistency_checks (=0)
 Computes a checksum at each file fetch. Not recommanded, ~5-
8% slowdown
 opcache.save_comments (=1)
 Saves PHPDoc comments to shared memory
OPcache other settings
 enable_cli (=0)
 Enables opcache in CLI mode
 Long living apps like PHPUnit and COmposer mat benefit from the
optimizer and the cache
 Other short scripts will pay an impact against the optimizer : don't
enable
 huge_code_pages (=0)
 Moves the PHP process code memory segment to huge pages
 On some specific OS, with a mainly-statically-linked PHP, that can have
a beneficial impact
 Check /proc/meminfo to activate HugePage before usage
 protect_memory (=0)
 mprotect() the SHM against writes. Useless until you find a bug, crash,
and want to debug it
Know what happens
 error_log (=stderr)
 File to write log to
 log_verbosity_level (=1)
 0=fatals … 4=debug
OPCache do's and don't's
 Prime your cache smoothly
 https://github.com/engineyard/ey-php-performance-tools
 Prevent cache stampede
 Have enough shared memory
 Size your hashtables correctly
 Try not to generate php files on runtime
 Prevent highly modified files from beeing cached
 Use blacklists
 Every cache action will lock the shared memory
 And PHP Engine is not threaded
Thank you for listening

More Related Content

What's hot

PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machinejulien pauli
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life CycleXinchen Hui
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?Ravi Raj
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memoryjulien pauli
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5julien pauli
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionjulien pauli
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Moriyoshi Koizumi
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHPWim Godden
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4Wim Godden
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Elizabeth Smith
 

What's hot (20)

PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memory
 
PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
How PHP works
How PHP works How PHP works
How PHP works
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
 

Similar to PHP 7 OPCache Tour: Optimize Your Code with the OPCode Cache

PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance毅 吕
 
php & performance
 php & performance php & performance
php & performancesimon8410
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APCBen Ramsey
 
Tips
TipsTips
Tipsmclee
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extensionhazzaz
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confooCombell NV
 
Apc presentation
Apc presentationApc presentation
Apc presentationguestef8544
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...anshkhurana01
 

Similar to PHP 7 OPCache Tour: Optimize Your Code with the OPCode Cache (20)

PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 
php & performance
 php & performance php & performance
php & performance
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
Tips
TipsTips
Tips
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Php basics
Php basicsPhp basics
Php basics
 
Php ppt
Php pptPhp ppt
Php ppt
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extension
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
PHP
PHPPHP
PHP
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
 
Php
PhpPhp
Php
 
Apc presentation
Apc presentationApc presentation
Apc presentation
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 

More from julien pauli

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019julien pauli
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGjulien pauli
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourselfjulien pauli
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)julien pauli
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPjulien pauli
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensionsjulien pauli
 
PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4julien pauli
 
Patterns and OOP in PHP
Patterns and OOP in PHPPatterns and OOP in PHP
Patterns and OOP in PHPjulien pauli
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentationjulien pauli
 
AlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHPAlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHPjulien pauli
 
Apache for développeurs PHP
Apache for développeurs PHPApache for développeurs PHP
Apache for développeurs PHPjulien pauli
 

More from julien pauli (13)

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Dns
DnsDns
Dns
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNG
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourself
 
Tcpip
TcpipTcpip
Tcpip
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHP
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensions
 
PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4
 
Patterns and OOP in PHP
Patterns and OOP in PHPPatterns and OOP in PHP
Patterns and OOP in PHP
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
 
AlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHPAlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHP
 
Apache for développeurs PHP
Apache for développeurs PHPApache for développeurs PHP
Apache for développeurs PHP
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

PHP 7 OPCache Tour: Optimize Your Code with the OPCode Cache

  • 1. What's up with OPCache ? PHP 7 OPCode Cache tour
  • 2. Hello everybody  Julien PAULI  Programming in PHP since ~20y (~1999-2000)  Programming in C for PHP Internals  PHP 5.5 and 5.6 Release Manager  Working at SensioLabs in Paris  http://www.phpinternalsbook.com co-author  @julienpauli - http://jpauli.tech - jpauli@php.net
  • 3. What we'll cover together  Reminder on how PHP works  Introduction to OPCodes  The need of an OPCode cache  OPCache in deep  OPCache configuration settings
  • 4. What is PHP ?  PHP  Programming language  "Scripting language"  No *manual* compilation needed  Fire and forget  Automatic memory management  Facultative strong typing. Type juggler  Provides OOP features  Highly dynamic, highly extensible
  • 5. How does PHP work ? Parsing (Lexer + parser) Compiling Executing opcodes PHP code AST nodes Result
  • 6. Zend Virtual Machine Parsing (Lexer + parser) Compiling Executing  "VirtualMachine"  Its purpose is to provide a platform- independent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform (Wikipedia)
  • 7. The PHP way  Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget …  By default (by design), PHP discards all the code it just executed  Request n+1 knows nothing about request n
  • 8. The PHP Way  If your pages get hit several times without changing (most likely)  → compile the same scripts several times  → it still produces the same OPCodes Parsing (Lexer + parser) Compiling
  • 9. Compiling VS Executing  Compile, then execute  Which one is the longest ?  Depends on your scripts Compile Execute Execute Compile
  • 10. Compiling VS Executing  Compilation can really take time  Usually less than execution, but...
  • 11. Understanding compil. / exec.  Classes usually require much more compile time than exec time  include / require / eval() = compile + execute  autoload = compile + execute  .. deffered at execution Compile Execute include/require ? eval() ? autoload ? B N Y E
  • 12. OPCode cache roles Parsing (Lexer + parser) Compiling Executing opcodes PHP code AST nodes Result Caching opcodes
  • 13. Save at first run Compiling Executing opcodes AST nodes Caching opcodesShared Memory opcodes save time File System OR
  • 14. Load (same script) after OPCode cache Executing opcodes PHP code opcodes load Shared Memory File System OR
  • 15. Optimize  Why not optimize the OPCode array ? opcodes Optimizing opcodes opcodes Caching Compiling save Executing opcodes Shared Memory File System OR
  • 17. OPCode  In computer science, an opcode (operation code) is the portion of a machine language instruction that specifies the operation to be performed. (Wikipedia)  Opcodes can also be found in so called byte codes and other representations intended for a software interpreter rather than a hardware device. (Wikipedia)
  • 19. Example of OPCode <?php const DEF = "default"; if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } var_dump($b);
  • 20. Example of OPCode <?php const DEF = "default"; if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } var_dump($b); L0 (3): DECLARE_CONST string("DEF") string("default") L1 (5): T2 = ISSET_ISEMPTY_DIM_OBJ (isset) CV0($argv) int(1) L2 (5): JMPZ T2 L7 L3 (6): T3 = FETCH_DIM_R CV0($argv) int(1) L4 (6): T4 = CAST (array) T3 L5 (6): ASSIGN CV1($b) T4 L6 (6): JMP L9 L7 (8): T6 = FETCH_CONSTANT (unqualified) string("DEF") L8 (8): ASSIGN CV1($b) T6 L9 (11): INIT_FCALL 1 96 string("var_dump") L10 (11): SEND_VAR CV1($b) 1 L11 (11): DO_ICALL L12 (12): RETURN int(1) 3 5 6 7 8 11
  • 21. Example of OPCode <?php const DEF = "default"; if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } var_dump($b); Execution Compilation  The more code to parse, the longer the compilation phase  The more OPCodes generated, the longer the execution  Depends on OPCodes, some are long to run, some are very fast DECLARE_CONST ISSET_ISEMPTY_DIM_OBJ JMPZ FETCH_DIM_R CAST ASSIGN JMP FETCH_CONSTANT ASSIGN INIT_FCALL SEND_VAR DO_ICALL RETURN
  • 22. Optimize execution  Some OPCodes are heavier than others  OPCodes may be run several times (in fact, yes they do)  The optimizer can play a role Optimizing Caching Compiling Executing
  • 23. OPCache  "OPCache" is PHP OPCode cache  It is both a cache, and an optimizer  It is bundled by defaut with PHP  But it may not be activated by default
  • 24. Optimizer  Tries to simplify/optimize the OPCodes so that there are less of them and they are more efficient  Works on code branches (if, switch, try …)  Optimize constant expressions - Look for dead code / code reuse
  • 25. Try the optimizer php -dzend_extension=opcache -dopcache.enable_cli=1 -dopcache.opt_debug_level=0x30000 -dopcache.optimization_level=0x7FFFFFFF /tmp/php.php
  • 26. Optimizer impact  The optimizer is very heavy, and burns tons of CPU cycles  At compilation  But compilation only happens once, as opcodes will be cached  So they'd better be cached optimized, than raw  The very first run of a big application should pay a big perf penalty  Prime your caches  Use OPCache file-based cache
  • 27. Interned strings optimized  In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool Wikipedia
  • 28. Interned strings concept  Anytime a static string occurs in compiler, memorize it, and reuse its pointer when needed.  OPCache optimizes them even more class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo");
  • 29. No Interned strings class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo"); foo bar a foo foo foo foo bar foo memory
  • 30. Interned strings class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo"); foo bar a memory
  • 31. OPCache & interned strings  OPCache interns strings from all PHP processes of the same PHP pool  Nice memory savings on big pools interned string mem interned string mem interned string mem interned string mem
  • 32. Tune your OPCache string buffer php -dzend_extension=opcache -dopcache.memory_consumption=1024 -dopcache.interned_strings_buffer=64 /tmp/foo.php Mb
  • 33. Comments are interned strings  How many interned string bytes for this script ? <?php /** * @param $a string */ function foo(string $a) { }
  • 34. PHPDoc comments are interned strings  How many interned string bytes for this script ?  opcache.save_comments = 0 disables saving of PHPDoc comments to shared memory <?php /** * @param $a string */ function foo(string $a) { } 28 + 3 + 1 + {garbage} 0 ending strings in C + some alignment bytes
  • 35. PHP OPCache OPCache API, settings and tuning
  • 36. OPCache settings  memory_consumption  Size of Shared Memory  Don't forget to increase according to your needs  Buy more RAM, NEVER have a cache full, have margin  max_accelerated_files (=2000)  Max number of KEYS to be stored  KEYS are paths to files : ./foo.php, ../foo.php, etc...  One file usually is ~= 2 to 3 KEYS  Aligned at next prime number  Slots are preallocated : don't give too high number  Monitor
  • 37. OPCache explained  memory_consumption=128M  max_accelerated_files = 3000  Next prime : 3907  Cached files : 1878  Cached keys : 2722 / 3907
  • 38. OPCache settings  validate_timestamps (=true)  Check for file update to invalidate it  Checks are triggered every revalidate_freq seconds  revalidate_freq (=2)  How often check for file updates to invalidate them  0 = every time  Revalidate = syscall cached by PHP's realpath cache  revalidate_paths (=0)  1 : only the resolved realpath is used as cache key  0 : the unresolved path is also added as a cache key  use_cwd (=1)  prepend cwd to every cache key for relative paths
  • 39. OPCache memory details  When a script changes, it is recompiled (if validate_timestamps = 1)  Its old memory space is then considered as "wasted" (it is then NOT freed)  When cache is full, if max_wasted_percentage is reached : a cache restart is triggered  Cache restart = empty cache and recompile all  Cache is never restarted if not full
  • 40. OPcache other settings  optimization_level (=0x7fffffff)  Enable/disable some optimizations.  enable_file_override (=0)  Redefines file_exists(), is_readable() and is_file() to look into cache before : recommanded  blacklist_filename (=NULL)  Prevents caching some files you decide  consistency_checks (=0)  Computes a checksum at each file fetch. Not recommanded, ~5- 8% slowdown  opcache.save_comments (=1)  Saves PHPDoc comments to shared memory
  • 41. OPcache other settings  enable_cli (=0)  Enables opcache in CLI mode  Long living apps like PHPUnit and COmposer mat benefit from the optimizer and the cache  Other short scripts will pay an impact against the optimizer : don't enable  huge_code_pages (=0)  Moves the PHP process code memory segment to huge pages  On some specific OS, with a mainly-statically-linked PHP, that can have a beneficial impact  Check /proc/meminfo to activate HugePage before usage  protect_memory (=0)  mprotect() the SHM against writes. Useless until you find a bug, crash, and want to debug it
  • 42. Know what happens  error_log (=stderr)  File to write log to  log_verbosity_level (=1)  0=fatals … 4=debug
  • 43. OPCache do's and don't's  Prime your cache smoothly  https://github.com/engineyard/ey-php-performance-tools  Prevent cache stampede  Have enough shared memory  Size your hashtables correctly  Try not to generate php files on runtime  Prevent highly modified files from beeing cached  Use blacklists  Every cache action will lock the shared memory  And PHP Engine is not threaded
  • 44. Thank you for listening