Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OPcache optimization levels - what are they?

There is an opcache.optimization_level php.ini directive. It is a bitmask a defaults to 0xffffffff - so by default OPcache does all the optimizations.

What kind of optimizations does OPcache do? What passes on bytecode are done?

Follow-up question: is there a code pattern that OPcache can optimise very well? For example, HHVM can skip execution of files that contain only class/function declarations and it just fills class/function tables.

like image 234
Jakub Kulhan Avatar asked Jan 17 '14 08:01

Jakub Kulhan


People also ask

How does PHP Opcache improve script performance?

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

Should I enable Opcache?

You should definitely enable opcache. I remember that WordPress has something like 8x smaller response time with opcache. Difference may be less pronounced for Laravel, but will still be huge.

What is Opcache Enable_cli?

opcache. enable_cli boolean enables the opcode cache for the CLI version of PHP. This is mostly useful for testing and debugging. Therefore it should be disabled unless you're really need this.

What is PHP OpCode caching?

OpCode Caches are a performance enhancing extension for PHP. They do this by injecting themselves into the execution life-cycle of PHP and caching the results of the compilation phase for later reuse. It is not uncommon to see a 3x performance increase just by enabling an OpCode cache.


1 Answers

The bits of opcache.optimization_level correspond to:

  • bit 0 - pass 1:
    • CSE - constants subexpressions elimination
    • Sequences of ADD_CHAR/ADD_STRING optimization
    • convert CAST(IS_BOOL,x) into BOOL(x)
    • convert INTI_FCALL_BY_NAME + DO_FCALL_BY_NAME into DO_FCALL
  • bit 1 - pass 2:
    • Convert constant operands to expected types
    • Convert conditional JMP with constant operands
    • Optimize static BRK and CONT
  • bit 2 - pass 3:
    • Convert $a = $a + expr into $a += expr
    • Convert $a++ into ++$a
    • Optimize series of JMPs
  • bit 3 - pass 4:
    • PRINT and ECHO optimization - removed
  • bit 4 - pass 5:
    • block optimization (the most expensive optimization pass which perform many different optimization patterns based on CFG - control flow graph)
  • bit 8 - pass 9:
    • register allocation (allows re-usage of temporary variables)
  • bit 9 - pass 10:
    • remove NOPs

I've looked around to see if I can find anything on any code patterns that it handles better than others, but I haven't had any luck.

Information from https://github.com/zendtech/ZendOptimizerPlus/blob/master/Optimizer/zend_optimizer.c and https://gist.github.com/ck-on/4959032?ocp.php

like image 132
G-Nugget Avatar answered Sep 20 '22 05:09

G-Nugget