149 points · by jervisfm · 12y ago · 52 comments
This is where I feel functional languages will have a huge advantage in coming years. As side effects are reified and put in the compiler's control, they can be modified and optimized automatically.
While TFA is interesting, and a pretty good overview of designing programs with the cache in mind, it just shows me how far we have to go in language design. I don't want to manually make all of these modifications, let alone carefully profile each permutation of all the possible choices. We have fast, dumb computers, let them do that work.
To counter this, people invent things like the asm.js specification, which tells you exactly what you need to do to make JavaScript fast, and then we're programming in assembly again. (Or perhaps C++.)
So there is still plenty of room for languages that aim for predictable performance. I believe this is where Go and Rust are headed.
Some faint glimpses of light can be seen, like compressed references in HotSpot or this Halide.
Rust and Go have nothing planned in this area.
Is that so bad? The reason we're in JS in the first place in those environments isn't because it's the best language for the job, it's because it's the only language that can be used in that environment.
If you can have the best of both worlds, why not?
Functional compilers that outperform hand tuned C++ on many core machines may be possible, but very hard. Just because everyone has previously underestimated the difficulty does not mean it's an idea worthy of derision.
I think it's much more likely that it's perpetually ten years away, until one day it's not (like the current, possibly temporary end to Moore's Law).
Watching Simon Peyton Jones' talk on data parallel Haskell, I'd not make a bet against high performance Haskell's eventual success, especially as core counts continue to climb, and NUMA gets more NU.
The best way to understand it is probably to watch the video, which is relatively long but explains things clearly: https://www.youtube.com/watch?v=3uiEyEKji0M
Some of my findings:
- Effective bandwidth is approximately 6x greater for data that fits in L1, 4.3x greater if it fits in L2, and 2.9x greater if it fits in L3.
- Contention for shared L3 cache can limit the speedup you get from parallelization. For instance, running two threads for a data set that fits in L3 results in a speedup of only 1.75x, rather than 2x. Four threads on one four-core processor results in a speedup of only 2x vs the single-threaded program.
- It takes relatively few operations for programs to become compute-bound rather than memory bound. If 8 or more "add" operations were performed per data access, we found that the effects of caching disappeared almost completely, and the program's execution was limited by processor rather than the memory bottleneck.
The specific magnitude of these results is machine-dependent, but I would expect the general relationships to hold for other machines with a similar cache hierarchy.
[1] http://www.stoneridgetechnology.com/uploads/file/ComputevsMe...
I think they'd differ from the author here that this isn't an optimization to go to when performance starts to suffer, but a design that you have to start using from the beginning, because it's a big change to refactor everything to this style.
Then we did some OpenMP parallelization. That was cool.
Nice post!
"How to get 100K TPS with 1ms latency": http://www.infoq.com/presentations/LMAX
http://www.amazon.com/Code-Optimization-Effective-Memory-Usa...
I hope universities start to catch up and build this, along with distributed systems computing and networking, into their core curriculum.
Mix multi-threaded programming with cache effects and there is a whole new world of side effects and un-expected results. There is a side-note in the article about it, but I think that deserve a whole article on it too!
It is interesting that this kind of plays a bit in the favor of functional languages. They are often data centric more than code centric. As in your lay out your data then pass it to your functions as opposed to thinking about the layout of the functionality as different objects that just happen to encapsulate data (so this separates and breaks down the data).
I got curious about how lscpu was determining L1 Data and L1 Instruction caches. So I had a look at the source code of lscpu and found that it was looking in /sys/devices/system/cpu/cpu/cache/ and /sys/devices/system/cpu/cpu/topology/
This lead me to "Memory part 4: NUMA support" http://lwn.net/Articles/254445/
Pretty interesting stuff.
I'm slightly in the dark as to why loop optimizations are not part of the default compile process.
Also, if you really want performance, go parallel. Cilk is amazing and there are forks of both gcc and clang with cilk. Cilk is seriously awesome.
In case you missed it, there was a relevant article last week about how B-Trees benefit by reducing the number of cache requests.
[1]. http://db.lcs.mit.edu/projects/cstore/abadisigmod06.pdf
The object was a struct with a size of a typical game object( 100B ), then I trashed the memory by doing a lot of malloc/free using the size of the object. The two tests were one with an array of objects( contiguous array ) and the other an array of pointers to objects which were allocated separately. Then I iterated over the object doing some very simple operation( just enough to access it, and identical for each object ) and timed this. And of course I trashed the memory some more for each time.
The time taken ratio is:
array of objects : array of pointers to objects
1 : 1.189
The second test is identical except I made some extra effort to make sure that every object in array of pointers was not adjacent to any previous one in memory:
array of objects : array of pointers to objects
1 : 2.483
The difference got much smaller once the operation on each object became more time consuming.
If someone knows of an easier to digest explanation than the proof-laden academic articles I'd appreciate a pointer :-)
http://gameprogrammingpatterns.com/images/data-locality-char...