<分区>
libuv
包含 core.c:uv_run()
中的下一个代码
/* The if statement lets the compiler compile it to a conditional store.
* Avoids dirtying a cache line.
*/
if (loop->stop_flag != 0)
loop->stop_flag = 0;
这是什么意思?这是某种优化吗?为什么他们不简单地分配 0?
<分区>
libuv
包含 core.c:uv_run()
中的下一个代码
/* The if statement lets the compiler compile it to a conditional store.
* Avoids dirtying a cache line.
*/
if (loop->stop_flag != 0)
loop->stop_flag = 0;
这是什么意思?这是某种优化吗?为什么他们不简单地分配 0?
最佳答案
我认为这种优化很糟糕。例如,在带有 -O3 的 gcc 上,它给出以下代码:
foo():
movl stop_flag(%rip), %eax
testl %eax, %eax
je .L3
movl $0, stop_flag(%rip)
.L3:
ret
stop_flag:
.zero 4
如您所见,没有条件移动,只有分支。而且我敢肯定,分支预测错误比弄脏缓存行要糟糕得多。
关于c++ - 奇怪的优化?在 `libuv` 中。请解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067315/