cold
The cold attribute on functions is used to inform the compiler that the
function is unlikely to be executed. The function is optimized for size
rather than speed and on many targets it is placed into a special subsection
of the text section so all cold functions appear close together,improving
code locality of non-cold parts of program. The paths leading to calls of
cold functions within code are marked as unlikely by the branch prediction
mechanism. It is thus useful to mark functions used to handle unlikely
conditions, such as perror, as cold to improve optimization of hot functions
that do call marked functions in rare occasions.
函数前面使用这个扩展,表示该函数比较冷门,这样在分支预测机制里就不会对该函数进行预取,或说是将它和其他同样冷门(cold)的函数放到一块,这样它就很可能不会被放到缓存中来,而让更热门的指令放到缓存中。
hot
The hot attribute on a function is used to inform the compiler that the
function is a hot spot of the compiled program. The function is optimized
more aggressively and on many targets it is placed into a special subsection of
the text section so all hot functions appear close together, improving locality.
函数前面使用这个扩展,表示该函数会被经常调用到,在编译链接时要对其优化,或说是将它和其他同样热(hot)的函数放到一块,这样有利于缓存的存取。