Hacker News new | past | comments | ask | show | jobs | submit login

Could CPython take a page out of this playbook and become faster?

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.




The part that is relevant for CPython is implemented by it since at least 2.0. The rest is horrible hack that involves abusing shared memory to share what effctively are parts of GC'd heap between unrelated interpreter instances. Usefulness of that hack is completely predicated on PHP's execution model where there is no persistent application instance and everything gets torn down after each request.


I actually love the approach to tear down everything after each request.

It makes reasoning about the system much easier.


I first came across the term 'shared nothing' in... 2000 or 2001? (John Lim from adodb project, but I'm sure he didn't coin it). There is a difference in thinking required for shared nothing, but as you say, it does typically make reasoning easier - fewer moving parts to potentially complicate your processing.


What I love about this approach is that if I fuck up somewhere specific and a user triggers that part then the whole app doesn't go down.


This actually reminds me a bit of CGI ( https://en.wikipedia.org/wiki/Common_Gateway_Interface ) or its later variants.

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).


PHP's programming model definitely should remind you of CGI, because it was a CGI module: https://en.wikipedia.org/wiki/PHP#Early_history


Until you need to talk to db, where you may want to have a persistent connection, so hacks required in php-fpm to support it.


PHP with Java std lib is called JSP, with .NET it's called ASP; the request model is similar and the packaging can be quite minimal, even if the good practice favor complexity.

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.


> would be something a bit like PHP, but with the standard library of .NET

Try the PeachPie compiler then ;)


Indeed. But the difference between modern Perl/Ruby/Python/Java/Whathewer frameworks that use some kind of application container and PHP is that in the first case you know what is shared and work in the world with shared things, while for PHP there is shared nothing except for implementation defined shared random things that you have no idea that might be shared state.


To do this in Python, couldn't you just fork the Python process after initializing the web framework etc. and handle the request in the forked process? Optionally keep a pool of pre-forked worker processes so that the request doesn't have to wait for the forking. Sort of like Android's zygote process.


In principle, yes. But forking an application server gets tricky -- some resources (like database connections) aren't fork-safe, and need to be recreated after forking.


Fork is kind of expensive in most OS, even it's relative cheap compared to start a new process from scratch, but still much slower than few function calls.


Well, yes, but the point was to get a process per request just like PHP, so you'll have to start those processes one way or another.

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...


The whole PHP opcache hack works reliably only on platforms that have fork(2).


Me too, it's amazingly simple. It's a radically different language but if you really like that, you should check out Elixir/Phoenix.


Seconds? What libraries are you using?

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.


Seconds are extreme examples of course, where heavy data processing is taking place.

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.


https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

There's a small set of benchmarks between php8 and python 3.9. Your 'guess' isn't too far off.


I have tried to move some stuff from PHP to Python (using Flask for example - I don't want to imagine the performance of Django) and the difference in performance is pathetic. Especially given the reputation of both languages. Python is still easier so if you can throw money at AWS it is a better option.


Were you maybe using Python on AWS Lambdas? That would be relatively slow, since it takes away the advantage Python/Flask would have vs PHP...that it's already warm.


In PHP 7.4 PHP can be configured to precache (typically framework) files. Thus with mod_php (as opposed to FPM), Apache and the Zend engine are already warm.


Can you be more specific and list out why 'Python the language is so much better than PHP'?



This article is horribly outdated, and generally very ignorant. This site was the response to that blog, https://phpbestpractices.org/


It's not saying anything about why python is 'good' or 'easy' or whatever, which was the original claim.

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.


Note the year on that post.


I think that's part of the plan to accelerate Python that the Microsoft team has.


I haven't followed this. What plans does Microsoft have? All I know is that they pulled out of PHP recently after supporting its development for quite a few years.



Thanks! (And oh, /. always has this historic vibe ...)


They didn't pull out, they used to provide a php windows build which is now handled by the community instead on azure resources donated by Microsoft


Sure they pulled out. They had different developers over time on their payroll and stopped that.


> the low performance of CPython is hard to swallow

African or european swallow?




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: