Fork me on GitHub

I have a question about using core-async. It works slower when you use parallel go blocks to fetch data from a channel than using one. I created a simple example here . Can you tell me what I’m doing wrong?


@darnok this is mostly a benchmark of the overhead placing and removing items from a channel. To put this in perspective, the generation of a int, is basically free (the go doing the puts), and summing a integer is a few clock cycles. While performing a channel op involves two locks and unlocks. Those locks can be as high as 300 cycles each.


So the issue I think you're seeing is that the producer of the values can't keep up with the consumers. Since the consumers are really doing nothing but contending for produced values, the overhead of 4 threads locking each other probably is giving you worse performance


One option is to make the producer parallel as well. Another option would be to produce integers of batches of 1000 or so so that the sum processes have more work to do.


also, for is lazy, so parallel-sum is not really parallel


(never mind, merge calls vec)


I tried to generalize the example from . I had similar lower performance e.g. with counting lines.


Yeah, that article does so many things wrong I don't even know where to start.


@darnok a few points where it goes wrong: 1) They never actually use non-blocking IO. They're wrapping a blocking data source (line-seq) in a go block, which causes a ton of issues 2) Once again the working being done here is so trivial the overhead of using a channel and splitting the file into lines is going to dominate. 3) The aggregation tap is going to run a lot slower than the counting tap. So we're running into the problem every queuing system runs into: a system can only be as fast as its slowest part, and communication overhead makes it worse.


@tbaldridge Thanks for response 🙂. Yeah, I’ve done the same exercise that the author did, with just regular loop etc. and it was 3 times faster. BTW: Why is wrapping up a blocking IO in go block a problem? Will it be a bad idea to run a JDBC query inside go block and iterate through ResultSet and push to the channel?


Yes, because go blocks run on a limited thread pool that defaults to 8 threads. Block to many of them and the program will deadlock


using (async/thread ...) instead of (async/go ...) is the easiest fix there