<p>Right now, the Go compiler inserts the following prologue into every function to check if the Go routine ran out of stack:</p>
<pre><code>somefunction:
mov %fs:-8,%rcx # fetch TLS pointer
lea -amount(%rsp),%rax # optionally compute stack frame size
cmp 0x10(%rcx),%rax # do we need more stack?
jbe .Lmorestack
...
# actual code here
...
.Lmorestack:
call runtime.morestack_noctxt # get more stack
jmp somefunction # try again
</code></pre>
<p>This prologue has a size of 34 bytes (less if no stack space is needed). That's a lot of space wasted for each function.</p>
<p>I have an idea how to reduce the size of this boilerplate code: First, instead of putting the stack limit into the structure pointed to by <code>%fs:-8</code>, it could be placed directly in the TLS segment. Then we can check if we need more stack like this:</p>
<pre><code>somefunction:
lea -amount(%rsp),%rax # compute stack frame size
cmp %fs:stacklimit,%rax
</code></pre>
<p>This saves four bytes and one load, making each function call a tiny bit faster in the process. It's not a huge size improvement, I estimate binaries would shrink by about 0.5% with this change.</p>
<p>Really though, if the linker would shuffle around code bits so jump targets (especially that frame code) are near the jump instructions, that would save another 7 bytes in the boilerplate code.</p>
<hr/>**评论:**<br/><br/>Ainar-G: <pre><p>You should probably repost this to <a href="https://groups.google.com/forum/#!forum/golang-nuts">golang-nuts</a> if you want to get feedback from the Go team.</p></pre>FUZxxl: <pre><p>Thank you. Will do.</p></pre>Faffenheim: <pre><p>Please post to golang-dev, nuts is not meant for compiler and runtime discussions</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传