But Why?

Almost every post, article or thread on Reddit about PeachPie is inevitably met with someone posting the famed Ryan Reynolds GIF asking “but why?”, so instead of explaining each time, we decided to put our entire motivation behind this project on paper so that we can link to it.

A little bit of history

As some of you might know, PeachPie is heavily based on a project called Phalanger, which originates all the way back in 2005, when two students of the Charles University in Prague – Tomas Matousek and Ladislav Prosek – decided to explore whether it would be possible to compile a dynamic language like PHP into a static, type-safe framework like .NET. If you want to watch an extremely old school video of the two explaining how Phalanger works to Microsoft employees, click [HERE]. As a side note, both Tomas and Ladislav now work at Microsoft, and Tomas is in fact one of the lead developers on the Roslyn platform.

Long story short, the motivation behind running PHP on the CLR back in 2005 was fairly obvious: performance. This was during the years of PHP 5 (or even earlier) and PHP was nowhere near as fast as it is nowadays, so the allure of compiling to the performant and managed .NET was clear. Phalanger went far beyond a simple university project and was developed for several years, during which other maintainers started working on it, e.g. F# legend Tomas Petricek and Jakub Misek, who ultimately became the lead maintainer and also ended up starting the PeachPie project. Phalanger received a lot of recognition and also achieved some commercial success – for example Jadu, a well-known UK CMS provider built its platform completely on top of the compiler.

Phalanger, however, faced a few issues. Firstly, CI/CD was not yet popular, Docker was at best an idea in someone’s mind and most importantly, it only worked on Windows. Any cross-platform capabilities had to be achieved by targeting the Mono Framework, which wasn’t as simple as it sounds, and consequently, there were quite a few issues with this setup. While Phalanger did run real-world apps, such as WordPress, MediaWiki or phpBB, and achieved a performance boost compared to standard PHP, the peripheral tooling was not yet mature enough for the project to achieve widespread usage.

Enter: PeachPie

Phalanger continued in maintenance mode for several years, with minor bug fixes and support of existing customers, but major development ceased sometime in 2013. A few years went by before Microsoft shipped two completely groundbreaking innovations that changed everything for us: Roslyn and .NET Core. Roslyn meant that instead of writing the entire compiler architecture from scratch, projects can simply use the provided APIs and benefit from a common compiler platform, which is maintained by some of the best software engineers in the world and constantly being improved. Additionally, .NET Core was introduced – a lightweight, highly performant and cross-platform framework heavily inspired by Mono. In other words, if we re-designed our architecture to work on top of the Roslyn API and as long as we were compatible with .NET Standard, the target framework was only a matter of changing a single line of configuration, and all the sudden the project would work regardless of operating system or device – including watches, IoT, phones and whatnot (thanks, Xamarin).

So, what are the objectives?

In 2016, we decided to give the PHP compiler to .NET a facelift and in order to distinguish it from the already obsolete Phalanger, we gave it a new name as well. So, even though we certainly re-used many of the functions and libraries already implemented in Phalanger, why on earth did we decide to re-implement the entire PHP language in C#?

Performance

PHP7 is insanely fast compared to PHP5. Still:

TechEmpower benchmarks comparison

While PHP’s fastest possible setup on NGINX web servers can serve somewhere around 2.5 million requests/sec, ASP.NET Core is currently at around 7 million, and improving by double digit percentage points every single minor release. PeachPie is already one of the fastest PHP setups with 425k req/sec, but still far far behind the full potential of ASP.NET Core. Long story short – right now, PeachPie’s performance is, on average, equal to or slightly lower than PHP 7, but the potential for improvement is endless, and we have yet to properly optimize for speed. So even though PHP is nowhere near the performance bottleneck it used to be, the prospects of running your average PHP website 3x as fast are still quite attractive.

Security

