C 内联函数和内存使用

发布于 2024-07-09 15:45:39 字数 27 浏览 8 评论 0原文

如果我使用内联函数,内存使用量会增加吗?

If I use inline functions, does the memory usage increase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

久隐师 2024-07-16 15:45:39

内联函数会影响两种内存使用情况:

代码大小 - 一般来说,内联代码会增加加载程序所使用的内存量。 这是因为生成的代码的多个副本分散在您的程序中。 然而,情况并非总是如此——如果您的内联函数仅使用一次,则几乎没有变化,并且如果内联函数非常小,则可以通过消除函数调用开销来净减少代码大小。 此外,优化器可以减小函数的大小,优化器能够删除特定内联调用中未使用的代码。

堆栈使用 - 如果您的内联函数有大量局部变量,那么您可能会使用更多堆栈空间。 在 C 中,编译器通常在进入函数时为函数分配一次堆栈空间。 它必须足够大才能容纳所有未存储在寄存器中的局部变量。 如果您离线调用函数,则将使用该函数的堆栈,直到它返回为止,然后再次释放它。 如果您内联该函数,那么该堆栈空间将在超级函数的整个生命周期中保持使用状态。

内联不会影响堆的使用,因为内联代码会发生与非内联版本相同的分配和释放。

There are two kinds of memory usage that inline functions will affect:

code size — in general, inlining code will increase how much memory is used to load your program. This is because there will be multiple copies of the generated code scattered around your program. However, this isn't always true -- if your inlined function was only used once, there's little change, and if the inlined function is very small, you could get a net reduction in code size by removing the function call overhead. Also, the function may be reduced in size by the optimizer that's able to remove code that's not used in the particular inline invocation.

stack usage — If your inlined functions have lots of local variables, then you may use more stack space. In C, the compiler usually allocates the stack space for a function once upon entry to the function. This has to be large enough to hold all the local variables that aren't stored in registers. If you call a function out-of-line, the stack for that function is used until it returns, when it's released again. If you inline the function, then that stack space will remain used for the whole life of the uber-function.

Inlining won't affect heap usage, as the same allocations and deallocations would occur for the inlined code as would occur for the non-inlined version.

注定孤独终老 2024-07-16 15:45:39

您还必须考虑另一点:

使用内联函数,编译器能够看到调用方的变量将在被调用方中用作变量。 编译器可以基于这些知识优化冗余代码(通常这实际上是可以省略的许多汇编程序行。请注意所谓的“别名问题”)。 因此,你的“代码膨胀”通常不会那么大,特别是如果你有较小的函数,它甚至可以减少膨胀,正如吉姆上面所说的那样。

有人提出了一个很好的观点:最好让编译器决定是否内联有问题的函数,因为它比你更了解它生成的代码。

There is another point you have to consider:

Using inline functions, the compiler is able to see where variables of the caller are going to be used as variables in the callee. The compiler can optimize out (often this is really many assembler lines that can be omitted. look out for the so called "aliasing problem") redundant code based on that knowledge. So your "code bloat" is often not all that big, especially if you have smaller functions it can even reduce bloat as Jim stated above.

Someone made a good point: Better make the compiler decide whether it inlines the function in question or not, since it knows the code it generates better than you ever would.

下壹個目標 2024-07-16 15:45:39

取决于功能。 简单的单行代码可以减少内存,因为不需要设置和清理调用堆栈,也不需要进行函数调用。 如果函数大于调用函数所需的开销,那么它当然会使代码变得臃肿。

Depends on the function. Simple one-liners could have a memory reduction since no callstack needs to be setup and cleaned and no function call is made. If the function is larger than this overhead needed to call a function, then it will of course bloat the code.

镜花水月 2024-07-16 15:45:39

这在一般情况下确实无法回答。

首先,您通常无法控制内线。 即使您将函数标记为内联,它实际上仍然取决于编译器,它实际上会执行内联(这只是一个提示)。

