PHP Classes

PHPNG Dramatic Speedup Features Coming in PHP 6 Release

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHPNG Dramatic Speedu...   Post a comment Post a comment   See comments See comments (15)   Trackbacks (0)  

Author:

Viewers: 88

Last month viewers: 26

Categories: PHP Performance, News, PHP opinions

Not a very long after Facebook announced the Hack language, Dmitry Stogov of Zend announced a somewhat secret development branch of PHP called PHPNG that brings a JIT engine, significant speed and memory management improvements eventually to PHP 6.

Read this article to learn more details about what happened and what this means for the future of PHP.




Loaded Article

Contents

What is PHPNG?

When PHPNG will be available?

Backwards Incompatible Changes

The Plot to Kill Apache mod_php SAPI

PHPNG versus Facebook Hack

Conclusions


PHPNGWhat is PHPNG?

PHPNG is a new branch of development that aims to bring a whole new level of performance and memory usage efficiency to PHP.

This branch was added somewhat secretly by Zend developers to the PHP development repository in April 16 but it was openly described only in May 5 when Sebastian Bergmann of the PHPUnit fame asked in the PHP internals about it.

Dmitry Stogov of Zend presented a more or less detailed description of the PHPNG branch. He explained that he has been experimenting using a JIT engine (Just In Time compilation to native machine code) using LLVM.

Using LLVM as a JIT for the Zend Engine was already proposed by Nuno Lopes in 2008. Some work was done with a student that was directed by Nuno Lopes as a Google Summer of Code project that resulted in the PECL LLVM extension. I have covered all possible PHP JIT solutions in an article about this topic in 2011.

The PHPNG branch also features improvements in the PHP memory management. According to Dmitry Stogov, the PHP execution takes a big part of the time dealing with memory allocations, and so that is an aspect that affects significantly PHP performance.

According to his tests the speed improvements of typical PHP applications are significant. For instance, he measured it and noticed that it can server 20% more requests per second when testing a Wordpress installation.

You cannot expect that all PHP applications will benefit from similar performance improvements because most of the time PHP is waiting for database or cache access operations to complete. Those operations will not benefit much from speed improvements in the execution of PHP, as PHP will be idle most of the time.

When PHPNG will be available?

These developments are mostly experimental. There is plenty of work to do to make them ready to be used in a stable PHP enviroment.

The expectation is that the PHPNG branch will be merged into PHP 6 or PHP 7 depending on what is supposed to be called the next PHP major release.

Backwards Incompatible Changes

The idea behind the PHPNG branch is to run existing PHP code without any changes. So PHP users will not be required to change their code to benefit from these improvements.

However, since PHPNG requires changes in the PHP internals, existing PHP extensions need to be adapted to work with the changes done in the Zend Engine. It seems to not require much work but those changes need to be done in the code of all PHP extensions.

The Plot to Kill Apache mod_php SAPI

During the debate about the work to be done to adapt extensions for these Zend Engine changes it was discussed which server APIs (SAPIs) should be worth supporting in future PHP versions. PHP supports many SAPIs. Some of them are probably not being used by many PHP users.

One of the SAPIs that was considered to not be supported in the future is the mod_php SAPI. The reason is not whether it is used by PHP users or not. mod_php is very popular, probably the most popular SAPI.

The reason for a possible deprecation and future discontinuation is the claim that using Apache or some other Web server with FastCGI or similar SAPI would be more efficient than using mod_php SAPI.

While that may be true in certain circumstances, many PHP applications rely on features only available when PHP is being used with Apache mod_php. For instance mod_php can read PHP options from .htaccess files. While there is the htscanner extension that can be used with other SAPIs to read configuration from .htaccess files, it is not exactly a perfect solution.

This is just to say that once mod_php is discontinued, upgrading to the PHP version that no longer supports it, will cause eventual grief and discourage PHP users and hosting companies to upgrade PHP.

This is a deja vu situation. In 2011 PHP core developers started discussing the discontinuation of the original MySQL extension. This was announced in another article of this blog. It was probably the article here that got most reactions because the change would affect probably millions of PHP users.

By then some core developers blamed this blog for announcing something that was not yet decided. Actually the article was just telling that there was intention to kill the mysql extension. Blaming this blog for exposing PHP core developers intentions was like shooting the postman for delivering letters with bad news.

The fact is that mysql extension ended up being deprecated in PHP 5.5 and is scheduled to be removed in PHP 5.7 or PHP 6, regardless of how many PHP users opposed to the decision. So the chances that Apache mod_php SAPI will be removed in future PHP versions are not negligible. Anyway, no decision was made about this yet, so not panic (yet).

PHPNG versus Facebook Hack

It seems it is not a coincidence that this great plan to finally bring a JIT compiler to PHP is something that happened just a few weeks after Facebook announced the Hack language.

The Hack language provides many features that PHP users want but were never implemented often with the justification that it takes a lot of time and effort to implement them.

One of the requested features is that it is built on top of the HHVM (HipHop Virtual Machine) engine. Among other features, HHVM implements JIT dynamic compilation of PHP code to native machine, just like PHPNG implements now to achieve speed improvements. So apparently the release of the Hack language rushed the development of PHPNG.

