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

More Decks by Jachim Coudenys

Other Decks in Technology


  1. PHP OPCache, Realpath Cache and Preloading

  2. Jachim Coudenys

  3. PHP-WVL

  4. Combell

  5. OPCache

  6. PHP Scripting Language Fire and forget No "manual" compilation

  7. Compilation PHP Code Lexing + Parsing: Tokens Compiling: Opcodes Executing

  8. https://engineering.facile.it/blog/eng/realpath-cache-is-it-all-php-opcache-s-fault/

  9. Throwaway language Always performs same steps All information is discarded

    Request per request
  10. OPCodes (usually) don't change

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

    Compiling: Opcodes Cache: Opcodes Executing
  12. Next run Cache: Opcodes Executing

  13. And the next run Cache: Opcodes Executing

  14. Even the next run Cache: Opcodes Executing

  15. OPCache Since PHP 5.5 Zend Optimizer "donated" by Zend

  16. 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.
  17. PHP-FPM Only "relevant" SAPI nowadays :) Master process Dynamic worker

    child processes Shared memory in the master
  18. Opcodes? In computing, an opcode (abbreviated from operation code) is

    the portion of a machine language instruction that specifies the operation to be performed.
  19. Vulcan Logic Dumper https://3v4l.org/A1B4H/vld#output

  20. This can get big quite quickly Libraries Frameworks

  21. OPCache Optimizer Gets better every release Optimizes: Branches Dead code

    ++$a vs $a++
  22. Interned strings Used in a lot of languages Since PHP

    5.4 "Compression" for source code In shared memory (FPM master process)
  23. opcache_compile_file() opcache_get_configuration() opcache_get_status() opcache_invalidate() opcache_is_script_cached() opcache_reset() php.net/manual/en/ ref.opcache.php

  24. opcache.memory_consumption opcache.validate_timestamps opcache.revalidate_freq opcache.interned_strings_buffer opcache.max_accelerated_files (keys) opcache.max_wasted_percentage php.net/manual/en/ opcache.configuration.php

  25. Keys Full path to file Relative paths to files

  26. Wasted memory Opcache doesn't to "defragmentation" File changes cause recompilation

  27. Opcache restarts OOM Restarts Hash Restarts Manual Restarts

  28. NEVER have a full cache!

  29. Opcache Priming opcache_compile_file() FPM Pools

  30. opcache.php demo

  31. APCu User Shared memory apc_* compatible

  32. 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
  33. opcache.file_cache=/var/tmp/php/opcache opcache.file_cache_only=1 # Useful for CLI opcache.file_cache_consistency_checks=1 # Adler checksum

  34. Scenario's Memory Full? Interned Strings Full? Key store full?

  35. Deployment Strategies It Depends ¯\_( )_/¯

  36. Realpath Cache

  37. 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")
  38. realpath_cache_get() realpath_cache_size () realpath() php.net/manual/en/ ref.filesystem.php

  39. realpath.php demo

  40. realpath_cache_size realpath_cache_ttl php.net/manual/en/ ini.core.php

  41. Preloading

  42. 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.
  43. 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
  44. 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
  45. opcache.preload https://wiki.php.net/rfc/preload#proposal

  46. PHP <= 7.3 PHP process starts up

  47. 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
  48. 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
  49. PHP >= 7.4 + preloading PHP process starts up

  50. PHP >= 7.4 + preloading preloading starts: preloader new A()

    preloader + PHP "compiles" A.php into opcodes and stores it in memory
  51. PHP >= 7.4 + preloading First request: app does new

    A() opcodes are executed
  52. PHP >= 7.4 + preloading Next requests: app does new

    A() opcodes are executed
  53. 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
  54. https://externals.io/message/103333 https://wiki.php.net/rfc/preload https://github.com/php/php-src/pull/3538

  55. PHPArch Opcache article https://www.phparch.com/article/diving-in-the- opcache/

  56. Elastic Stack @ Combell Gather realpath/opcache/etc... data from all accounts

    Act upon that data (which is changing constantly) Autotuning
  57. Thank you! https://joind.in/event/php-wvl-may-meetup-at-studio- emma-2019/php-opcache-and-preloading