TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Goroutines Under the Hood (2020)

154 点作者 s3micolon0将近 3 年前

9 条评论

captainmuon将近 3 年前
One thing that really goes against my intuition is that user space threads (lightweight treads, goroutines) are faster than kernel threads. Without knowing too much assembly, I would assume any modern processor would make a context switch a one instruction affair. Interrupt -&gt; small scheduler code picks the thread to run -&gt; LOAD THREAD instruction and the processor swaps in all the registers and the instruction pointer.<p>You probably can&#x27;t beat that in user space, especially if you want to preempt threads yourself. You&#x27;d have to check after every step, or profile your own process or something like that. And indeed, Go&#x27;s scheduler is cooperative.<p>But then, why can&#x27;t you get the performance of Goroutines with OS threads? Is it just because of legacy issues? Or does it only work with cooperative threading, which requires language support?<p>One thing I&#x27;m missing from that article is how the cooperativeness is implemented. I think in Go (and in Java&#x27;s Project Loom), you have &quot;normal code&quot;, but then deep down in network and IO functions, you have magic &quot;yield&quot; instructions. So all the layers above can pretend they are running on regular threads, and you avoid the &quot;colored function problem&quot;, but you get runtime behavior similar to coroutines. Which only works if really every blocking IO is modified to include yielding behavior. If you call a blocking OS function, I assume something bad will happen.
评论 #31633546 未加载
评论 #31633769 未加载
评论 #31633754 未加载
评论 #31633819 未加载
评论 #31633463 未加载
评论 #31633314 未加载
评论 #31635500 未加载
评论 #31633567 未加载
评论 #31635852 未加载
评论 #31635518 未加载
geodel将近 3 年前
I mean it has many diagrams and logical explanation of Goroutines and concurrency concepts in general but it is definitely not under the hood descriptions.
评论 #31633374 未加载
jeffbee将近 3 年前
It didn&#x27;t really lift the hood at all, unfortunately. Luckily for us the runtime is extensively commented, e.g. <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;blob&#x2F;master&#x2F;src&#x2F;runtime&#x2F;proc.go" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;blob&#x2F;master&#x2F;src&#x2F;runtime&#x2F;proc.go</a>
aaronbwebber将近 3 年前
I love Go and goroutines, but...<p>&gt; A newly minted goroutine is given a few kilobytes<p>a line later<p>&gt; It is practical to create hundreds of thousands of goroutines in the same address space<p>So it&#x27;s not practical to create 100s of Ks of goroutines - it&#x27;s possible, sure, but because you incur GBs of memory overhead if you are actually creating that many goroutines means that for any practical problem you are going to want to stick to a few thousand goroutines. I can almost guarantee you that you have something better to do with those GBs of memory than store goroutine stacks.<p>Asking the scheduler to handle scheduling 100s of Ks of goroutines is also not a great idea in my experience either.
评论 #31633364 未加载
评论 #31633341 未加载
评论 #31634351 未加载
评论 #31633681 未加载
评论 #31633497 未加载
rounakdatta将近 3 年前
While the blog is a great introductory post, <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=KBZlN0izeiY" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=KBZlN0izeiY</a> is a great watch if you&#x27;re interested in the magical optimizations in goroutine scheduling.
qwertox将近 3 年前
I&#x27;ve fallen in love with Python&#x27;s asyncio for some time now, but I know that go has coroutines integrated as a first class citizen.<p>This article (which I have not read but just skimmed) made me search for a simple example, and I landed at &quot;A Tour of Go - Goroutines&quot;[0]<p>That is one of the cleanest examples I&#x27;ve ever seen on this topic, and it shows just how well integrated they are in the language.<p>[0] <a href="https:&#x2F;&#x2F;go.dev&#x2F;tour&#x2F;concurrency&#x2F;1" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;tour&#x2F;concurrency&#x2F;1</a>
评论 #31634314 未加载
bogomipz将近 3 年前
In the conclusion the author states:<p>&gt;&quot;Go run-time scheduler multiplexes goroutines onto threads and when a thread blocks, the run-time moves the blocked goroutines to another runnable kernel thread to achieve the highest efficiency possible.&quot;<p>Why would the Go run-time move the blocked goroutines to another runnable kernel thread? If it is currently blocked it won&#x27;t be schedulable regardless no?
评论 #31635657 未加载
评论 #31636287 未加载
guenthert将近 3 年前
So it&#x27;s just coroutines on top of n:m scheduling, similar to what SysV offered a while ago?
评论 #31636175 未加载
yellow_lead将近 3 年前
(2020) But so high level it&#x27;s still relevant.