68 points · by cpeterso · 4y ago · 75 comments
Speeding up Python has been a lucrative business for more than 10 years, with dozens of projects and very little results.
I'd just use another language if speed is the issue.
But it's probably everybody that is wrong, there is no way Python has a value proposal that is worth something to a lot of people.
There are two types: Web and desktop. Both are a mess.
On the web the "easy button" is to pay for a dedicated service that is setup to specifically deal with the fact that deploying a Python/Django app to a Linux server is a nightmare and a mess. You can deploy a PHP application to any server you care to choose from any hosting company in the world. Grab a cheap GoDaddy server and off you go. It's that simple. With Python? Forget it.
To some extent I blame this on the simplistic integrated development server trick. Sure, it's great, you install Django and you can see a site on your computer with no effort. And then comes the reality of moving whatever you created to the real world.
On the desktop, well, again, it's a mess. As much as people love to dump on Visual Basic, the one thing they got absolutely right was that you could develop an application, test it locally, and have it in installable form to use internally or send to customers with zero effort. Back in the day we created dozens of tools, both for internal and external applications both with VB and VC++ (these were Windows-based) and Objective-C for Mac. Python does not make this simple at all.
In my opinion, Python developers really need to take this aspect of how software is uses seriously. The only use for a language in isolation of creating tools people can use is in academia. Outside of that, we need to be able to deploy applications, be it web, desktop or mobile. Sometimes it seems zero thought or work has been given to this reality.
How crazy is it? Try to create just a simple desktop console application. Something silly, like a console-based calculator or hangman game. Now try to package it in installable form for delivery to Mac or Windows users who need to double-click on an icon and install/uninstall the application as they would with anything else on their systems.
Why can't Python do that when VB was doing it decades ago?
Cinder: Instagram's performance-oriented fork of CPython - https://news.ycombinator.com/item?id=27043217 - May 2021 (261 comments)
https://twitter.com/stylewarning/status/1571585280217579521
What boggles my mind is that Python has had billions of dollars of direct and indirect investment, with participation from every nook and cranny of the software syndicate—academia and industry—yet... this is all you get.
- it didn't get billions of dollar of investment
- it was born 30 years ago in a different time and carry the weight of those decisions and legacy
- it only got serious focus and investments the last decade, while alternative got 100 times more
- "this is all you get" downplay the enormous qualities of python to focus only on what's wrong with it. It never was in a situation to have success by default, it gained market share because it was good. No monopoly on the browser, no jump start at the begining of the web like php, no .net style founding by a giant. Somehow, the author must ignore some inherant good stuff to the language.
Seriously, use a faster language. If you need a performant fork of Python, you're using the wrong tool for the job.
They've very likely made their calculations, and assessed that paying engineers for speeding up the language, was overall preferrable to rewrite part of their (very large) systems and potentially extract them into separate services. It's unlikely that they just cargo culted.
Github/Gitlab, interestingly, did not take this road, and extracted slower part into Go services.
Stripe had the (IMO) most interesting idea - write a (Ruby AOT) compiler for the slow paths.
It'd be very interesting to see the precise/concrete considerations behind these choices, since generic considerations ("we love Ruby" :)) aren't very informative.
You see the same problem in Python with regards to correctness - it's hard to refactor Python or change a large codebase and have it keep working correctly, so huge Python projects tends to ossify.
It may be a rational solution only in the short-term, but still an objectively bad solution overall.
They now are in the optimization phase. They first picked the low hanging fruits, but after 10 years of doing that they need to fine tune. For them, gaining 20% of speed can add up to millions of dollar a day, so it's now the new priority.
Just like with JS in the 2000', today they have to decide if it's worth rewritting python code or just improve the platform. For some projects, they made the calculations and it was just cheaper to improve the plateform. We got V8. Now Python is getting the new focus.
Indeed this experience eventually made us jump into .NET as we had early access to it as Microsoft partners, and a couple of years later some of the people on this team went off and founded OutSystems.
20 years later, apparently this lesson of using scripting languages without dynamic compilers seems to have to be relearnt the hard way.
Awareness that organisations often end up in these problems should be considered when choosing the language for a green field project.
The community didn't have a lot of resources for a long time, and so had to prioritize: it chose to focus on python strenghts, and not compensate for weaknesses. It couldn't do both. Keeping the implementation simple was also a way to keep the project manageable on the cheap (and by cheap in mean in work hours, since the first 15 years, most core devs were just doing it for free).
Today, a lot of companies with a lot of money have much to gain to invest in python, and so we can finally have our cake and eat it too.
1. Python has a ton of C code in its libraries/stdlib, JS did not have that for the vast majority of its usage (browser). Optimizing across the FFI boundary seems unlikely to be a reasonable goal and may complicate optimization opportunity. Let's say you want to do some sort of optimization pass to make sure that `str.append` is not getting overridden - how are you going to handle native code?
2. Python is obscenely dynamic. Runtime generation of classes is not uncommon at all - for example, `@dataclass(slots=True)` creates an entirely new class.
You can overwrite methods, functions, there's heavy use of globals in libraries, no such thing as private implementations, hooks into everything, even importing code has side effects so you can't reorder or lazily import automatically, even reordering imports is unsafe in Python.
It seems really hard to optimize Python at all, and I wonder if we could ever reasonably expect to see a "V8-like" runtime, even assuming that there's funding, which I also doubt. Unless you're willing to break the language I just don't see a Python runtime overcoming these issues. Or introduce something like 'strict' where these things aren't possible.
Some of the best engineers at the world's leading tech companies need to "pick another language", obviously.
Python has a very good set of data structures and implicit casts, that is the main reason it is popular. Compare to jagascript for example... Anyway, making python fast while keeping all those casts and data structures is very hard, Javascripts casts and data structures are bad for the programmer but very good for the interpreter so is much easier to optimize which is why it runs orders of magnitude faster.
When you spend more time io bound, it’s not an issue. If your python code is choking on cpu time, a lower level language is probably more appropriate.
For example, do you think that python is an appropriate language to write pylint?
* It uses an interpreter, which is slower than compiled code
* All objects carry a dict to contain arbitrary attributes - this creates huge overhead
* Even when the dict is suppressed with __slots__, all containers (all classes, even!) are untyped, so extracting any field causes dynamic dispatch
* Objects cannot be stored inline in containers
* Integers are variable-sized, and thus slower.
Of course, you might counter that one could simply make a version of Python which is compiled, where all classes have a well-defined typed layout, where some classes can be immutable for performance, containers are typed, integers are machine integers, etc. But then I'd argue you're talking about a completely different language (Julia fits this description perfectly)
Fun fact, when the creator announced python beta he clearly said it was for building prototypes
Hard sell without huge investment.
It's common in science to see computer scientists implement domain-specific software with a hopelessly naive understanding of the domain, leading to biased, wrong, or misinterpreted results. Like, go to any bioinformatics conference and you will find these people.
"What do you mean I can't just use a database of clinical pathogens to make a tool that generalises to all bacteria?"
Given what insane performance has been extracted from Javascript, I'd expect the limits for Python being within an order of magnitude from C++/Rust.