It’s fair to say that PHP has a bit of a reputation with regards to security. Some small part of this is perhaps due to the nature of the language itself, but the bulk of the issue is simply the amount of PHP code out there, not all of which is necessarily well-designed given how easy it is to get going in PHP. WordPress alone powers 35% of the internet, and its massive plugin ecosystem gives room for plenty of security mishaps. There are some ways in which PeachPie addresses a number of these issues:

  • Diagnostics: the static analysis during the compilation may be a little bit more strict than, saym PHPStan’s or PsalmPHP’s, but nevertheless, it allows us to throw a whole bunch of diagnostics, tooltips, error underlining etc. your way while you’re actually coding, without having to install or configure any additional tools, directly within VS Code or Visual Studio.
  • Debugging and profiling: while you’re able to debug PHP code reasonably well with Xdebug, why not take advantage of the industry standard .NET debugger? Since PeachPie runs your PHP code on .NET, all the tooling is available as well, including the powerful and accurate .NET debugger and, especially with our Extension for Visual Studio, the VS profiling tools.
  • Managed, type-safe and compiled: the advantage of a compiled application is that nothing that’s not compiled along with it will be executed on the server. One of PHP’s main benefits, the fact that you can change stuff live on the server and instantly see the result, is also one of its biggest security problems.
  • Source-less, securely signed distribution: PeachPie allows PHP libraries or entire apps to be distributed as securely signed NuGet packages. The great part about this is that all of the application’s source code is compiled into a DLL and the original sources are actually not distributed along with the NuGet – this gives you added security.

Interoperability

By far the most frequently appreciated usecase of PeachPie is its ability to bi-directionally interoperate between PHP and .NET. In plain terms: you can natively use PHP libraries, classes, apps and functions in your C# projects or, vice versa, reference C# directly in your PHP app. The two can work side by side within a single solution, and they can be debugged seamlessly with no marshalling, overhead or costly API calls. The same is true for developers: PHP developers can easily work with .NET developers; they can each work in the language they are most used to and PeachPie offers a common ground that allows both languages to communicate with each other.

The main thing we’re excited about in this regard is the potential to re-use the vast array of existing PHP libraries and applications in the .NET ecosystem, where components and quality libraries are rare or very expensive. Instead of having to rewrite stuff from scratch, why not simply use something that’s tested, proven and maintained and save yourself a ton of time and money? We truly do believe that the PHP ecosystem has a lot to offer, and while there surely are badly written libraries, there are also plenty of perfectly well-designed and functional ones that can now be available to the .NET ecosystem. In order to make this process smoother, we’re currently preparing a great service that will allow you to use many of PHP’s best libraries natively in your .NET apps, so stay tuned.

In addition, even the most hardcore .NET fans will have a tough time arguing that you’d rather build your simple e-shop or website from scratch in C# in a questionable CMS as opposed to simply firing up WordPress and installing a couple of plugins. Sorry, folks, you can say all you want, but if we’re not talking business critical apps under heavy load, PHP is the king of web technologies by a country mile. But it’s understandable if you don’t want to maintain an additional language if the rest of your stack is Microsoft-based; so PeachPie offers you the possibility of using, for example, WordPress as the frontend of your application and connecting it natively with the rest of your infrastructure under one common runtime.

Whacky exotic use cases

Although we don’t foresee any of these becoming a huge hit, there is some motivation to pursue at least some unusual utilizations of the PeachPie technology. Some of the previously unthinkable use cases of PHP that are now possible include:

We occasionally mess around with these whacky use cases to prove the concept, but we’re generally not focusing on them too much. However, it’s still (in our opinion) interesting for these new technology trends to be available to PHP developers.

So there you have it, this is why we’re developing PeachPie and we hope that at least some of it makes sense to you. If not, that’s ok, we’re enjoying the work on our project regardless, and we fully embrace that not everyone will find it useful.

Share this article:Share on FacebookShare on VKPin on PinterestShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit
Posted on June 20, 2019, in category Information, tags: , , , ,