9 ms·
I read another commenter here mention that Python is 50x slower than Javascript. Now browser Javascript VMs underwent enormous speedup efforts over the last 12-
by taylorius 5y ago
I read another commenter here mention that Python is 50x slower than Javascript. Now browser Javascript VMs underwent enormous speedup efforts over the last 12-15 years (Google chrome starting the race). Is there anything inherent to Python's design as a language (or any other technical reason) that prevents such a speedy VM being created for it as well?
- adgjlsfhk1 5y agoThe big difference is that lots of python libraries use C under the hood, and a lot of internals of the language are leaked via the C api. It's a lot harder to do fancy things with a JIT when there are more people observing and depending on the internals.
- gravypod 5y agoFrom a language design perspective I don't think there's much in the way of making up this ground. The hard part is not breaking any existing modules written in C/C++/Rust/etc that depend on the cpython ABI (memory layout and calling conventions of the runtime's implementation). There are jit compilers for python like pypy which are very fast (>5x they're attempting to gain here) but they often break things like numpy that have a lot of native modules.
- londons_explore 5y agoSounds pretty straightforward to get great speedups where no modules are used, and just make module calls slow (ie. you reconstruct the data structures the module will need on any entry to the module). Then release a new 'fast' module API, and all the performance critical modules will soon move over.
- gravypod 5y agoIt might sound easy but that would be a critical component of such a rewrite. Python's standard library is implemented using the same API so much of that would need to be rewritten. You then have the risk of introducing bugs, etc. This post is doing something similar. They're rewriting the internal implementation through progressive refactoring and will then look into more long term incompatible changes that can be wrapped in a translation layer. These projects are difficult as a lot of python code exists in the world and a minor change in behavior can have large unintended consequences due to Hyrum's law.
- faisal_ksa 5y agoYes, it's the GIL [1] [1]: https://en.wikipedia.org/wiki/Global_interpreter_lock https://en.wikipedia.org/wiki/Global_interpreter_lock
- nathancahill 5y agohttps://mail.python.org/archives/list/python-dev@python.org/thread/ABR2L6BENNA6UPSPKV474HCS4LWT26GY/ https://mail.python.org/archives/list/python-dev@python.org/...
- wk_end 5y agoThe GIL just prevents thread-level parallelism - which JavaScript also doesn’t have.
- _bohm 5y agoIt depends on the runtime environment, no? For example, Node supports worker threads https://nodejs.org/api/worker_threads.html https://nodejs.org/api/worker_threads.html
- RussianCow 5y agoWorker threads are share-nothing, closer to processes than actual threads.
- inglor 5y agoWorker threads aren't share-nothing, you can share data through SharedArrayBuffer which lets you share arbitrary binary data. If they were share nothing, we probably wouldn't have shipped them. Source: I'm Node.js core and one of the people who worked on them
- zielmicha 5y agoYou can also share arbitrary binary data between processes by using shared memory (e.g. Python has support for this in multiprocessing.shared_memory module).
- fault1 5y agoI thought this presentation by Armin Ronacher (the creator of Flask and Jinja) was very enlightening: How Python was Shaped by leaky Internals(2016): https://www.youtube.com/watch?v=qCGofLIzX6g https://www.youtube.com/watch?v=qCGofLIzX6g See also this HN post (Attempts to make Python fast): https://news.ycombinator.com/item?id=24848318 https://news.ycombinator.com/item?id=24848318
- orf 5y agoIntrospection is a gigantic issue.
- funklute 5y agoCould you elaborate on that?
- hiptobecubic 5y agocpython's specific implementation is a "feature" that many things rely on, but even without that, python's semantics are very dynamic and it's hard to unbox anything in a way that is guaranteed not to be observable.
- orf 5y agoThere are a few different classes of introspection. The first is commonly used builtins like “isinstance”, “callable”, “getattr/hasattr” etc. The second is the inspect module that builds on that, allowing you to get the stack frames, inspect function signatures etc. The third is trackbacks, which need the stack and themselves are introspectable. The fourth is metaclasses (making dynamic classes at runtime). These are heavily used in Python, and are optimisation barriers. For example PyPy (Python with a JIT) disables optimisations when “inspect” is imported or used anywhere in the call stack.
- funklute 5y agoHow many of these "levels" (not to confuse them with actual classes) exist in javascript? Only the first one? (that said, I guess the fourth one (metaclasses) isn't a clear yes/no, since OO is very different in js)
- Jasper_ 5y agoHard to compare exactly with that classification, but JavaScript only has the first one, and arguably parts of the fourth one. metaclasses isn't really a major optimization hazard, it effectively boils down to hijacking the "class" syntax to instead run some custom code. It's used by things like dataclasses and the Django ORM to use class syntax to make a little DSL.
- matsemann 5y agoSince everyone knows Python is slow, no care is taken to make things faster in frameworks etc., since one assumes the users are fine with the speed. If "they wanted it to be fast, they would use something else, right"? So I stipulate that if the runtime becomes faster, those gains won't be visible to large swathes of code because python libraries do lots of weird things under the hood.
- kmod 5y agoYes, definitely. Python is much much more dynamic than Javascript -- the jump in dynamicness of Javascript to Python is about equivalent to Java to Javascript, and similarly requires a different set of techniques. The single example I typically give is that in Python you can inspect stack frames after they've exited! This has concrete implications on your VM design. Another example is that the multiple dispatch protocol for binary operations (such as addition) is sufficiently complicated that you generally cannot encode it into your compiler. We have been able to have a simplified version of that by building a tracing JIT for C, which is able to understand the dispatch protocol, but it is not always applicable. Source: I've been working on Python optimization for several years as part of the Pyston project.
- beagle3 5y agoPython is just too dynamic, and users do make use of it. There are JavaScript constructs that JITs give up upon, such as “with” - and this has steered JS programmers to avoid those constructs. Unfortunately, there is no Python subset people can stick to as such. But you can use e.g. TransCrypt to compile Python to JavaScript; the docs tell you about an efficient subset. It should be possible to build an efficient Python JIT for such a subset (and you sort of have one already through TransCrypt and JS)
- pjmlp 5y agoSmalltalk, remap all instances of a into b across the complete image, no matter where they are being used or which classes they actually are instances of. a become: b It is a myth that only Python enjoys such dynamic capabilities, it happens to be a convenient excuse.
- beagle3 5y agoIt's a lot more than an excuse; The simplest "a.b" has to check a lot of places before it gives an answer, and each of these needs its own in-line cache. Indeed, there have been experimental compilers for Python that gave much improved speeds for subsets that drop the ultra dynamic constructs (and usually a lot more dynamism and features to achieve that speed). And the fact that there are languages as dynamic as Python is not as important as how it is implemented through the language; Lua is about as dynamic as Python, but with effects that are much more local in general. IIRC Mike Pall (of LuaJIT fame) said that Lua was almost optimal for JITting, JS is sort-of-ok if you ignore some things, but Python is beyond hope -- and I tend to think he knows what he's talking about, given that his hobby, one-man-show LuaJIT handily beat all well-funded-with-tens-of-employees JS engines for a very long time (maybe still does). I wonder how good the SmallTalk JITs really are by today's standards, in the presence of e.g. "become:". The fact that's it is there, and supported, doesn't mean it's in common use, or supported efficiently -- just like JavaScript's "with".
- pjmlp 5y agoIn a sense it is, because in those communities the approach has been, "it is hard but we will solve it sooner or later", whereas in the Python world, PyPy seems to be the only approach that has embraced this culture, whereas everyone else takes the path "it is hard, Python cannot be made fast because it is dynamic".
- robert-brown 5y agoYes. At the end of the following Usenet post, the developer of a Python implementation compares the language features of Common Lisp to those of Python in order to explain why Lisp code can be compiled to run much much faster than Python. The same kind of comparison can be done for JavaScript. The problem is a collection of Python features, each of which saps performance. https://markmail.org/message/dp56i26zhpf4fehb#query:+page:1+mid:vxgbk5z7gipau2ah+state:results https://markmail.org/message/dp56i26zhpf4fehb#query:+page:1+...