Please review code and make it faster if possible

I decided to do a Go translation of my multi-threaded twins primes sieve algorithm I’ve done in various languages. This is my first serious foray into programming Go, and it actually didn’t feel so bad. :slight_smile:
I got some help here on a few code snippets and issues, and now its completely done.
Therefore, I hope some people will review|run the code and show how to make it faster.

Here’s the link to the gist file of the code.

For those interested in the math and details of the algorithm please read this paper.

The Use of Prime Generators to Implement Fast Twin Primes Sieve of Zakiya (SoZ)…

Go threads will switch when they do I/O or some other long running action. If there are none, then other Go threads will starve. I took a quick glance at twins_sieve() which is the only function called by the Go threads. I didn’t notice any I/O; looked like all math (I didn’t examine it closely, so I could be wrong).

I once wrote a multi-threaded program and asked for help too. Someone pointed this out and recommended I re-write it without threads and compare. In my case, it was significantly faster without threads. The Go runtime could not spread the work out in any reasonable way among the threads.

Multi-threading isn’t always faster…

No longer true.

Go 1.14 Release Notes (February 2020)

Goroutines are now asynchronously preemptible. As a result, loops without function calls no longer potentially deadlock the scheduler or significantly delay garbage collection. This is supported on all platforms except windows/arm , darwin/arm , js/wasm , and plan9/* .

In this case, we already know it’s faster. With four physical and eight logical cores, around five times (4 * 1 + 4 * 0,25 = 5) faster: