Magento 126.96.36.199 version was released on Jul 7, 2015. As you know, Magento is based on PHP and Zend Framework, and there’ll be a new PHP release soon. What can developers expect from the new language version?
PHP is used on 81.9% of websites all over the world and has celebrated its 20th birthday some time ago. We talked to the PHP 7 lead developer and Zend Technologies Chief Performance Engineer – Dmitry Stogov. He spoke about the newest trends in PHP development and the world of script languages.
Zend and getting the development mania
– How did you start writing code?
My introduction to programming started with Electronica BZ-34 calculator. Polish notation, 96 commands, simple games like landing on the moon. Electronica DZ-28, BASIC, struggling with tape cartridge drive as well.
After that I saw Pac-Man on SM-4. I wanted to code it for DZ-28, but BASIC possibilities and performance were not enough for this (or maybe I lacked skills), and I started to dig into Assembler. As a result, I didn’t make Pac-Man, but unassembled the monitor, got into the internal stuff as well.
– Why did you hook exactly with PHP and how did you make up at Zend?
My career started with SM-4 and Modula-2. Afterwards I felt that Modula was not enough, and at some moment I wrote my own compiler for Linux/DOS/Windows/OS2. Later on, when I worked on serious projects, I got to know Ada and Prolog, which also had their share of influence. Digging into internal design of Prolog (Warren Abstract Machine) and attempts to make it better gave me knowledge needed for creating interpreters.
Now PHP 7 is 14 times faster than PHP 5.0 on bench.php.
I first met PHP in the beginning of 2000s, when I was working for a German company. I guess, we were developing a public web service then. And very soon we got stuck with PHP performance and project budget, so we started to look for ways of boosting speed. That’s how Turck MMCahce (eAccelerator) was born. As it was a competitor for Zend products, I established some contacts there. Eventually, a couple of phone calls were enough to get a job at Zend.
Now I am a Lead Engineer at Zend, and you can see the results of my work by comparing PHP performance, starting from version 5.0 and up to version 7. Now PHP 7 is 14 times faster than PHP 5.0 on bench.php, this is my direct area of responsibility.
– So, what is writing code for you now? A job, a routine, or an activity with elements of art and challenge?
If to speak about the inner feeling, it’s a job. The beardless obsessiveness is gone, but I’ve still got adrenaline. At Zend I define tasks for my group, so we can’t live without analysis and investigating of existing solutions and experiments. As we have an idea in our minds and on paper, implementation is a matter of technique. If the implementation is interrupted by formidable obstacles, something’s wrong with the idea itself. A big problem is that we can actually evaluate the profit from solution implementation only after the prototype is ready, and sometimes it takes years, if not months. Take JIT for PHP or PHPNG for example.
I’m used to the fact that only half of my ideas get through to implementing in PHP or Zend products. It’s important to note that primary ideas and the following prototypes go through various talking stages. This way my ideas are analyzed and complemented, and, vice versa, I’m often into implementing ideas of other PHP community members.
– How is your work organized at Zend?
For now I’m the only Zend employee in Russia, that’s why my work is almost always remote. My direct superior, Zeev Suraski, is in Israel, CEO Andi Gutmans is in the Silicon Valley, and my subordinates are in China. All communication is handled via email and Skype. And from time to time we have meetings with partners when we need to make important decisions. Such organization of remote work is more or less typical for Western companies.
– Can we make a formal statement that a Russian is the head of PHP 7 development?
I’m the author and leader of PHPNG. And yes, I’m one of the main PHP 7 developers. But I can’t say that me or anybody else is the head of the whole PHP development. Technically, all the PHP community members are equal, and the majority of the questions is solved by voting, where each member has a single vote.
But, if to be honest, it’s worth mentioning that real code contributors are rare at all times. I would like to notice special contributions to PHP 7 made by Nikita Popov, Xinchen Hui, Andrea Faulds, Anatol Belski, Levi Morison, Joe Watkins, Bob Weinand… As you see, I’m not alone here with a Russian name, but yes, I’m the only one from Russia.
Introduction to PHPNG and PHP 7
– What is PHPNG in general?
It was an important Zend project, aimed at boosting PHP performance without losing compatibility. In fact, it means full PHP core refactoring based on optimized data structures. We started working on the private project in January, 2014 and opened it in May after we got some favourable results, such as 10-30% real apps boost.
In August PHPNG was accepted as a core for PHP 7, and it was, technically, the project finish. We have been working on PHP 7 from August, and it’s a project with entirely different aims.
– What were the background and motivation for the birth of PHPNG?
Well, PHP performance boost activities have been almost always in progress since 2005. We added improvements and optimizations from version to version. Most often they influenced separate core subsystems and didn’t require significant changes in the whole PHP code. After PHP 5.4 release in 2012 this approach has run its course, and we started thinking about a new one.
That’s how the JIT epic started. We began with a simple sequence generator for handlers calling, similarly to Forth threaded code, and came to the code analyzer, based on SSA (Static Single Assignement Form), and also we developed a pass through. It gives an opportunity to define which value types and ranges can be accepted by variables in each point of the software. Plus, we implemented interprocedural analysis, inlining, and function cloning.
We used our own library for x86 as a code generator, but came to a standstill with registers distribution and tied LLVM. All in all, we created JIT, but the result was weird. Synthetic tests like bench.php and Mandelbrot showed 10X-30X speed boost, but real apps didn’t speed up not even a bit.
It happened partly because deriving types in a real task (opposed to a mathematical one) is much more complicated, and as the types were unknown to us in JIT, we had to work with the data structures as in PHP, meaning, assigning storage to them, increase and decrease link counts, etc. Here came the knowledge that optimizing base data structures improves generated JIT code significantly, but at that time we didn’t know that these changes would boost PHP even without JIT.
As a matter of fact, that’s how PHPNG started. We spent a week implementing primary changes for structures and two months for 40MB of dependent code adaptation (just compiling, running and seeing the result). Sure, we didn’t get a working solution right away, and it took us a month to debug, but the final result was 20-30% boost both on tests and real apps. But we didn’t stop there and increased performance by 10% each month. Now we have 2X advantage compared to the previous version even without JIT.
I’d like to mention that now PHP 7 can be faster than HHVM on many projects (HHVM is an alternative PHP+KIT implementation from Facebook).
– Is it right that the direction of your work has changed a couple of times?
Yep, and it sounds like an understatement. But PHPNG was developed by a small team with a clear goal, and it led us to success. And PHP 7 is a democratic open project with all advantages and disadvantages of such organization. I can’t say that all the decisions that have been made for PHP 7 meet my views 100%, but looks like things are going pretty well now.
– What is PHP 7 (PHPNG) for now?
I will point out the properties that are the most important from the users’ point of view:
- A new level of performance;
- A possibility to define argument types and function values;
- Multiple errors, which led to application break before, now call exceptions and, accordingly, can be handled by the app itself;
- Assert() function now can be handled in a special way to exclude additional expenses. You can use it for debugging and turn it off on production;
- On 64-bit Windows integers are finally 64-bit!
- ext/mysql and ext/ereg are removed from the project main tree (still available in PECL);
- some outdated features are removed.
– What is the stage PHP 7 project is on now?
PHP 7 is ‘frozen’ from the beginning of the year. The last planned and approved changes are being implemented, followed by future cleaning up, code stabilization, testing, writing documentation… The release is planned for the second half of 2015.
Growth issues and PHP future
– Now that we know about the past and the present of the new language version, it’s high time to ask about the future. Is there a roadmap for PHPNG and PHP 7? Are there features you’re planning to implement and what should we expect from the nearest future?
PHPNG roadmap is over. We will evolve PHP 7.0, 7.1 later on. I’m against cardinal changes in the languages and I would like to see only highly required features and universal constructions, which can be used as a base for virtually anything you need.
Also, I can mention some future features that look interesting and accomplishable – annotations (they are called attributes in several languages), cooperative multitasking, asynchronous functions as well. I hope to get back to JIT in times to come.
– Very often fans of ‘modern’ languages, such as Ruby and Python, express arrogant attitude towards PHP. They criticize PHP for an undeveloped object model and a big number of various discrepancies and roughness. What can you say to answer these critics?
Let’s start with the facts. Right now PHP is the second most popular script language in the world. But language popularity is poorly connected with its so-called ‘quality’. It’s important to understand that PHP is not just a language; it’s a tool which allows you to solve certain tasks fast and effective. Its popularity means that it’s one of the best tools in the area.
Now, if we treat PHP theoretically and as a language, I can agree with the fact that it’s far from being perfect and I can mention cases of strange behavior and issues myself. The issues are being corrected, things are improving, but you have to understand that each element removal is a potential trouble for our users. What do you say if goto will be removed from C tomorrow? How do you think – why wasn’t it removed in spite of heavy criticism from academic specialists?
Anyway, my work on PHP is very much about boosting performance. And here PHP has left Python and Ruby far behind. So… Critics are criticizing, while we’re working. (Smiles and shows test results)
Mandelbrot benchmark (sec). gcc -O2 (4.9.2) 0.013 LuaJIT-2.0.3 (JIT=on) 0.014 gcc -O0 (4.9.2) 0.022 HHVM-3.5.0 (JIT=on) 0.030 Java-1.8.0 (JIT=on) 0.059 LuaJIT-2.0.3 (JIT=off) 0.073 Java-1.8.0 (JIT=off) 0.251 PHP-7 0.281 squirrel-3.0.4 0.335 Lua-5.2.2 0.339 PHP-5.6 0.379 PHP-5.5 0.383 PHP-5.4 0.406 ruby-2.1.5 0.684 PHP-5.3 0.855 HHVM-3.5.0 (JIT=off) 0.978 PHP-5.2 1.096 python-2.7.8 1.128 PHP-5.1 1.217 perl-5.18.4 2.083 PHP-4.4 4.209 PHP-5.0 4.434
(You can run the test yourself here)
– To draw a line under this topic, what are top three features you’d name as the coolest ones in modern PHP? And please follow up with a couple of examples for bottlenecks which annoy you?
Well, it’s boring to talk about PHP advantages. I’ll just say again that it’s a fairly good and accessible web developing instrument. Here are a couple of subjective thoughts, though:
- Low entry barriers;
- Most of the concepts are intuitive;
- A rich collection of libraries;
- Decentish documentation and a large community;
Disadvantages may vary from person to person depending on the area of interest and preferences. For me it’s a list of what I really want to implement for PHP:
- Lack of support for multitasking and asynchronous behavior on PHP core level (BTW, this is one of the main aims for the next version)
- Lack of JIT compilation (value of JIT for real-life PHP apps is still questionable. For now we have managed to get a significant boost only for small synthetic tests. The reasons may lie in both technology drawbacks or implementation issues)
- Some excessively dynamic possibilities significantly complexify effective implementation.
– Now, let’s go beyond PHP, we’re asking you as a skilled language developer which investigates development trends on a global level. Can you name some tendencies of script languages for backend?
I would point out a couple of technologies that look interesting to me.
- Event-oriented model which allows solving certain tasks effectively. I’m talking about handling a big number of concurrent requests (Node.js, nginx+Lua). The application area is rather limited, but very in-demand.
- Building optimized interpreters and JIT compilers based on meta interpreters (PyPy). Apart from the fact that PyPy is the implementation of Python which is written on Python (and it is significantly faster than the original CPython), it is also an instrument for other languages implementation.
- I love simple and logical languages. From my point of view, the language description should fit into several pages. Implementation shouldn’t be expanding endlessly. Lua is a good example of how it should be done. In reality, some technical solutions for PHP 7 were borrowed from Lua and LuaJIT.
The key moment is to actively participate in completely different projects which expands your background knowledge.
– Could you give some advice for junior developers, working with PHP as well?
Here are the four things I would recommend to the younger generation from my own experience.
- No matter what your attitude towards PHP is, I would recommend trying as many different approaches as possible. It’s good to understand the functional approach, logical coding, OOP, and sync mechanisms. The ideal case is when you see how it works altogether in real world.
- Books about compilation theory helped me a lot.
- The key moment is to actively participate in completely different projects which expands your background knowledge.
- The importance of the English language is inevitable. Knowing English gives you opportunities to read scientific works, books and documentation, possibilities to participate in open source projects (see the previous point). Join projects you like or create new ones, learning and moving forward is above all.