将数据写入Go语言通道的成本是多少?

huangapple go评论106阅读模式
英文:

Cost of writing data to a go lang channel?

问题

我有一个包含[]stringlistcontainers/list)。我经常通过通道发送它。我试图了解这种通信的开销有多大。一般情况下,在发送时,被发送的数据的浅拷贝会被复制到缓冲区,然后在接收时再次复制。所以发送和接收的开销不会比浅拷贝更大。一般情况下,是否存在一些需要注意的地方?

英文:

I have a list (containers/list) containing a []string. I am sending this over a channel a lot. I am trying understand how expensive this communication is. Is the general picture that on a send a shallow copy of the data being sent is copied to the buffer and then recopied on the other side on receive? So sending and receiving is no more expensive than shallow copying? Are there some gotchas in general?

答案1

得分: 6

该值被复制到通道中,并从通道中复制出来。如果你发送的是一个容器/列表,那么会复制一个具有两个字段的结构体。列表元素不会被复制。

这是一种浅拷贝。

需要注意的是,应用程序必须确保只有一个goroutine修改列表元素。

英文:

The value is copied to and from the channel. If you are sending a container/list, then the a struct with two fields is copied. The list elements are not copied.

It's a shallow copy.

The gotcha is that the application must ensure that only one goroutine modifies the list elements.

huangapple
  • 本文由 发表于 2017年1月27日 06:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/41884015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定