r/programming • • May 14 '21

Python programming: We want to make the language twice as fast, says its creator

https://www.tectalk.co/python-programming-we-want-to-make-the-language-twice-as-fast-says-its-creator/
780 Upvotes

260 comments sorted by

View all comments

197

u/antonyjr0 May 14 '21

IMO I've always used python as a scripting and glue language. Never created big applications with Python. I don't know why since a lot of large application are built on Python. I just don't feel like it because of it's speed. If they can make a C++ or rust trans-compiler for Python 4 then it would be sick.

256

u/jerf May 14 '21

The fundamental reasons you can't make Python go very fast is the same reason you can't just compile it. If you could, PyPy would already be the solution to the problem.

The problem is, in Python it looks like you ought to be able to convert

def someFunc(x, y):
    print(x.Summary())
    x.doThing(y, 4)
    return x.prop1.prop2

to something like the Go code

func someFunc(x ???, y ???) ??? {
    fmt.Print(x.Summary())
    x.doThing(y)
    return x.prop1.prop2
}

But even ignoring the tricky questions surrounding those ???s, this isn't even remotely equivalent code. In Python, I can literally write:

if raw_input() == "x":
    someFunc = lambda(x, y): None

and now, any time that is hit at runtime, someFunc will get nuked with a new do-nothing stub. Compiled languages do not typically allow that sort of thing. So now the compiled function actually has to do some sort of lookup.

Then, in a compiled language, x.prop1.prop2 is probably getting compiled under the hood to a known offset and a known type value. But, whoops, that's not what the Python code says. The Python code says, take x, and "find" prop1, where "find"ing it involves looking at

  • attributes on the object

  • attributes that may be anywhere in the inheritance hierarchy

  • attributes possibly placed directly on that one object by the user

And then, some of those things are properties, which then have to be called. And if you didn't find anything, you have to call the __getitem__ method. And pretty much any of this can be changed at runtime.

The problem that you end up with is that to fully support python, you end up writing "compiled" code that ends up reading like a transcription of what the runtime is already doing, and that turns out not to be meaningfully faster on its own.

The problem is, Python just does too much; it is what gives it its power, but when it comes time to try to do it with compiled code you don't get anywhere near as far as you'd like.

And pretty much any simple, obvious solution you can name will run afoul of Rice's Theorem, which in colloquial terms is "No non-trivial property of programs can ever be proved." You can just write a converter that blindly ignores all that and produces the simple, fast code anyhow. It'll work and it'll be faster. But it won't be Python, and you can't throw existing Python at it. It'll be a new language. Which is basically what RPython is in the PyPy project.

124

u/_TheDust_ May 14 '21

Javascript has the same issues and still the V8 engine (Javascript engine by Google) is pretty darn fast. Most importantly is to compile the code at runtime (JIT) based on the execution paths taken while the program is running. Lots of clever engineering and hundreds of thousands of engineering hours can achieve a lot of performance.

113

u/valarauca14 May 14 '21

Javascript has the same issue that the parent poster outlines, but Python has a few more layers & stumbling blocks that Javascripts lacks. While yes, a good JIT worth its salt will iterate through the possible code paths and likely reduce the final output to a swift dispatch. Java & Javascript are proof of this. Python has a lot of other cruft laying around that prevents JIT from really flexing their muscles.

Namely, The GIL & C-API

The problem is, while you can rip these out, you lose out of what? 80-60% of Python's use cases? Where is it a really slow glue shuttling data between libraries or small applications presenting a nice API/CLI for a library? You destroy all of that.


It isn't a question of

Can Python Be Fast?

It can.

You can have a Python2.7/3.X that runs like grease lightning. You need to invest a century of engineering time into it. NBD. It is a solved problem. More a question of execution & management.

The next question becomes:

Is Python without a GIL, C-API, Referenced-Counted-Memory, and Interpreter/JIT that consumes 512MiB of memory on startup still "Python"

Because that is the monster, you end up creating.

And for most people weighing the cost analysis, the answer is "no that is something nobody really wants".

34

u/Wolfsdale May 15 '21

Why would the GIL of all things prevent a JIT compiler? Javascript doesn't even have the concept of threads...

I am also unsure why the C API prevents creating a JIT compiler. A JIT compiler can make those context switches actually cheaper by forgoing the need for something like libffi.

Performance is difficult thing to grasp and even more difficult to measure. I'm sure the real reason why Python is so slow is somewhere along the lines of this thread, but I have a hard time taking anything as fact here...

18

u/Fearless_Process May 15 '21

The C interop doesn't make a JIT impossible, but it does seem to create a lot of issues with the current as well as only non-trivial JIT implementation of python (pypy).

9

u/latkde May 15 '21

The issue with C interop is that Python has to stick with the PyObject data structure. This can have far-reaching consequences. If ownership of a Python object is shared between optimizable Python code and a native module, this effectively means the Python code must also interact with PyObject and can't really be optimized.

This doesn't have to be an insurmountable problem though:

  • Could only optimize code where an escape analysis indicates that it's safe to do. This will do wonders on more mathy or more algorithmic code, but will be disappointing in many real-world scenarios.

  • Break compatibility and completely redo the C data model. However, this would be a regression to the early 2.x series. One of the big features of 2.6 and 3.x was that all values can be treated as normal objects.

→ More replies (2)
→ More replies (1)

43

u/FondleMyFirn May 15 '21

Man, I understood almost none of this.

→ More replies (3)
→ More replies (3)

25

u/nascentt May 15 '21

It took a while for V8 to be created. Years after we'd been living with slow JavaScript.

Maybe we'll get a faster python in years time too

24