编译器会尽力优化代码; 使用内联只是实现此目的的一种工具。 因此,内联短函数将使代码更小(因为您不需要设置调用参数或检索返回值。但即使使用长函数,答案也不是绝对的。

如果编译器决定内联长函数那么您会认为代码会变得更长,但情况通常并非如此;因为这为编译器提供了额外的机会来应用其他优化技术,如果编译器分析发现生成的代码会变得更小。 基本上,编译

器会进行分析并决定最佳的操作方案,因此

编译器比您更聪明,并且会做正确的事情。

That is really un-answerable in the general case.

To start with you do not generally have control over the in-lining. Even if you mark a function inline it is actually still up to the compiler wither it will actually do the in-lining (it is just a hint).

The compiler will do its best to optimize the code; using in-lining is just one tool in doing this. So inlining short functions will make the code smaller (as you don't need to set up the parameters for the call or retrieve the return value. But even with long functions the answer is not absolute.

If the compiler decides to inline a long function then you would think the code would get longer. But this is not generally the case; as this gives the compiler extra opportunities to apply other optimization techniques that could potentially make the code still smaller. If the compilers analysis finds that the resulting code bloat is detrimental to the code the in-lining will not be done.

Basically the compiler does its analysis and decides and the best course of action.

Conclusion. Don't worry about it. The compiler is smarter than you and will do the correct thing.

何其悲哀 2024-07-16 15:45:39

内联函数肯定会增加最终可执行文件(或二进制文件)的大小,因为无论您在哪里调用它们,它们都会被“复制粘贴”。

Inline functions definitely increase the size of your final executable(or binary), because they will be "copy-pasted" whereever you call them.

落叶缤纷 2024-07-16 15:45:39

在一般情况下,您的程序会变得更大(但我确信也有例外)。 运行时内存消耗可能会下降,但幅度不会很大。

你问来干什么? 通常,您让编译器确定函数是否应该内联; 考虑到函数的大小和复杂性,它通常可以做出更好的调用。

You program will in the general case become larger (I'm sure there are exceptions, though). The runtime memory consumption might go down, but not by much.

Why are you asking? Normally, you let the compiler determine whether a function should be inlined or not; it can usually make a better call given the size and complexity of the function.

枫林﹌晚霞¤ 2024-07-16 15:45:39

函数调用需要多个处理器指令。

通常,函数的每个参数都需要一条 PUSH 指令,调用该函数的 CALL 指令,通常还需要另一条在函数调用后清理堆栈的指令。

此外,函数可能会修改处理器的寄存器,因此调用函数可能需要更多指令来保留寄存器或重新加载本来仍在寄存器中的值。

因此,如果您调用的函数只有几条指令长,则内联它可以节省内存并且运行得更快。

也就是说,内联适用于您的分析器告诉您应该进行内联的情况。

A function call requires several processor instructions.

You usually need a PUSH instruction for every argument to the function, a CALL instruction to call the function, and often another instruction that cleans up the stack after the function call.

Also, functions may modify the processor's registers, so the calling function may need more instructions to preserve registers or reload values that would otherwise still be in registers.

So if the function you're calling is just a few instructions long, inlining it can save memory and run faster.

That said, inlining is for when your profiler tells you that you should.

烈酒灼喉 2024-07-16 15:45:39

有时会出现这样的情况,我们的函数分散在整个程序中。在这种情况下,函数调用会导致程序跳转到该函数的地址,并在函数调用终止时返回。 这会占用一些宝贵的时间。

使用内联函数可以解决上述问题。 这会导致编译器直接从源代码调用代码。 不会为内联函数代码创建新的内存指令集。

虽然c++中的内联声明是自由的,并且在声明中定义函数时自动发生,但在c中它受到以下规则的限制 ::

  1. 在C中,任何具有内部链接的函数都可以声明为内联,但函数具有外部链接的函数具有内联限制。

  2. 如果在函数声明中使用 inline 关键字,则函数定义应出现在同一翻译单元中。

    如果

内联数据类型 function_name(arguments)

此代码的运行速度比非内联函数快 30%,其余部分取决于处理器速度。

现在是策略部分。 您可以随意使用内联函数,但请记住,内联函数的执行时间要少得多,但它们在运行时会占用很高的内存。 此外,如果声明内联的代码与代码大小相比异常大,编译器始终可以选择忽略内联声明。

内联声明虽然破坏了求值的顺序,但并不使函数成为内部函数。 该功能仍然是外部的。

It sometimes so happens that we have functions scattered all over the program.In this case a function call causes the program to jumps to the address of the function and come back when the function call terminates. This takes away some precious time.

The above problem can be resolved with the use of inline functions. This causes the compiler to call the code directly from the source. No new memory instruction set is created for the inline function code.

Although the inline declaration in c++ is free and occurs automatically when the function is defined in the declaration, in c it is restricted by the following rules ::

  1. In C, any function with internal linkage can be declared inline, but a function with external linkage is has restrictions on inline.

  2. If the inline keyword is used in the function declaration, then the function definition should be present in the same translation unit.

inline datatype function_name(arguments)

This code runs upto 30% fastert than a non inline function, the rest depending on the prcessor speed.

Now comes the strategy part. You may use inline functions at your will but keeping in mind that inline functions can take much less time to execute but they have a high memory occupancy on the run. Also the compiler has always an option to overlook your inline declaration if the code declared inline is abnormally large compared to the code size.

Inline declaration although destroys the order of evaluation, does not make the function internal. The function is still external.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文