• Inject themselves into the execution life-cycle of PHP and cache as much of it as possible • Common to see a 300% speed increase with default settings! 4
is known as a cache stampede. This is where your cache expires while under heavy load. Caused by: • Restarting Server • Cache invalidation because of data edits • Newly provisioned servers being added to a cluster to help scale under load • Corruption (hardware issues)
till PHP 5.5 the dominant choice was the Alternative PHP Cache (APC) • With PHP 5.5, Zend open sourced it’s proprietary Zend OpCache and contributed it to the PHP project • Zend OpCache is now enabled by default with PHP 5.5+ unless disabled by --disable-all. • Zend OpCache is between 6-100% faster than APC 11
alongside Zend OpCache to give backwards compatibility with APC’s User Cache: APCu • APCu is APC with the OpCode Caching removed • 100% Backwards Compatible 13
to your php.ini: [zendopcache] zend_extension=/full/path/to/opcache.so ; May be added by pecl opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 Restart PHP (php-fpm/apache)
your php.ini: [apcu] extension=apcu.so ; May be added by pecl apc.serializer=php ; See bug report for more info: http://ey.io/1aJhcOY Restart PHP (php-fpm/apache)
to aid with improving PHP performance • Open Source, Apache 2.0 License • On Github: http://github.com/engineyard/ey-php- performance-tools • Composer Compatible Package 22
cache */ 'urls' => [ 'http://ephernote.com/', 'http://ephernote.com/user/login', 'http://ephernote.com/user/register', 'http://ephernote.com/privary', // Note: this is a typo ], /* How many concurrent HTTP requests to make (requires the pecl_http extension) */ 'threads' => 3 ];
the cache • Still requires a single HTTP request • Can cache more than just easily web accessible code • Any valid PHP can be cached, including partial view templates, global code, etc. • Use: engineyard/php-performance-tools/apc-primer 33
relying on it, instead integrate it behind other authentication instead (e.g. embed it into your admin control panel) • Uses apc_compile_file() to add code to the cache • Requires a wrapper to expose via HTTP • Caches all .php and .phtml files not inside a tests directory 34
strings. • They are both double quoted strings! • Interpolated: 4 Tokens • Non-interpolated: 1 Token • Start to see why this matters for performance: but don’t get caught up by micro-optimizations. 45 " T_ENCAPSED_AND_WHITESPACE Hello T_VARIABLE $to " T_CONSTANT_ENCAPSED_STRING "World"
the compiled opcodes • Installed via PECL $ pecl install vld-beta Add the following to php.ini: extension=vld.so • Use via command line: $ php -dvld.active=1 -dvld.execute=0 <file> 48
the source file • #: The opcode number • *: entry (left aligned) and exit points (right aligned), indicated by greater than symbols (>) • op: The opcode name • fetch: Details on global variable fetches (super globals, or the use of the global keyword) • ext: Extra data associated with the opcode, for example the opcode to which it should JMP • return: The location where return data from the operation is stored • operands: the operands used by the opcode (e.g. two variables to concat) 51 line # * op fetch ext return operands ----------------------------------------------------------------
point (!) are compiled variables (CVs) — these are pointers to userland variables 2. Tilde (~) are temporary variables used for temporary storage (TMP_VARs) of in-process operations 3. Dollar ($) are another type of temporary variables (VARs) which are tied to userland variables like CVs and therefore require things like refcounting. 4. Colon (:) are temporary variables used for the storage of the result of lookups in the class hashtable 52
value which is assigned to !0 (which represents $to) • ADD_STRING: Next we create a temporary variable identified by a ~, ~0 and assign the static string, 'Hello+', where the + represents a space • ADD_VAR: After this, we concat the contents our variable, !0 to our temporary variable, ~0 • ECHO: Then we echo that temporary variable • RETURN: Finally we return nothing as the function ends 54
we store this reference in :1 • NEW: Then we instantiate an instance of the class (:1) and assign it to a VAR, $2, and • DO_FCALL_BY_NAME: call the constructor • ASSIGN: Next we assign the resulting object (in VAR $2) to our CV (!0) • INIT_METHOD_CALL: We start calling the sayHello method, and • SEND_VAL: pass in 'World' to the method, and • DO_FCALL_BY_NAME: Actually execute the method • RETURN: The script ends successfully, with an implicit return of 1. 56