对于只有两个可能的代码执行分支的情况,在 c++20 中使用[[likely]]和属性的一般准则是什么?[[unlikely]]是否建议仅将[[likely]]or[[unlikely]]放在执行路径的一个分支上,或者我们应该将其中之一放在一个代码分支上,将另一个放在其余分支上。例如,我们应该做
if (condition) [[likely]]
{
}
else [[unlikely]]
{
}
Run Code Online (Sandbox Code Playgroud)
或者
if (condition) [[likely]]
{
}
else
{
}
Run Code Online (Sandbox Code Playgroud)
当我们知道 if 条件中的分支更有可能被执行时。
在我的objdump -t输出中,我看到以下两行:
00000000000004d2 l F .text.unlikely 00000000000000ec function-signature-goes-here [clone .cold.427]
Run Code Online (Sandbox Code Playgroud)
和
00000000000018e0 g F .text 0000000000000690 function-signature-goes-here
Run Code Online (Sandbox Code Playgroud)
我知道l意味着本地,也g意味着全球。我还知道这.text是目标文件中的一个部分或一种类型的部分,包含已编译的程序指令。但什么是.text.unlikely?假设它是一个不同的部分(或部分类型).text- 有什么区别?
这是一个对任何平台、语言或编译器都开放的天真的一般性问题。虽然我最好奇的是 Aarch64、C++、GCC。
当在依赖于 I/O 状态的程序流中编写不可避免的分支时(编译器无法预测),并且我知道一种状态比另一种状态更有可能,我如何向编译器表明这一点?
这是否更好
if(true == get(gpioVal))
unlikelyFunction();
else
likelyFunction();
Run Code Online (Sandbox Code Playgroud)
比这个?
if(true == get(gpioVal))
likelyFunction(); // performance critical, fill prefetch caches from this branch
else
unlikelyFunction(); // missed prediction not consequential on this branch
Run Code Online (Sandbox Code Playgroud)
如果通信协议使更有可能或临界值为真(高)或假(低),这是否有帮助?
在 Visual Studio 中阅读标准库算法的实现时,他们根本不使用[[likely]]/属性。[[unlikely]]对我来说,应该使用的教科书示例[[unlikely]]是例如std::find_if(...),微软已经这样实现:
// Note that some noise are removed from the code
template <class _InIt, class _Pr>
_InIt find_if(_InIt _First, const _InIt _Last, _Pr _Pred) {
for (; _First != _Last; ++_First) {
if (_Pred(*_First)) {
break;
}
}
return _First;
}
Run Code Online (Sandbox Code Playgroud)
如前所述,find_if(...)这是 if 子句的 true 分支不太可能的教科书示例,因为大多数情况下它会在谓词验证为 true 之前迭代几个元素,因此 [[unlikely]] 将是一个优化机会。Microsoft 没有[[unlikely]]在这里使用该属性有什么原因吗?
c++ algorithm micro-optimization compiler-optimization likely-unlikely
c++ ×3
algorithm ×1
attributes ×1
c++20 ×1
elf ×1
gcc ×1
objdump ×1
object-files ×1
optimization ×1
symbols ×1