是否可以内联 lambda 表达式?

发布于 2024-09-10 15:21:17 字数 41 浏览 5 评论 0原文

我想内联 lambda 表达式,因为出于性能原因它非常短。是否可以?

I want to inline a lambda expression since it is very short for performance reason. Is it possible?

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

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

发布评论

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

评论(6

晨敛清荷 2024-09-17 15:21:17

inline 关键字实际上不会导致函数内联。任何最近的编译器在内联方面都会比您做出更好的决定。

在 lambda 较短的情况下,该函数可能会被内联。

如果您尝试将 inline 关键字与 lambda 一起使用,答案是否定的,您不能使用它。

The inline keyword does not actually cause functions to be inlined. Any recent compiler is going to make better decisions with regards to inlining than you will.

In the case of a short lambda, the function will probably be inlined.

If you're trying to use the inline keyword with a lambda, the answer is no, you can't use that.

◇流星雨 2024-09-17 15:21:17

如果可以的话,编译器将内联它。例如,在 g++ 4.5 中使用 -O2,

#include <vector>
#include <algorithm>

int main () {
    std::vector<int> a(10);
    for (int i = 0; i < 10; ++ i) a[i] = i;

    asm ("Ltransform_begin: nop; nop; nop; nop; nop; nop; ");
    std::transform(a.begin(), a.end(), a.begin(), [] (int x) { return 2*x; });
    asm ("Lforeach_begin: nop; nop; nop; nop; nop; nop; ");
    std::for_each(a.begin(), a.end(), [] (int x) { printf("%d\n", x); });
    asm ("Lforeach_done: nop; nop; nop; nop; nop; nop; ");

    return 0;
}

生成 2*xprintf lambda 完全内联的程序集。

# 9 "x.cpp" 1
    Ltransform_begin: nop; nop; nop; nop; nop; nop; 
# 0 "" 2
    .align 4,0x90
L13:
    sall    (%rax)
    addq    $4, %rax
    cmpq    %rax, %r12
    jne L13
# 13 "x.cpp" 1
    Lforeach_begin: nop; nop; nop; nop; nop; nop; 
# 0 "" 2
    .align 4,0x90
L14:
    movl    (%rbx), %esi
    leaq    LC0(%rip), %rdi
    xorl    %eax, %eax
LEHB1:
    call    _printf
LEHE1:
    addq    $4, %rbx
    cmpq    %r12, %rbx
    jne L14
# 17 "x.cpp" 1
    Lforeach_done: nop; nop; nop; nop; nop; nop; 
# 0 "" 2

The compiler will inline it if it can. For example, in g++ 4.5 with -O2,

#include <vector>
#include <algorithm>

int main () {
    std::vector<int> a(10);
    for (int i = 0; i < 10; ++ i) a[i] = i;

    asm ("Ltransform_begin: nop; nop; nop; nop; nop; nop; ");
    std::transform(a.begin(), a.end(), a.begin(), [] (int x) { return 2*x; });
    asm ("Lforeach_begin: nop; nop; nop; nop; nop; nop; ");
    std::for_each(a.begin(), a.end(), [] (int x) { printf("%d\n", x); });
    asm ("Lforeach_done: nop; nop; nop; nop; nop; nop; ");

    return 0;
}

generates the assembly that the 2*x and printf lambdas are completely inlined.

# 9 "x.cpp" 1
    Ltransform_begin: nop; nop; nop; nop; nop; nop; 
# 0 "" 2
    .align 4,0x90
L13:
    sall    (%rax)
    addq    $4, %rax
    cmpq    %rax, %r12
    jne L13
# 13 "x.cpp" 1
    Lforeach_begin: nop; nop; nop; nop; nop; nop; 
# 0 "" 2
    .align 4,0x90
L14:
    movl    (%rbx), %esi
    leaq    LC0(%rip), %rdi
    xorl    %eax, %eax
LEHB1:
    call    _printf
LEHE1:
    addq    $4, %rbx
    cmpq    %r12, %rbx
    jne L14
# 17 "x.cpp" 1
    Lforeach_done: nop; nop; nop; nop; nop; nop; 
# 0 "" 2
终陌 2024-09-17 15:21:17

lambda 表达式可能是内联的。从本质上讲,lambda 表达式与任何其他函数对象没有什么不同。

特定的 lambda 表达式是否内联完全取决于编译器以及编译器是否决定其值得内联。

It is possible that a lambda expression might be inlined. Under the hood, a lambda expression is no different than any other function object.

Whether a particular lambda expression is inlined is entirely dependent upon the compiler and whether it decides it is worth inlining.

行至春深 2024-09-17 15:21:17

如果你有一个常规的结构函子,编译器几乎肯定会内联它。如果您有 C++0x 风格的 lambda,编译器几乎肯定会内联它。如果您使用的是 boost::lambda,那么它可能会这样做,具体取决于 lambda 在幕后的工作方式。简短版本:您不能保证它是内联或非内联,但您应该相信您的编译器,如果有疑问,请使其轻松简单地内联。

If you have a regular struct functor, the compiler will almost certainly inline it. If you have a C++0x style lambda, the compiler will almost certainly inline it. If you're using a boost::lambda, then it might do, depending on how the lambda works underneath the scenes. Short version: You can't guarantee it's inlining or non-inlining, but you should trust your compiler and if in doubt, make it easy and simple to inline.

谎言月老 2024-09-17 15:21:17

我还没有以任何方式查看其中很多的输出,但到目前为止,我查看过的所有输出都已内联。

I haven't looked at the output from a lot of them by any means, but so far all of them I've looked at the output from have been inlined.

鲸落 2024-09-17 15:21:17

C++1x' lambdas 将在底层生成普通函数对象。这些可以由编译器内联。

您所做的任何测量是否表明编译器没有将它们内联到导致显着性能损失的位置?

C++1x' lambdas will, under the hood, generate normal function objects. Those can be inlined by the compiler.

Did any measurements you made suggest the compiler did not inline them in a place where this leads to a notable performance loss?

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