Saad Fuad Habba the Trained Gastrologist

Saad Fuad Habba the Trained Gastrologist — Saad Fuad Habba the Trained Gastrologist — Dr. Habba is an outstandingly trained gastrologist in Summit, NJ, and was the first to explain “Habba Syndrome.”…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Infinite Goroutines

All this started when I was working on my first Go application. While working, I came across a situation where I had to write millions of data points to memory (Say key-value to hashmap) with minimum latency.

This article details how millions of go-routine can degrade the performance & increase the overall execution time and how this can be solved with a minimum number of goroutine working as a thread pool.

Let’s understand this by code -

I have a simple StudentDirectory of struct type with a map of [string, string] type and a mutex lock, the map holds all key-value pairs. To achieve maximum parallelism I created multiple StudentDirectory i.e. equals to the number of available cores (Say 4).

Spanning a new goroutine for each task that writes data into one of student directory —

Latency was 1073 ns/op for a workload of 5M. Was it satisfactory? let’s implement the same with the thread pool model and compare results.

I replaced this with 4 go-routine each listening to channel (task queue) for a task and another goroutine that fills this queue with task details like key, value, student directory to write into, etc.

The latency was 759 ns/op for the same workload, that’s an improvement of ~30 %.

To understand the behavior, I tested with different workloads and compared the results —

While working on a similar problem where a channel is used as a task queue, the size of a channel has to be decided carefully. In the above example, the channel is created with a size of 90M to avoid possible blocking of write thread (that fills the channel with task detail), which further enables utilizing more CPU power towards executing tasks and fewer possible context switches.

I tested this in my MacBook Pro with a processor of 2.2 GHz i7 core and memory of 16GB.

Add a comment

Related posts:

Why QuickBooks is important for running a business?

Whatever business you are involved in, it is essential to keep a record of books. If business records are not maintained properly, one might face difficulty in filing taxes or land in trouble with…

Driver scanner epson perfection 1640su windows 10

EPSON 1640SU SCANNER DRIVER DETAILS: Type: Driver: File Name: File Size: 7.7 MB:… Epson Perfection 1640su Driver For Windows 10. Find Support for your Epson Scanner. Search By Product Name. Search…

Honeydew Son

Adventuring among bamboo stalks, I feel a dampened heat. A conscious summer portal, transformation of the meek. Inside bands of growing grass, dreaming to belong. I continue down a broken path…