Hacker News new | ask | show | jobs
by ryanplant-au 3154 days ago
Does JavaScript make it easier to reason about those potentially-optimizable areas? Which language features make it easier to optimize to the level that V8 is? (V8 being 7-10x faster than CPython 3 on most of the Benchmarks Game.)
4 comments

As far as I understand, one important difference is that JavaScript doesn't have __getattr__ or __setattr__ (or at least earlier versions didn't).

You might not use those hooks a lot in your application code, but it seems that web frameworks and the like do a lot of reflection, which makes the code difficult/impossible to optimize (even at runtime with a JIT).

Python also has __add__ (operator overloading) and JavaScript doesn't. This is a good talk about how subtle or crufty the semantics of something like "a+b" is in Python:

https://www.youtube.com/watch?v=IeSu_odkI5I

The PyPy developers had to copy a lot of the implementation details of CPython, which doesn't always result in the fastest code.

You should take a look at the Chambers and Ungar papers on the implementation of Self. In Self, _everything_ is theoretically done through message send/dispatch. This includes field access, numerical operations, flow control... the works. (In a language with prototypical inheritance too.)

What's interesting is that by the time they're done with their optimizations, they manage to get relatively close (x2, iirc) to native speed by essentially inlining everything they can and keeping enough metadata around that they can retain the dynamic properties of the languauge. Impressive stuff. (Which is probably why Sun hired them early in the Java/JIT days.)

I feel like that's more a case of throwing resources at the problem until it improves than the language making life easier for optimizers.
It's at least partially a factor of the cython implementation, because pypy does a pretty great job doing JIT compilation, and making python fast. That's without the resources of Google behind it, too. (Though I've seen some python compiler "20%" style projects roll around: https://github.com/google/grumpy).

That said, python does have a lot of magic functionality that makes predicting paths through the program difficult for a compiler

That’s just telling you that a JIT is faster than an interpreter. You’d want to compare PyPy to V8 to see how well they can both be JITed.
Only after someone spends as much money on PyPy as has been spent on V8.
That’s true but a secondary concern since most of these benchmarks are for toy programs where all but the most primitive JIT will help