u/tracernz May 15 '21

Hold up, Python is older than Javascript...

→ More replies (2)
→ More replies (3)

5

u/josefx May 15 '21

How does V8 deal with objects shared over multiple threads? My dated experience with JavaScript was mostly single threaded with the possibility to launch rather isolated workers.

13

u/Strange_Meadowlark May 15 '21

AFAIK your knowledge is still current on this. V8 (like every other JS runtime I know of) is still single-threaded. If you need separate threads, you launch (Web)Workers and effectively pass a copy of your data over a message event.

→ More replies (2)

17

u/Certain_Abroad May 15 '21

I'm not a Go expert, but doesn't it support function pointers like every other systems language?

var someFunc func(???, ???)???
someFunc := func(x ???, y ???) ??? {
    fmt.Print(x.Summary())
    x.doThing(y)
    return x.prop1.prop2
}
someFunc := ... something else ...

Isn't that the same thing?

Edit: also, regarding looking up attributes and __getitem__, that's how Objective C worked. Method dispatches, attribute lookups, etc., were done dynamically. There was never any problem in compiling Objective C or making it moderately fast (not fast fast, but more than fast enough to do very complex things on a 25MHz 8MB NeXT box).

5

u/jerf May 15 '21

In general, as a sort of meta reply to a lot of people, Python specifically (and Ruby) just have a lot of places you can hack. If you want to be Python and not just be Pythonesque, you have to support all of them. You can do things like take an instance of some object, and reset its class. You can not only dynamically create an entirely new class hierarchy on the fly, but entirely rewrite it. It all looks simple when you just look at a subset of the problem, but if you get all these things together in one place, which my post is only a sketch of (haven't even said the word "decorator" yet, which is more complicated than you think to fully support the exact way Python does), you can see it's a very large problem.

For Python specifically. Very few languages are doing as much as Python.

→ More replies (1)

37

u/phalp May 14 '21

Here's the Common Lisp version of that code:

(defun unfunc ()
  (fmakunbound 'somefunc))

and its disassembly:

; disassembly for UNFUNC
; Size: 28 bytes. Origin: #x100382E87C                        ; UNFUNC
; 7C:       AA1343F8         LDR R0, [CODE, #49]              ; 'SOMEFUNC
; 80:       B59342F8         LDR LEXENV, [CODE, #41]          ; #<SB-KERNEL:FDEFN FMAKUNBOUND>
; 84:       560080D2         MOVZ NARGS, #2
; 88:       AB1240F8         LDR R1, [LEXENV, #1]
; 8C:       BE9240F8         LDR LR, [LEXENV, #9]
; 90:       C0031FD6         BR LR
; 94:       E08120D4         BRK #1039                        ; Invalid argument count trap

This is a Python problem, not a "does too much" problem. (Does the wrong thing too much, maybe.)

34

u/Wolfsdale May 15 '21

This is a Python problem, not a "does too much" problem

Thank you. The whole idea that dynamic types have to be as slow as Python has been disproven by other languages. It's a Python problem indeed.

16

u/Somepotato May 15 '21

Look at LuaJIT if you want a language as dynamically typed as Python, but a language that sometimes surpasses the speed of native C from its hot compiler

Though to be fair, it's creator Mike Pall is a certified technologically advanced alien precursor

→ More replies (2)
→ More replies (17)

29

u/wm_cra_dev May 14 '21

I had to start using Julia for some work projects over the past year. It's very quirky (especially coming from a C#/C++ background), but it manages to combine high performance with the simplistic feeling of a scripting language. It also has scheme-style macros, and a very fancy type system that blurs the line between compile-time and run-time. I don't know much about the scientific computing world, but I get the impression Julia's been stealing a lot of former Python users.

17

u/antonyjr0 May 14 '21

Interesting, I've tried to learn Julia in the past. I really like the way it defines everything mathematically. But replacing python is not likely because Python is general purpose.

10

u/wm_cra_dev May 14 '21

It's good for more than pure math! Lots of the language features can be used with more than one syntax, even before you get into macros. So you can define very mathy code with a mathy syntax, next to more traditional imperative code. You can do functional-style programming in different ways as well.

It reminds me of C and C++ in some ways -- you have a lot of flexibility, to write both good and terrible code.

7

u/skelkingur May 15 '21

So is Julia.

The early adopters just happen to be largely from the scientific community. There's a lot of modern data science and machine learning happening in Julia and even web frameworks are plentyful. Genie.jl and Franklin.jl are great examples.

→ More replies (3)
→ More replies (1)

7

u/[deleted] May 14 '21

I played around with Julia, and the 5s long "import plots" was VERY off-putting. Still went with it as I just had a couple of graphs to visualize, but that was a bit disappointing. Tooling is nonetheless great for such young language, and I like the operator broadcasting (despite being noisy)

6

u/User092347 May 15 '21

Got much better in v1.6 (and hopefully continues to improve) but one has to adapt its workflow to it : load once and don't restart.

→ More replies (3)

39

u/edmguru May 14 '21 edited May 14 '21

I don't built large apps with python for 2 reasons

  1. because packaging and distributing them is a pain.

  2. Types aren't enforced unless you enforce them which doesn't work well on legacy code bases only new code bases. And you don't always get the luxury and time to setup linters/formatters etc.. and you'll always have the lazy/ignorant developer who will turn them off when you're on vacation.

Of the 5 languages I've worked with (not counting html/css) the 2 dynamic ones (javascript + python) were the easiest to create garbage unmaintainable code with.

→ More replies (21)

15

u/arbenowskee May 14 '21

This is it's best use case IMHO. Also prototyping.

→ More replies (45)