r/programming • • May 25 '19

Making the obvious code fast

https://jackmott.github.io/programming/2016/07/22/making-obvious-fast.html
1.3k Upvotes

263 comments sorted by

View all comments

282

u/Vega62a May 25 '19 edited May 25 '19

Great post. In particular the Javascript benchmarks were enlightening to me - syntactic sugar can be nice but not at the expense of orders of magnitude of performance. I'm definitely guilty of this myself.

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.

66

u/Kapps May 25 '19

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.

→ More replies (9)

42

u/Vega62a May 25 '19

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.

64

u/threeys May 25 '19

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.

36

u/Vega62a May 25 '19 edited May 25 '19

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.

8

u/auxiliary-character May 26 '19

That would definitely be nice - but I think, as you said, it's definitely a nontrivial task.

It's a nontrivial task that C++ usually pulls off pretty well.

30

u/NonreciprocatingCrow May 26 '19

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

4

u/auxiliary-character May 26 '19

Using a whole lot of information it gets from the type system.

To be fair to C++, there's still quite a lot you can do with type inference and lambdas, and it'll still just do the right thing.

9

u/[deleted] May 26 '19

By taking a lot of time to compile.

4

u/auxiliary-character May 26 '19

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.

3

u/TheAuthenticFake May 26 '19

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.

→ More replies (1)

21

u/carnivorixus May 25 '19

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 :)

17

u/hegbork May 25 '19 edited May 25 '19

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.

5

u/DidiBear May 25 '19

Java (1.8) and Python (3) solve this problem by making map/filter/reduce lazy by default. That's a non sense Js doesn't permit it in some way

5

u/Smallpaul May 26 '19

Laziness is not always faster. Can be slower in many cases.

9

u/atomheartother May 25 '19

Especially since Rust, in this very article, does this in a way that is just as readable and about as fast as C

13

u/Smallpaul May 26 '19

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.

2

u/joonazan May 26 '19

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.

4

u/warlockface May 26 '19

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.

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

36

u/Retsam19 May 25 '19

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 might, nowadays. They ran these benchmarks on an older version of node (v6), while newer versions of node ship with a new V8 engine which is much more performant.

12

u/NoInkling May 26 '19 edited May 26 '19

Yeah I'd like to see this benchmark redone on Node 12 to see what difference there is.

7

u/dksiyc May 26 '19
nodejs
v10.15.3
map reduce x 0.22 ops/sec ±1.71% (5 runs sampled)
reduce x 2.45 ops/sec ±2.02% (11 runs sampled)
foreach x 0.54 ops/sec ±4.73% (6 runs sampled)
imperative x 36.62 ops/sec ±1.22% (64 runs sampled)

nodejs
v11.15.0
map reduce x 0.20 ops/sec ±2.04% (5 runs sampled)
reduce x 2.28 ops/sec ±2.86% (10 runs sampled)
foreach x 0.72 ops/sec ±6.40% (6 runs sampled)
imperative x 37.65 ops/sec ±0.83% (65 runs sampled)

nodejs
v12.3.1
imperative x 39.18 ops/sec ±0.46% (51 runs sampled)
foreach x 1.30 ops/sec ±2.75% (8 runs sampled)
reduce x 2.12 ops/sec ±3.91% (10 runs sampled)
# map reduce either hangs or takes forever

13

u/Smallpaul May 25 '19 edited May 25 '19

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.

→ More replies (3)

6

u/binkarus May 25 '19

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.

→ More replies (4)