Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP OPCache, Realpath Cache and Preloading

PHP OPCache, Realpath Cache and Preloading

Everybody wants quick applications. A lot of that speed can be gained by the way you write your software, but a big chunk has to do with the way PHP is configured. As PHP matured, it got quicker, it used less memory and it accumulated a lot of settings which can be tuned for performance.

The biggest, and often most misunderstood, features for performance are OPCache (introduced in 5.5) and preloading (introduced in 7.4). This talk covers how both features work, how you can take advantage of them on your servers and during deployments, and tries to show all the ini settings relevant for performance.

Jachim Coudenys

May 14, 2019
Tweet

More Decks by Jachim Coudenys

Other Decks in Technology

Transcript

  1. Add in some cache! PHP Code Lexing + Parsing: Tokens

    Compiling: Opcodes Cache: Opcodes Executing
  2. Shared memory In computer hardware, shared memory refers to a

    (typically large) block of random access memory (RAM) that can be accessed by several different central processing units (CPUs) in a multiprocessor computer system.
  3. Opcodes? In computing, an opcode (abbreviated from operation code) is

    the portion of a machine language instruction that specifies the operation to be performed.
  4. Interned strings Used in a lot of languages Since PHP

    5.4 "Compression" for source code In shared memory (FPM master process)
  5. Opcache File Cache Read opcodes from disk Can help busy

    sites Supports CLI (used to be "fire and forget") Cache to "user directory" http://talks.php.net/froscon15#/php7pcache1
  6. Realpath Cache Used to reduce IO Caches all possible paths

    to destination path explode('/') No shared memory (per worker process)! Mem*Workers 4M (Prior to PHP 7.0.16 and 7.1.2, the default was "16K")
  7. PHP has been using opcode caches for ages (APC, Turck

    MMCache, Zend OpCache). They achieve significant performance boost by ALMOST completely eliminating the overhead of PHP code recompilation. With an opcode cache, files are compiled once (on the first request that uses them), and are then stored in shared memory. All the following HTTP requests use the representation cached in shared memory.
  8. Preloading PHP 7.4 Part of opcache Starts in master process

    before anything else Loads code in memory "permanently" No need to copy from shared memory to process memory
  9. Preloading Opcache only works per file Preloading helps with class

    libraries Code will perform as internal entities (e.g. strlen, etc...) Simple file with autoloading magic
  10. PHP <= 7.3 First request: app does new A() autoloader

    include 'A.php'; PHP "compiles" A.php into opcodes and stores it in opcache opcodes are executed
  11. PHP <= 7.3 Next requests: app does new A() autoloader

    include 'A.php'; A.php is already in opcache, no need to read the file and re-interpret the PHP opcodes are executed
  12. PHP >= 7.4 + preloading preloading starts: preloader new A()

    preloader + PHP "compiles" A.php into opcodes and stores it in memory
  13. Normal vs Preloading first request we save calls to the

    autoloader + compilation to opcode + storing to opcache next requests we save calls to the autoloader + fetching from opcache
  14. Elastic Stack @ Combell Gather realpath/opcache/etc... data from all accounts

    Act upon that data (which is changing constantly) Autotuning