Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using channels in Go good for performance?

Tags:

go

I'm wondering as to whether channels are efficient enough to be used as part of coding a Go program.

like image 803
WoooHaaaa Avatar asked Sep 24 '12 03:09

WoooHaaaa


People also ask

Are channels blocking in Go?

Channel operation (i.e. write or read) are blocking in nature. This means: When we send data into the channel using a GoRoutine, it will be blocked until the data is consumed by another GoRoutine. When we receive data from channel using a GoRoutine, it will be blocked until the data is available in the channel.

What are Goroutines good for?

Goroutines are useful when you want to do multiple things simultaneously. For example, if you have ten things you want to do at the same time, you can do each one on a separate goroutine, and wait for all of them to finish.

What are buffered channels Go?

Channels can be defined as a pipes using which Goroutines communicate. Similar to water flows from one end to another in a pipe, data can be sent from one end and received from the another end using channels.

Why are there buffer channels in Golang?

The blocking nature of channels can be inefficient when you want to send data values without having to wait for the other end to respond. Golang provides buffered channels, which allow you to specify a fixed length of buffer capacity so one can send that number of data values at once.


2 Answers

Channels in Go are not a function, they are a primitive type, a so called first-class citizen of the language.

In contrast to semaphores (aka. mutexes), channels are highly recommended since, when used correctly, they can serialize concurrent access very efficiently.

Go will certainly outperform any interpreted dynamic language and deals with concurrency way better than a lot of compiled languages. There are still a couple of use-cases where Go is not adequate, like in rocket controllers and the like but for normal real-world applications, Go is certainly one of the fastest and most flexible languages around.

like image 126
thwd Avatar answered Nov 10 '22 06:11

thwd


Generally speaking, yes, channels are fast but we can't tell if you should use it without knowing your program. I'd say this part isn't constructive.

As for the implementation, it's available when you install Go with the sources. Look for exemple at src/pkg/runtime/chan.c.

Most Go concurrent programs rely on channels. If you want to code in Go and execute concurrent tasks, I'd say you have almost no choice : use channels, profile, and see if you have a problem related to channels.

like image 43
Denys Séguret Avatar answered Nov 10 '22 07:11

Denys Séguret