我可以部分/选择性地内联函数吗?

Wil*_*ill 6 c inline c99

void run_hot(void) {
    // I am called very often!
    serve();
    // <more code here>
}

void run_cold(void) {
    // I am called only occasionally!
    serve();
    // <more code here>
}

???inline??? void serve(void) {
    // I only want to be called inline from hot functions!
    // <more code here>
}
Run Code Online (Sandbox Code Playgroud)

有没有什么办法,明确内联函数一个函数中,同时明确在函数内联相同功能的Ç?还是我完全受编译器的支配?

Alo*_*ave 4

您完全受内联编译器的支配。
抛开部分不谈,是否使用inline函数完全是编译器最好做出的决定,您应该依靠它来做出最佳决定。

  • @Will如果你使用GCC,你可以使用`__attribute__((hot))`告诉编译器进一步优化该函数,这样它就不必猜测。 (3认同)
  • @WillBuddha:还有配置文件引导的优化(您编译并运行用于生成配置文件数据的程序,然后将该配置文件数据反馈回编译器以进行优化构建)。请参阅 GCC 的“-fprofile-generate”和“-fprofile-use”选项](http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)。 (3认同)