在完全受CPU限制的工作负载中,有必要限制goroutine的数量吗?

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

Is it nescessary to limit the number of go routines in an entirely cpu-bound workload?

问题

如果是的,如何确定最大值?这对我来说是最重要的部分。我真的很想手动设置它。我考虑使用runtime.GOMAXPROCS(0),因为我怀疑更多的并行性不会带来额外的好处。评论似乎暗示它在某个时候会被弃用。

据我所了解,当涉及到Go协程时,唯一的限制因素是内存,因为一个休眠的Go协程仍然需要内存来存储其堆栈。

英文:

If yes, how does one determine that maximum? That is the most important part to me. I'd really like to have it be set manually. I considered using runtime.GOMAXPROCS(0), as i doubt that more parallelism will yield any additional benefits. The comment seems to suggest, that it is marked for deprecation at some point.

From what I gather, the only limiting factor when it comes to go routines is memory, as a sleeping go routine still requires memory for its stack.

答案1

得分: 3

这不是严格必要的。这些 goroutine 的运行线程数量默认等于机器上的 CPU 核心数量(可通过 GOMAXPROCS 进行配置),因此在线程级别上不会有争用。

然而,通过减少准备运行的 goroutine 数量,你可能会获得性能上的好处,因为会有内存缓存效应。例如,在一台有 8 个核心的机器上,如果你有 1000 个活跃的 goroutine,它们都会访问大量的内存,那么当一个 goroutine 再次运行时,所需的内存页面可能已经从 CPU 缓存中被驱逐出去了。减少 goroutine 的数量可以提高缓存命中的几率。

对于性能问题,始终要进行自己的测量,使用代表性的工作负载才能确切得出结论。

英文:

It's not strictly necessary. The number of threads running these goroutines is by default equal to the number of CPU cores on the machine (configurable through GOMAXPROCS), so there will be no contention at the thread level.

However, you might get performance benefits from having fewer goroutines ready to run, because of memory caching effects. For example, on an 8-core machine, if you have 1000 active goroutines that all touch significant amounts of memory, by the time a goroutine gets to run again, the needed memory pages have probably already been evicted from your CPU caches. With fewer goroutines, the odds of a cache hit are better.

As always with performance questions: the only way to be sure is to measure it yourself with a representative workload.

答案2

得分: 2

在我们的测试中,我们确定最好是生成一定数量的工作例程,并使用它们来执行所有的工作。创建和销毁 goroutine 是轻量级的,但并非完全没有开销。如果 goroutine 在任何时间段都处于阻塞状态,那么这种开销通常是微不足道的。

英文:

In our testing, we determined that it is best to spawn a fixed number of worker routines and use those to perform all the work. The creation and destruction of goroutines is lightweight, but not entirely free of overhead. That overhead is usually insignificant if the goroutines spend any amount of time blocked.

答案3

得分: 0

goroutines非常轻量级,所以完全取决于您所运行的系统。一个平均进程在4GB内存中应该没有问题,可以同时运行不到一百万个并发例程。当然,要确定这是否适用于您的目标平台,我们需要知道该平台是什么。

请参阅这篇文章这篇文章,它们非常有用。

英文:

goroutines are very lightweight so it depends entirely on the system you are running on. An average process should have no problems with less than a million concurrent routines in 4GB Ram. Whether this goes for your target platform is, of course, something we can't answer without knowing what that platform is.

see this article and this, they are usefull

huangapple
  • 本文由 发表于 2021年9月1日 15:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/69009822.html
匿名

发表评论

匿名网友

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

确定