Facebook is a social network site. Their focus is not to develop new programming languages. If they created a new language, it is because they needed features that PHP did not provide. They just could not wait forever until the PHP core developers to give those features enough priority to implement them.

At the same time I think Facebook would rather rely on the official PHP core developments if it had the features they needed. So, if Facebook goal was to rush the development of such new PHP features, congratulations it seems they already succeeded to a certain extent.

Still the current PHP official implementation is far behind Hack in terms of features. Here are some of the features that the official PHP needs to catch up to at least match Hack features:

1. Type Hinting

Hack provides support for type hinting on everything: function arguments, function return values, and variables. Type hinting is useful for enforcing type checks and detecting bugs in PHP code.

However, type hinting is also useful to help the HHVM engine to generate optimized native machine code. Once the engine is aware of the types of variables and function return values, it no longer has to generate machine code to check and convert those values types at execution time. The resulting machine code can be smaller and faster when compared to when there is no type hinting.

2. Asynchronous Programming

Asynchronous programming is a way to execute multiple tasks in parallel while waiting for asynchronous tasks that depend on I/O operations like accessing files, network, databases, etc..

Hack introduced the await keyword. That allows your code to call some other code that is running in parallel while the calling script flow will only resume when the asynchronous operations inside the await code are finished.

This means that you can have multiple parallel activities running without having to use callback functions to handle their results. This is the most elegant asynchronous programming solution I have seen so far in any language. It is certainly better than the way it is done in JavaScript with Node.js for instance.

3. Production Ready Standalone Web server

Hack code runs inside HHVM. HHVM may run either as a single command line script, as a Web server, or as a FastCGI server. As a Web server, HHVM can run PHP Web applications without needing a separate Web server.

Nowadays, PHP can also run as a standalone Web server. The problem is that it is not yet considered robust enough to run as a Web server in a production environment.

4. Multithread safe

HHVM can run as a multithreaded Web server. This means that it can accept simultaneous connections in separate threads.

In theory PHP can also run with multithreaded Web servers. However when it uses extensions and libraries that are not thread safe, it may crash due to memory corruption caused by multiple threads attempting to access the same data in the same memory space.

To avoid this problem, it is recommended that PHP runs with a multi-process environment. The difference between processes and threads is that each process has its own memory space. So there is no way one process may cause another process to crash due to memory corruption.

On the other hand, in a multithreaded server all threads share the same memory space. This allows to make a more efficient use of memory. When a thread ends handling a HTTP request, it frees memory for handling other simultaneous requests.

This would not be possible in a multi-process environment like with Apache mod_php (pre-fork) or with PHP handling requests as FastCGI.

This way HHVM can provide a more scalable solution that can handle more simultaneous requests within the same server machine, as the limit of simultaneous requests that a single machine can handle, is directly tied to the amount of RAM that all requests are using at a given moment.

This is a matter that I have talked about in 2008 when I was invited to attend a PHP event organized by Microsoft along with many other PHP developers.

The effort to make the Zend Engine based PHP fully thread safe was always considered too complicated by the PHP core developers, so it was never done. However it was not too complicated for Facebook, so they made its HHVM based implementation as thread safe as it should be.

Conclusions

The PHPNG branch is certainly good news for the PHP community. Congratulations to Dmitry Stogov, Nikita, Xinchen and everybody else at Zend for finally have taken this step to move on with the PHP evolution.

After the release of Facebook Hack, maybe they finally realized that moving on to a JIT compiler based Zend Engine was a necessary step to keep Zend relevant in the PHP future.

PHP still has a lot to catch up to match Hack language features and HHVM benefits. Still this is good start.

Personally I would rather see Facebook join forces with Zend and other core PHP developers because that would make the PHP community stronger and the PHP future brighter, but there must be good will on each side to cooperate.

Still I think the Hack language development will continue independently at least for a while even if Zend wants to colaborate because Facebook needs Hack features to reduce their costs of deployment of the Facebook site.

What do you think? Should Zend Engine based PHP and the Hack language teams merge efforts, or shall they continue separately and compete forever? Post a comment here to tell what is your opinion.




You need to be a registered user or login to post a comment

1,611,062 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

2. Absolutely, they should merge efforts - Gary England (2014-08-19 19:38)
Yes... - 2 replies
Read the whole comment and replies

6. definetly - Alexandru Ocheana (2014-08-19 02:35)
my opinion... - 1 reply
Read the whole comment and replies

5. excited about phpng! - Michael Dagn (2014-05-21 07:05)
reduced ram footprint could be the game changer... - 1 reply
Read the whole comment and replies

4. Unity 4 All - Jurgen_v_O (2014-05-14 22:33)
Do not Frament the Open Source Community !!!... - 1 reply
Read the whole comment and replies

3. PHPNG - Abbas Farahmand.P (2014-05-14 01:36)
merge to improve... - 3 replies
Read the whole comment and replies

1. Zend and Facebook jointly make an impact on PHP - Saleem Jash (2014-05-12 11:35)
Zend and FB developers unite together and bring the best for PHP... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHPNG Dramatic Speedu...   Post a comment Post a comment   See comments See comments (15)   Trackbacks (0)