We're hiring! Check out our currently open positions »

tech.CurrencyFair

Introduction

Rasmus Lerdorf is described as the ‘father’ of PHP, having written the first two versions and he continues to maintain an involvement in its development and direction. He was an infrastructure architect at Yahoo! for more than 7 years and currently works at Etsy.

The Talk

Slides

Rasmus gave a talk on the 12th January regarding PHP 7 and what developers can expect from it. The talk was really well attended with many needing to stand at the back. It was very well presented with significant time given for Q & A afterwards where many interesting questions were asked.

The talk covered everything from what’s new in the language, to how to prepare for migration (things that most likely will cause some problems) and the most interesting part of all, the jaw dropping benchmarks showing remarkable improvement in req/s and memory usage across a range of standard out-of-the-box applications.

New Stuff

  • Abstract Syntax Tree
  • Persistent secondary file cache for the OPCache
  • Exceptions on fatals (now implements throwable - a new interface for Exceptions), making them catchable.
  • Return Types
  • Coercive Scalar Types for hinting, strict mode is available.
  • Anonymous classes (which can implement interfaces)
  • Coalesce operator. Useful for default values
  • Spaceship operator for comparison functions in sorting
  • Zero cost assertions, can be configured on/off plus can be included in the opcache if you need to enable them later
  • Added Closure::call()
  • Bye bye deprecated features o/
  • New reserved words
  • Plus several more

Performance

The key drivers behind the performance gains and lower memory usage were:

  • The use of a persistent secondary file cache for the OPCache
    • This secondary cache allows for faster response times after a web server restart (which clears the shared memory cache). CLI based applications can now also use this secondary cache, faster composer/crons scripts for instance!
  • Optimisation of the existing PHP source code
    • Better use of CPU cache
    • Better memory allocation - opcache pointers used by processes rather than a copy existing in each
    • Faster hashtable iteration
    • Array duplication optimisation
    • PCRE JIT enabled. Very useful for MediaWiki and other frameworks that do a lot of regex parsing.
    • Plus many more

Additionally, improvements were made by:

  • Offering HugePage support in Opcache - more data in each page, less misses. Requires OS configuration

Benchmarks

Rasmus then brought us through benchmarks for various popular codebases such as Drupal 8, WP 4.1, MediaWiki, Magento 2 to name but a few. Each benchmark showed dramatic req/s improvements over PHP 5.5 and 5.6 but also showing how close PHP 7 was in comparison to HHVM 3.10.1 and in most cases exceeding its performance by a small margin (some showed significantly better performance with PHP7).

WordPress Requests per Second

He noted that HHVM seems to be very well optimised for WordPress however we were shown that PHP7 could be optimised for a specific codebase by utilising GCC Feedback Directed Optimisation (FDO) during the build process to “train” PHP to be more optimised. By training PHP for WordPress in this way he showed that it was possible to beat HHVM in the req/s benchmark albeit by 2 requests (658 vs 656 req/s)!

He was transparent regarding the specs of his test boxes including the config setups he used and invited people to try for themselves to prove the results were correct.

Measuring Memory Usage

Next we were shown how to properly measure memory usage for php-fpm. He recommended using smem reporting tool to view the Proportional Set Size (PSS) which is a much more meaningful representation of the amount of memory used. PSS will measure each processes “fair share” of each shared memory area to give a more realistic measurement.

Memory Use Benchmarks

Using the PSS metric mentioned, he showed us various memory usage levels from the base systems (no load, 10 processes) and across the same applications that were measure in the req/s benchmarks. All benchmarks showed incredible reductions in memory usage for PHP 7 compared to PHP 5.3 / 5.4 / 5.5 and 5.6.

WordPress Memory Usage

Gotchas when migrating codebases to PHP7

After we had seen the awesome performance benchmarks, Rasmus highlighted some of the issues which we may run into when moving codebases to PHP7:

  • LTR Semantics - Uniform Variable Syntax
    • $foo->$bar[‘baz’] would be interpreted as ($foo->bar)[‘baz’]
    • Previous behaviour could be restored using curly braces (which makes it far easier to understand what the developer intended): $foo->{$bar[‘baz’]}. He noted that this change should be performed anyway on current codebases well in advance of any PHP7 testing.
    • The Phan tool (described in next section) can also be used to statically analyse the codebase to find such occurrences. Requires PHP7 and php-ast extension
  • Invalid octal literals produce parse error
    • echo 05678; // PHP5.x echos 375, PHP7 gives Parse error, easy to find using php -l
  • Phan Static Analyser
    • Written by Rasmus, Phan relies on the internal Abstract Syntax Tree introduced in PHP7 to analyse code. The AST is a way of organising code between tokens and execution in a tree. The tree is then indexed and traversed to find bug patterns fast.

Deployment

Rasmus spoke about deployment process and how they should be atomic and without a performance hit. The points he made for ideal deployments were:

  • No restarts
  • No Load Balancer removals
  • No thundering herd
  • Proper cache reuse

He spoke about how during deployment, the web server should be able to essentially serve two versions of the site in parallel. In-flight requests must be allowed finish in their own document roots (context) without the symlink switch having an impact on these requests (such as a call to include a file which would include it from the new version of the site). This is possible to do on nginx by setting the document root to be the symlink target and which is already in place on our web servers:

fastcgi_param DOCUMENT_ROOT $realpath_root

He proceeded to discuss the deployment process in use at Etsy. They use an IRC channel with bots to control access to the queue and a deployment tool called Deployinator.

Testing PHP7

Rasmus encouraged us to go and test our applications and extensions on PHP 7. He provided links to his own development Vagrant Environment which has 24 pre-compiled versions of PHP to allow for easy testing.

Q & A session

A lengthy and interesting Q & A session followed which was still in full flight by the time I had to leave. Questions ranged from events that occurred during the early days of PHP’s history, to whether the optimisations were almost discovered by accident since they hadn’t set out initially on that path, to whether PHP will ever natively support threading/event loops plus many more I can’t even recall.

Huge thanks for a great evening goes to Etsy for organising, for a great crowd turning up and to Rasmus himself for being so generous with his time.

Bonus Read

Silicon Republic interviewed him the following day and the article can be found here. CurrencyFair is name-checked by the reporter as a company heavily reliant on PHP \o/


  • php7

blog comments powered by Disqus