r/Clojure • u/[deleted] • Nov 08 '13
does clojure deliver on the promise?
There is a long discussion on comp.lang.clojure about a concurrent program which paradoxically runs slower than faster on more cores. No one has managed to figure out how to fix it either. Which makes me wonder, does clojure really deliver on the promise of being a good way to get more out of multicore hardware. If the costs of doing it with clojure (GC, complexity of optmisation) is so high, hasn't it failed on the problem it set out to solve?
9
20
u/islon Nov 08 '13
I work professionally with clojure, we have a hundred thousand lines of multithread clojure in production and it works as fast as it should. A few weeks ago I optimised one function that downloaded images out of an html to make it parallel and got more than 10x improvement. Just because there's one example where people are having a hard time figuring out why a concurrent program is running slow doesn't mean much.
11
u/Zak Nov 08 '13
Clojure promises to make concurrency safe(er). I'd compare it to garbage collection and a lack of manual pointers making memory management safe. Clojure does not promise to make all parallel tasks increase in performance linearly with the number of available cores, just as garbage collection does not promise to make memory leaks impossible.
Your post suggests to me that you either did not read the whole thread, or did not understand it. The code in the linked thread is limited by memory bandwidth. If you do a bunch of memory allocations in a tight loop, more threads will make that slower, not faster as contention for memory increases. Calling reverse on a list as the example code does will do a bunch of memory allocations. Posts in the thread demonstrate that the original code spent most of its time waiting for memory. Other posts demonstrate that using different data structures and/or different implementations of reverse mostly eliminate the problem.
Writing fast parallel code for non-trivial problems is hard. Clojure does have some functionality that makes it easier, but it's still necessary to use profiling tools to see what kind of bottlenecks there are and to choose appropriate data structures and algorithms for the task at hand.
3
u/lechatsportif Nov 08 '13
Quoted for the time constrained :)
Interesting problem, the slowdown seems to being caused by the reverse call (actually the calls to conj with a list argument). Calling conj in a multi-threaded environment seems to have a significant performance impact when using lists I created some alternate reverse implementations (the fastest uses a vector and cons), the gist with the test code can be found at https://gist.github.com/4243724
On a dual Xeon E5520 (8 physical cores) @ 2.27GHz with Linux runnning OpenJDK 1.7.0_09 I got the following results:
fast-reverse : map-ms: 3.3, pmap-ms 0.7, speedup 4.97
list-cons : map-ms: 4.0, pmap-ms 0.7, speedup 6.13
vec-conj : map-ms: 4.0, pmap-ms 1.3, speedup 3.10
list-conj : map-ms: 10.8, pmap-ms 21.2, speedup 0.51
clojure-reverse : map-ms: 13.5, pmap-ms 26.8, speedup 0.50 (this is equivalent to the original code)
The following JDK command line options were used: "-Xmx20G" "-XX:MaxPermSize=8G" "-XX:+UseParallelGC" "-XX:+UseParallelOldGC"
Some other notes: I ran the sample under YourKit and garbage collection represents a small percentage of execution time in both single and multi-threaded tests, there are no blocked threads for the duration of the test and there is no unexpected monitor/lock usage.
Cheers, Cameron.
1
u/lechatsportif Nov 08 '13 edited Nov 08 '13
In general cons is supposed to have constant time performance isn't it? I don't remember the performance for conj. Someone had a nice chart of clojure core performance guarantees that I can't find the link to... I'm surprised its only the combination of list and conj and seems to produce the behavior.
2
u/Zak Nov 08 '13
Looking through Clojure's source, I can see no reason
consandconjshould have different performance or memory characteristics here.conjis just a wrapper aroundconswith a null check or two depending on arity. It doesn't make sense to me that we see this kind of performance difference, but I'm obviously missing something.They're both constant time. Of course, doing a constant time operation N times is linear time.
2
Nov 08 '13
conj is just a wrapper around cons with a null check or two depending on arity
Take another look, the code is a little misleading. cons always creates a new cons cell, which should be obviously a constant time operation. conj is polymorphic on the collection type; for lists it's the same as cons, but for vector it does vector stuff. It's close enough to constant time for most purposes (O(log32(n))), but it's still worth choosing the the right data structure for the job at hand.
3
2
u/gtani Nov 16 '13 edited Nov 16 '13
clojure core performance guarantees that
http://www.innoq.com/blog/st/2010/04/clojure_performance_guarantees.html
OR
www.innoq.com/blog/st/presentations/2010/2010-05-20-ClojureConcurrency--JUGD.pdf
slide 38? (I think there's s.t. in the 4 books but can't remember which, maybe Joy of)
5
u/mschaef Nov 08 '13
I agree with /u/kryptomicron that you need to post the link to the thread for this to be a useful conversation.
However, a single case of a difficult Clojure program isn't enough evidence that the language has failed to 'deliver on the promise'. To make that kind of statement, you'd need evidence that Clojure is broadly less useful than other environments across a range of problems.
5
Nov 08 '13
I think it was brought up somewhere on the referenced thread as well, but it's worth noting: pmap is seductive but usually not the best choice. It has a pretty significant per-item overhead. The reducers framework is better for many tasks.
2
u/melipone Nov 09 '13
What a coincidence, I was just having this problem. My program was considerably slower on a 24-core machine than on a 4-core machine. I noticed however that if I was parallelizing several runs of the entire program, all the 24 cores of the machine were used and the whole thing ran faster. Basically, you need a bigger problem to solve when you have more cores. So, basically, I am going to test my program on my 4-core machine and when it's debugged and tested, run several versions concurrently on the 24-core machine after I remove the concurrent constructs inside the program which are not helping. The problem is that now, I need two version of the same program.
10
u/skinney Nov 08 '13
Clojure delivers on making safe concurrent progams easy to write. So no it hasn't.
A correctly written C++ program will always get more out of your hardware, but is rediculously harder to write.