We have previously microbenchmarked the computation of Pi in PHP 5.6 and 7 versus Peachpie, as well as its predecessor Phalanger. With the progress we made in Peachpie, we are now able to measure how quickly it calls instance methods compared to regular PHP. This article summarizes the results of this benchmark.
Peachpie will fully support the DLR (Dynamic Language Runtime) and provide a complete interoperability with other dynamic languages, as well as with objects that inherit the IDynamicMetaObjectProvider. The present concept of Peachpie utilizes the so-called CallSite and, where possible, it uses primarily a native direct function call.
The CallSite is a part of the .NET framework and implements a support for dynamic behavior on the runtime level. It learns from the running of the program and compiles whatever is supposed to occur as needed while the program is running. As a direct result, the overhead is minimized since we can eliminate the lookup into the hashtables during runtime. The lookup only occurs once and the result is cached for future use.
Wherever the type is definitively known, it is often possible to perform a direct call; this means that the request processing speed is comparable to that of C# code. Peachpie heavily makes use of its type analysis, but it is also possible to further boost the speed manually by using documentation commentaries or type hints.
Merely by adding documentation commentaries, you can virtually increase the performance of your application by multiple times. This can have a significant impact and may enjoy a widespread usage, because this type of code will be backward compatible with PHP as well.
- Windows 10
- Lenovo Thinkpad Yoga 260
- Core i7 6500U
- 8GB DDR4 RAM
We have decided to include PHP 5.6 in the benchmark for the sake of interest and completeness. As expected, PHP 7 is considerably more performant thanks to the new implementation of hashtables.
Peachpie can yield two results – with a known type $x and when the call is postponed to runtime. If the type analysis or a documentation comment determines that $x is a Dog, the speed is approximately 50 times faster. Otherwise the behavior will fall back to using the CallSite, where even now the speed is already significantly better or at least comparable with PHP 7, depending on the particular example.
Instance Method Call Speed Benchmark:
As the above diagram suggests, the difference between Peachpie using direct call and PHP 5.6 is difficult to visualize. For our test with 100,000,000 cycles in the loop the performance of Peachpie and direct call is a staggering 0.0875 sec, and even Peachpie and CallSite performed at 2.440 sec. PHP 7 shows how significantly more performant it became, with a speed of 7.644 for this microbenchmark, whereas PHP 5.6 needed 152.542 sec for the same task.