奇怪的优化?在“ libuv”中.请解释
Strange optimization? in `libuv`. Please explain
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?
是的,就像评论所说的那样。如果标志已经为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
如您所见,没有有条件的移动,而是分支。而且我敢肯定,分支错误预测远比弄脏缓存线更糟糕。
相关文章:
- 请解释"函数1(p1,p2,p3);"的输出
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 被解释为低级别const的const对象的地址
- 计算每个节点的树高,帮助我解释这个代码解决方案
- MSVC将仅移动结构参数解释为指针
- 内联程序集printf将整数解释为地址
- 有人能解释一下为什么下界是这样工作的吗C++的
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 有人能为我解释一下C++代码吗
- 你能解释一下什么运行时错误是如何解决它的吗?
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 有人可以向我解释为什么控制台输出 0 吗?
- 有人可以解释一下这段代码如何能够反转字符串
- 在 C++ 中解释多维向量的语句时感到困惑
- 解释一下 for (char c : str) 的作用?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 隐式重新解释引用时强制转换,没有警告/错误
- 有人可以在 c++ 中解释数组的这种输入
- 奇怪的优化?在“ libuv”中.请解释