How PHP Opcache Works
npopov.com
npopov.com
Whatever PHP does (I am not a language expert), they are doing it right. Whenever I write projects in PHP and think "If this approach is too slow, I will optimize it later", I get surprised like "WOW... it takes just a few milliseconds!".
Whenever I write projects in Python and think "This should not result in a noticeable workload", I get surprised like "Uhmm.. this takes seconds?".
Python the language is so much better than PHP. But the low performance of CPython is hard to swallow. In the context of answering web requests, there is no room for execution times of more than a few milliseconds. So as things are, Python development is way more costly because you constantly have to optimize performance bottlenecks.
I develop professionally in PHP but use Python for most of my personal projects, and for scripting text files. Sometimes I'll feel a few hundred miliseconds' delay and be surprised, but unless we're talking about a loop that probably should not loop seconds is a long time.
If you could share more of your workflow I'll be happy to advise.
My guess is that CPython is something like 4x slower for typical code and even slower if most of the code is control structures like branching, loops, function calls etc.
There's a small set of benchmarks between php8 and python 3.9. Your 'guess' isn't too far off.
It makes reasoning about the system much easier.
Personally, i think that PHP's request-response workflow is infinitely easier to deal with than that of servlets in Java or whatever abstractions other technologies out there have, even if only when scaling isn't a concern (which is perfectly passable for some systems out there).
My ideal language for getting things out the door would be something a bit like PHP, but with the standard library of .NET/Java (LINQ or Streams in particular), the wider ecosystem of Python, simplicity of Go and a decent type system on the top of it all, that'd process requests much like PHP does (or at least would only make you think it does, though abstractions that are leaky are arguably worse than none at all).
Also I don't think that you can have a simple language like Go and a decent type system like Typescript, to me those properties are in opposition.
Try the PeachPie compiler then ;)
Anyway, it appears uwsgi can be configured to do this, using fork-server[1] and maybe max-requests[2], although it only works with Perl at the moment.
[1] https://uwsgi-docs.readthedocs.io/en/latest/ForkServer.html
[2] https://uwsgi-docs.readthedocs.io/en/latest/Options.html#max...
Personally, I find very little attractive or easy or 'good' about Python. I've used it on a few occasions, and can get by, but pretty much nothing that people find attractive about it resonates with me. Couple that with answers to "why do you like python?" answered with "php sucks!" or "ruby is slow" or similar, and nothing much makes me want to dip my toes in that particular pool.
African or european swallow?
First, the "opcache.force_restart_timeout" setting kills scripts upon reaching a timeout, independently of the well-known "max_execution_time" php.ini directive, but only from time to time. It is unpredictable and doesn't leave a comparable message in the error_log.
Also, for a long time in the PHP 7.x days there was (still is?) a race condition in the SHM-to-file-and-back-to-SHM sequence that caused weird errors in programs that were executed at an unfortunate moment during the restart. A single bit would flip in a class name, function name or filename, and the script with the bit-flipped name would be cached instead of the original. Imagine the_post() in WordPress suddenly changing to the_qost(). The entire site would grind to a halt until the corrupted entry was evicted from the cache, and in the meantime, error messages would refer to nonexistent functions in nonexistent files. Moreover, because the bit flip can happen to any name, it's hard to search for a clue -- a real head-scratcher! As far as I'm aware of, none of the related issues in the PHP bugtracker has been conclusively fixed.
Solves this issue elegantly.
It's also the kind of workaround that isn't applicable to the vast majority of PHP sites out there: beginners running WordPress on a single box. One day, you update a few plugins like a good security-minded person should do, and this causes a bunch of cache invalidations. Bam! The site is down and the error messages aren't helpful. It really doesn't help bolster PHP's reputation as a stable, beginner-friendly language.
Even up here in the clouds, there are many times more people who rely on one-click WordPress-preinstalled droplets than there are people who understand how to correctly juggle multiple droplets to deploy updates without downtime. Besides, a bug in the PHP runtime is a bug in the PHP runtime, regardless of whether the user is following today's best practices.
Migrating to K8s-based deployments was the effective workaround, but before that it was not fun to have that sort of unreliability.
Restarts are an area that still doesn't have much in terms of automated tests, but at least it's covered by fuzz testing.
Anyone have stats on this?
PHP is absolutely relevant, I'd think, by many peoples' measurements. But... we're on a 'hacker' site and people love to dump on PHP. PHP powers a large portion of internet sites. One estimate from w3techs is ~80% of sites with a known server side language are PHP.
Yes, much is WordPress. If you estimate that half of that is Wordpress, that still would leave... ~35%-40% of sites using PHP. Maybe even that is too high. What if it's 30%? That's a large number, both in terms of percent and absolute numbers.
Yes, PHP is 'relevant'. The ecosystems (Symfony, Laravel, etc) are pragmatically iterating, evolving goods ideas from a wide spectrum of web platforms. The language itself is similarly pragmatic, adopting new features, deprecating legacy stuff, and moving forward. The language is roughly 3x faster than it was 10 years ago, while retaining a lot of backwards compatibility.
The language itself along with various frameworks offers a good combination of speed of development, flexibility and rigor. The 'shared nothing' default makes it easier to reason about for many problems. Need to write a small shell script? Easy in PHP. Need to scale up to large loads? PHP can do it. There will be scaling concerns in any large setup - a PHP platform will likely have similar problems that other language/stacks will encounter at similar load.
So again, yes, it's relevant. There will be some other response saying "no", and then ... we'd have to define what you mean by 'relevant'... :)
Popular? Growing? Evolving? Uptick in usage for many problem spaces? Easy to scale?
I'd say 'yes' to all of those.
Headless WP is becoming very popular too, and it seems to fill the same role. I've never used it, but I would imagine it takes just as much knowledge to build an SPA as any other backend framework like Laravel or Rails.
No Linux terminal knowledge need it or worry about Nginx, SQL, or iptables configuration.
Yeah most of those cpanels hostings are bad for scale, but for a lot of new devs allowed them to deploy without having to be a sysadmin
Still compelling for people who see spiky to high traffic and are happy to have scale reduced to an accounting/budgeting problem, of course, and I'm pretty sure Laravel has something like this.
[1] https://aws.amazon.com/blogs/compute/introducing-the-new-ser...
Etsy still uses PHP, as does Vimeo and Wikimedia. A few other launched-in-early-oughts web properties rely heavily on it too (e.g. Flickr).
I still find it has one of the best developer experiences for web applications, out of all the tools I've had to learn through agency work.
PHP is easy to deploy, atleast from the start, but was difficult to use compared to python and ruby. It's deployment model also encouraged people to edit directly on the server, which lead to all kinds of problems. So it got a bad reputation.
I have heard that it has fixed many of it's inconsistencies, but many have already moved on. The ones still using it are either stuck, or happily quiet.
Or maybe I'm just getting fucking old & grumpy ;)
Modern JS (modules, web components, fetch, ...) is rather expressive and powerful, and you absolutely do not need to pull in a framework and twelve third-party hype technologies to get a small project up and running.
Only to mention a few of the newest and hottest PHP tools. Then you have all the heavyweight, widely tested and used like Symfony, the Laravel ecosystem, Yii Framework, etc.
PHP has never been better and healthier.
I'm not a Python developer, but it is probably true that Python is a better designed language and has more bells and whistles as language, like decorators, operator overloading and modules. Python is also used in more domains than PHP.
But if you look past some of the weird design choices, bad reputation and the packaging as web only language, you will find a JIT-ed & typed dynamic language with good performance, FFI for easy C glue, good threading performance that is not suffering from a GIL like Python, a large & mature standard library, can be used everywhere from a small script to a large monolith, a true diamond in the rough.
Today I worked on a importing system that takes an HTTP stream to read a CSV file line by line to mangle it and rewrite it simultaneously to a FTP stream, never keeping the entire file in memory nor downloading it entirely to disk first, and by only using functions from the standard library.
I'm not saying you can't do this with Python, you probably can, or any other language, I just want to dismiss the idea that PHP is only suited for typical web tasks, PHP is a versatile tool that has every potential to expand outside of its traditional role.