C++ 我可以选择性地(强制)内联函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7108797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:28:29  来源:igfitidea点击:

Can I selectively (force) inline a function?

c++optimizationcoding-styleinline

提问by Samaursa

In the book Clean Code(and a couple of others I have come across and read) it is suggested to keep the functions small and break them up if they become large. It also suggests that functions should do one thing and one thing only.

Clean Code一书中(以及我遇到并阅读的其他一些),建议将函数保持在较小的范围内,如果它们变大,则将它们分解。它还建议函数应该做一件事,并且只做一件事。

In Optimizing software in C++Agner Fog states that he does not like the rule of breaking up a function just because it crosses a certain threshold of a number of lines. He states that this results in unnecessary jumps which degrade performance.

Optimizing software in C++ 中,Agner Fog 表示他不喜欢仅仅因为函数跨越了若干行的某个阈值而将其分解的规则。他指出,这会导致不必要的跳转,从而降低性能。

First off, I understand that it will not matter if the code I am working on is not in a tight loop and that the functions are heavy so that the time it takes to call them is dwarfed by the time the code in the function takes to execute. But let's assume that I am working with functions that are, most of the time, used by other objects/functions and are performing relatively trivial tasks. These functions follow the suggestions listed in the first paragraph (that is, perform one single function and are small/comprehensible). Then I start programming a performance critical function that utilizes these other functions in a tight loop and is essentially a frame function. Lastly, assume that in-lining them has a benefit for the performance critical function but no benefit whatsoever to any other function (yes, I have profiled this, albeit with a lot of copying and pasting which I want to avoid).

首先,我明白,如果我正在处理的代码不是紧密循环并且函数很重,那么调用它们所需的时间与函数中的代码所需的时间相形见绌无关紧要执行。但是让我们假设我正在使用大多数时间由其他对象/函数使用并且执行相对微不足道的任务的函数。这些函数遵循第一段中列出的建议(即,执行一个单一的函数并且很小/易于理解)。然后我开始编写一个性能关键函数,它在一个紧密的循环中利用这些其他函数,本质上是一个框架函数。最后,假设内联它们对性能关键功能有好处,但对任何其他功能没有任何好处(是的,我已经对此进行了分析,

Immediately, one can say that tag the function inlineand let the compiler choose. But what if I don't want all those functions to be in a `.inlfile or exposed in the header? In my current situation, the performance critical functions and the other functions it uses are all in the same source file.

立即,可以说标记函数inline并让编译器选择。但是,如果我不希望所有这些函数都在一个 ` .inl文件中或在头文件中公开怎么办?在我目前的情况下,性能关键函数和它使用的其他函数都在同一个源文件中。

To sum it up, can I selectively (force) inline a function(s) for a single function so that the end code behaves like it is one big function instead of several calls to other functions.

总而言之,我是否可以选择性地(强制)内联单个函数的函数,以便最终代码的行为就像一个大函数,而不是对其他函数的多次调用。

采纳答案by Puppy

You cannot force the inline. Also, function calls are pretty cheap on modern CPUs, compared to the cost of the work done. If your functions are large enough to need to be broken down, the additional time taken to do the call will be essentially nothing.

您不能强制内联。此外,与完成工作的成本相比,现代 CPU 上的函数调用非常便宜。如果您的函数大到需要分解,则调用所花费的额外时间基本上是零。

Failing that, you could ... try ... to use a macro.

否则,您可以……尝试……使用宏。

回答by rodrigo

There is nothing that prevents you to put inlinein a static function in a .cpp file.

没有什么可以阻止您将内联放入 .cpp 文件中的静态函数中。

Some compilers have the option to force an inline function, see e.g. the GCC attribute((always_inline)) and a ton of options to fine tune the inlining optimizations (see -minline-* parameters).

一些编译器可以选择强制使用内联函数,例如参见 GCC属性((always_inline)) 和大量用于微调内联优化的选项(参见 -minline-* 参数)。

My recommendation is to use inlineor even better static inlinewherever you see fit, and let the compiler decide. They usually do it pretty well.

我的建议是在您认为合适的任何地方使用内联或更好的静态内联,并让编译器决定。他们通常做得很好。

回答by Jacob

No, inlineis a recommendationto the compiler ; it does not force it to do anything. Also, if you're working with MSVC++, note that __forceinlineis a misnomer as well ; it's just a stronger recommendation than inline.

不,inline是对编译器的建议;它不会强迫它做任何事情。另外,如果您正在使用 MSVC++,请注意这__forceinline也是用词不当;这只是比inline.

回答by Travis Griggs

This is as much about good old fashioned straight C as it is about C++. I was pondering this the other day, because in an embedded world, where both speed and space need to be carefully managed, this can really matter (as opposed to the all too oft "don't worry about it, your compiler is smart and memory is cheap prevalent in desktop/server development).

这既是关于老式纯 C 的,也是关于 C++ 的。前几天我在思考这个问题,因为在嵌入式世界中,速度和空间都需要仔细管理,这真的很重要(与经常说的“别担心,你的编译器很聪明而且内存在台式机/服务器开发中很普遍)。

A possiblesolution that I have yet to vet is to basically use two names for the different variants, something like

我尚未的一个可能的解决方案是基本上为不同的变体使用两个名称,例如

inline int _max(int a, int b) {
    return a > b ? a : b;
}

and then

进而

int max(int a, int b) {
    return _max(a, b);
}

This would give one the ability to selectively call either _max() or max() and yet still having the algorithm defined once-and-only-once.

这将使人们能够有选择地调用 _max() 或 max() 并且仍然定义一次且仅一次的算法。

回答by deeznutts

Inlining – For example, if there exists a function A that frequently calls function B, and function B is relatively small, then profile-guided optimizationswill inline function B in function A.

内联——例如,如果存在一个频繁调用函数 B 的函数 A,而函数 B 相对较小,那么配置文件引导优化会将函数 B 内联到函数 A 中。

VS Profile-Guided Optimizations

VS 配置文件引导的优化

You can use the automated Profile Guided Optimization for Visual C++ plug-in in the Performance and Diagnostics Hub to simplify and streamline the optimization process within Visual Studio, or you can perform the optimization steps manually in Visual Studio or on the command line. We recommend the plug-in because it is easier to use.For information on how to get the plug-in and use it to optimize your app, see Profile Guided Optimization Plug-In.

您可以使用性能和诊断中心中的自动化 Profile Guided Optimization for Visual C++ 插件来简化和优化 Visual Studio 中的优化过程,或者您可以在 Visual Studio 中或在命令行中手动执行优化步骤。我们推荐插件,因为它更容易使用。有关如何获取插件并使用它来优化您的应用程序的信息,请参阅Profile Guided Optimization Plug-In

回答by Mark B

Compilers are actually really really good at generating optimized code.

编译器实际上非常擅长生成优化的代码。

I would suggest just organizing your code into logical groupings (using additional functions if that enhanced readability), marking them inline if appropriate, and letting the compiler decide what code to optimally generate.

我建议只将您的代码组织成逻辑分组(如果可以增强可读性,则使用附加函数),如果合适,将它们标记为内联,并让编译器决定以最佳方式生成哪些代码。