News for hackers. Stuff that matters.
Re:Python-based compiler achieves orders-of-magnitude speedups by ar9av on Wednesday March 15, 2023 @06:06AM (#35165961)
There are other python implementations like pypy which includes a JIT (Just In Time compiler). There are other jit which can run with official python (cpython) like numba (not all code can be optimized, but if you only need optimize your hot code path).

You can use a superset language of python called cython that generate C code. It can be used to generate C bindings or fast python (for cpython) modules implemented in a python like code.

You can use a really fast language like C, Rust, C++,... create python wrappers with cython, swig, Boost.python, cffi,... and use python like glue code.

Python is not a fast languages as others, but there are tricks to make fast programs.

Re:Python-based compiler achieves orders-of-magnitude speedups by a-dub on Wednesday March 15, 2023 @06:26AM (#35166097)
numba seems to hit the sweet spot for most numerics for me. it can get a little annoying with type inference issues but overall it seems the most concise and least hassle for moving loops into optimized machine code.
Re:Python-based compiler achieves orders-of-magnitude speedups by DeathArrow on Wednesday March 15, 2023 @06:56AM (#35166314)
Can you use Django with those optimisations or are they good mainly for scientific computing?
Re:Python-based compiler achieves orders-of-magnitude speedups by doix on Wednesday March 15, 2023 @07:03AM (#35166373)
At my old gig we ran Django and FastAPI with pypy. I don't remember there being too many issues. One thing is that pypy versions lag behind official python, so if you're using bleeding edge stuff, it won't be supported in pypy yet.

That was a couple of years ago at this point, and I've not been in the python ecosystem since then, but I can only imagine things are getting better in that regard rather than worse.

Re:Python-based compiler achieves orders-of-magnitude speedups by rpep on Wednesday March 15, 2023 @07:07AM (#35166405)
You can certainly use it, but whether you see any benefit is going to strongly depend on your workload. If you're doing significant calculations in the API then it might be considerably more performant, but if your API is primarily retrieving things from the database and transforming it to JSON then you're going to be limited mostly by the database latency and so I wouldn't expect major improvements.
Re:Python-based compiler achieves orders-of-magnitude speedups by doix on Wednesday March 15, 2023 @08:22AM (#35167161)
If you are fetching lots (not even 'big data', but a few thousand rows) of data using the Django ORM, you will see a performance difference when using pypy, or at least I did a few years ago. The database can happily return a few thousand rows very quickly, especially if you take care to optimize your queries and have good indexes.

Converting a few thousand rows to python/django objects takes _time_. I can't quantify anything, because it's been too long, but I remember it being fairly significant. When I profiled it, the majority of the time was spent calling __setattr__ a few million times.

Like you said, it depends on your use case. If your queries are slow, then optimize your database queries. But if your queries are fast and your responses are still slow, then investigating pypy is definitely worth it. You can also play around with .values_list or something in Django, so that you get 'raw values' instead of objects (but there's still a cost to building them up).

Re:Python-based compiler achieves orders-of-magnitude speedups by coldtea on Wednesday March 15, 2023 @08:46AM (#35167440)
And yet the same supposedly "io bound" workloads (like "parse request, fetch something from DB, return it as JSON") still have widely difference performance characteristics in some languages vs others, with 10x to 100x requests handled per second...
Re:Python-based compiler achieves orders-of-magnitude speedups by acdha on Wednesday March 15, 2023 @09:17AM (#35167889)
2x, yes. If you’re seeing 100x you’re comparing different things like creating and serializing complex objects versus simple types or using a JSON parser which loads an entire document into objects versus one which only retrieved specific values.
Re:Python-based compiler achieves orders-of-magnitude speedups by coldtea on Wednesday March 15, 2023 @10:39AM (#35168965)
Well, perhaps not 100x in rps, but 10x sure:

Overall top performing frameworks (JS, Java, and Rust) at 650K rps. That's 7x.

Top Python based framework: 86K rps.

A very popular Python framework (flask): 2K rps. That's 325 times worse to the best.

And that's the "single DB query" benchmark: https://www.techempower.com/benchmarks/#section=data-r21&tes...

https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Proj...

Re:Python-based compiler achieves orders-of-magnitude speedups by jstx1 on Wednesday March 15, 2023 @07:28AM (#35166577)
Have a look at Cinder - https://github.com/facebookincubator/cinder - it's Meta's performance oriented fork of CPython that they use to run Instagram (which is a big Django app).
Re:Python-based compiler achieves orders-of-magnitude speedups by robertlagrant on Wednesday March 15, 2023 @11:06AM (#35169445)
I always wondered with Cinder why they didn't turbocharge PyPy development instead.
Re:Python-based compiler achieves orders-of-magnitude speedups by lozenge on Wednesday March 15, 2023 @04:47PM (#35174493)
With a codebase of any significant size the priority is always to maintain compatibility while improving performance.

If you start with an incompatible, highly performant interpreter, the compatibility "distance" is difficult to measure and could create unknown performance cost. For example, PyPy doesn't support C modules due to the differing memory layout.

Re:Python-based compiler achieves orders-of-magnitude speedups by tekknolagi on Wednesday March 15, 2023 @07:15PM (#35175995)
Two main reasons:

* C extensions

* Instagram's forking server model

I gave a talk that touched on some of this last year: https://2022.ecoop.org/details/ICOOOLPS-2022-papers/5/Cinder...

I wonder why the recording is not up...

Re:Python-based compiler achieves orders-of-magnitude speedups by acdha on Wednesday March 15, 2023 @09:10AM (#35167793)
I had little trouble switching a Django app years ago but the results were mixed. Some complicated views and reports saw a hefty win but most of the app was database-limited and optimized to the point that there was no meaningful difference, except that PyPy used more RAM.
Re:Python-based compiler achieves orders-of-magnitude speedups by jstarfish on Wednesday March 15, 2023 @02:58PM (#35173092)
Pypy is great but I didn't find it very useful with Django.

Quick, transactional HTTP exchanges (GET, POST, etc.) aren't really its thing-- there's no time for the compiler to get warmed up; the request is complete before pypy has gotten out of bed.

But if you have to do really complex view rendering (graphs or something) where it would take cpython ~10s or more to process, then pypy will leave cpython in the dust.

Re:Python-based compiler achieves orders-of-magnitude speedups by afdbcreid on Wednesday March 15, 2023 @04:31PM (#35174315)
I've never used PyPy, but shouldn't it warm after the Nth request even if you don't have loops?
Re:Python-based compiler achieves orders-of-magnitude speedups by adammarples on Wednesday March 15, 2023 @10:22AM (#35168722)
One great move I've discovered recently is to simply type annotate the python thoroughly and use mypyc to build a c package
Re:Python-based compiler achieves orders-of-magnitude speedups by wcdolphin on Wednesday March 15, 2023 @01:07PM (#35171380)
Would you mind sharing more details of your experience using MypyC? What domain are you working in, and what kind of effort and speedups did you see?
Re:Python-based compiler achieves orders-of-magnitude speedups by FreakLegion on Wednesday March 15, 2023 @05:56PM (#35175282)
Here's a detailed write-up from one of the Black maintainers: https://ichard26.github.io/blog/2022/05/compiling-black-with.... That's on the low side of the speedup curve, in my experience.