Pretend This Optimization Doesn’t Exist
Programming in the 21st Century - James Hague - January 31, 2012In any modern discussion of algorithms, there’s mention of being cache-friendly, of organizing data in a way that’s a good match for the memory architectures of CPUs. There’s an inevitable attempt at making the concepts concrete with a benchmark manipulating huge—1000x1000—matrices. When rows are organized sequentially in memory, no worries, but switch to column-major order, and there’s a very real slowdown. This is used to drive home the impressive gains to be had if you keep cache-friendliness in mind.
Now forget all about that and get on with your projects.
It’s difficult to design code for non-trivial problems. Beautiful code quickly falls apart, and it takes effort to keep things both organized and correct. Now add in another constraint: that the solution needs to access memory in linear patterns and avoid chasing pointers to parts unknown.
You’ll go mad trying to write code that way. It’s like writing a short story without using the letter “t.”
If you fixate on the inner workings of caches, fundamental and useful techniques suddenly turn horrible. Reading a single global byte loads an entire cache line. Think objects are better? Querying a byte-sized field is just as bad. Spreading the state of a program across objects scattered throughout memory is guaranteed to set off alarms when you run a hardware-level performance analyzer.
Linked lists are a worst case, potentially jumping to a new cache line for each element. That’s damning evidence against languages like Haskell, Clojure, and Erlang. Yet some naive developers insist on using Haskell, Clojure, and Erlang, and they cavalierly disregard the warnings of the hardware engineers and use lists as their primary data structure….
...and they manage to write code where performance is not an issue.
(If you liked this, you might enjoy Impressed by Slow Code.)
Categories: Blogs Programming in the 21st Century
Erlang on Twitter
» ericmoritz (Eric Moritz): RT @heinz_gies: Experimenting with #CRDT’s in #Erlang: https://t.co/OyCcH0dbG2
» heinz_gies (Heinz N. Gies): #Erlang #protio every now and then take a break form big systems and write a simple algorithm, it helps to remember how cool #Erlang is!
» silentbicycle (Scott Vokes): RT @NotDoctorOk: “if” is indeed a special case of “case”, and “case” is basically an in-lined function call. http://t.co/A43LNxboj3
» hiddayngokngok (Wahyu Hidayatullah): Demi eewa erlang, semoga Munchen menang (˘ʃƪ˘)
» mactsouk (Mihalis Tsoukalos): Writing about Erlang—pretty nice programming language. @ErlangInfo @joeerl
» debasishg (Debasish Ghosh): RT @heinz_gies: Experimenting with #CRDT’s in #Erlang: https://t.co/OyCcH0dbG2
» ErlangInfo (Erlang!): RT @heinz_gies: Experimenting with #CRDT’s in #Erlang: https://t.co/OyCcH0dbG2
» heinz_gies (Heinz N. Gies): Experimenting with #CRDT’s in #Erlang: https://t.co/OyCcH0dbG2
» sasajuric (Saša Jurić): RT @al_maisan: very cool lecture -> “.. even though it’s 25 years old is 15 years ahead of everything else..”, https://t.co/kkv2aWEiY8 #erl…
» MirkoBonadei (Mirko Bonadei): RT @simonxk: Erlang is an operating system on top of operating system…
Statistics
Number of aggregated posts: 10651
Most recent article: May 21, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…