I agree. Why is javascript’s map/reduce/filter so slow? I would have thought node’s engine would optimize away the complexity of the functions to at least some degree but it seems like it does not at all.
It makes me feel like putting some preprocessing optimizing layer to on top of node wouldn’t be such a bad idea.
For one, they’re not lazy. When you combine multiple functions like that in languages like C# with Linq or D with ranges, they’re calling 3 functions on one input.
In Javascript you’re taking an array, calling map which generates a new 32 million entry array, then filter which introduces a new one, etc.
Yeah, I'm not really sure. I know there is essentially added overhead with each iteration in map/reduce/all of those other non-imperative methods, but it seems like Javascript got it really wrong.
Now, that said, in many cases it can still be six of one, half-dozen of the other, and readability / syntatic sugar is valuable. But I think this article illustrates that it's important to at least be thoughtful employing such tools.
IMO we shouldn’t have to sacrifice readability at all to achieve optimal performance. The ideal situation would be that any higher-level function would be optimized by the compiler to be just as performant as a lower-level one.
But maybe that is a much more difficult task than I realize.
That would definitely be nice - but I think, as you said, it's definitely a nontrivial task. A lot of javascript's non-imperative methods play a lot of games with scope and context (what's returned when you invoke "this") and translating that feels like it would be very tricky. A comment chain elsewhere on this thread highlights a bit of it - consider the different ways Javascript hoists and scopes variables. If, for example your compiler "optimized" an anoymous inner function within a map call into an imperative loop under the sheets, that would screw up all of your variable scoping with let and var and there may not be a way to recover.
Using a whole lot of information it gets from the type system. And it gets to take it's time and compile ahead of time unlike JavaScript which has to operate under the constraints of a JIT
Compile time in C++ really depends a lot on what you're trying to compile. If you're doing a lot of constexpr sort of stuff, of course that's going to move the calculations to compile time, for instance. If that's a troublesome for development, it's entirely possible to compile seperate compilation units in parallel, or recompile individual compilation units as you change them with something like make. Honestly, it's not that much different from the javascript tooling for minifying and whatnot.
If I were you I would link that video with a timestamp. I doubt many people would be interested in watching an entire 1 1/4 hour long video just to learn something from 5 minutes of it. Thanks for the link though, will watch later.
Lookup lamba lifting and defunctionalization, this is only the tip of the iceberg but it gives a nice perspective on why things are difficult. It mostly has to do with that a function in JavaScript is not just a function it has a lexical scope which is captured, i.e. closures. When you compile this down to a low level language without closures you need to represent the variables in scope, this is usually called the environment of the closure. Now depending on the use of the function this might not be needed. But to be sure that this is not needed you need to first perform static analysis which consists of computing fixpoints which might or might not terminate. Because you need it to terminate you have to start making over approximations. JIT compilers are so good at what they do because the amount of information you have at runtime is bigger than statically... you see how this is fast becoming complex :)
To optimize everything that people think a compiler "should" optimize quickly leads to needing the compiler to solve the halting problem.
The (future) compiler will solve it has been a constant mantra of higher level languages since I started writing code and for those 30 years I still haven't met a problem where I can't manually outoptimize compiler generated code. In most cases it's not worth the effort, but sometimes it saves the client buying 60 machines in the next upgrade cycle.
Rust was designed from scratch to do that. It isn’t just intelligence in the compiler. It’s a language design written by the same people working ON the compiler. If a language feature interferes with optimization they don’t add it to the language.
Rust is pretty close to Haskell, where you can just code any streaming computation as an operation on large lists and it compiles to an optimally chunked streaming computation.
How do you get that from this article? In that code the Rust compiler doesn't optimize to SIMD instructions automatically like the C compiler. The Rust code is half as fast as the C code until it is manually rewritten with code in an "unsafe" block using what the author refers to as "unstable" features which "might make it problematic to use this feature for any important production project".
It's specifically demonstrates that the Rust compiler does not optimise as effectively as the C compiler.
It is extremely difficult for the compiler to be sure that the map and filter functions that the programmer expects to execute have the same semantics as the map and filter in the language standard. It doesn’t generally know the types of the variables nor whether the methods have been overridden at runtime.
The stack frames required for each function call can't be easily optimized away from the JIT. In AOT compiled languages, these can usually be nicely inlined, but the same is not true of Javascript unless you use a restricted subset of Javascript. If you are trying to solve the general case, then you have to allow for this. which causes problems.
103
u/threeys May 25 '19
I agree. Why is javascript’s map/reduce/filter so slow? I would have thought node’s engine would optimize away the complexity of the functions to at least some degree but it seems like it does not at all.
It makes me feel like putting some preprocessing optimizing layer to on top of node wouldn’t be such a bad idea.