Warm tip: This article is reproduced from serverfault.com, please click

clojure reduce function uses all cores even though it seems to be single threaded

发布于 2020-11-27 16:20:30
(defn sum [numbers]
  (reduce + numbers))

(def numbers (into [] (range 0 100000000)))

(time (sum numbers))

The above was the code that was ran.

Simply adding up a lot of numbers.

This line was executed in the repl multiple times:
(time (sum numbers)) Each time it almost gets all cores fully running.

enter image description here

Looking at jvisualvm, there were not a lot of threads created.

But this code used all 12 of the hyperthreads that were available on my 6 core laptop.

What was happening behind the scene that made this possible?

Questioner
Cui Pengfei 崔鹏飞
Viewed
0
Cui Pengfei 崔鹏飞 2020-11-28 13:42:12

Thanks to the comments.

It has to do with the size of the range.

On my laptop, when it's around 70 million numbers, all is fine.

When it gets around 80 millions, the heap size grows a lot, the time taken grows very significantly, and all cores get to work. And visual vm shows more GC activity was happening.

So the comments above are probably right, it has to do with GC.