Home Backend Development C++ Why do Lambdas have an Inlining Advantage Over Functions in Compiler Optimization?

Why do Lambdas have an Inlining Advantage Over Functions in Compiler Optimization?

Nov 16, 2024 pm 10:55 PM

Why do Lambdas have an Inlining Advantage Over Functions in Compiler Optimization?

Why Lambdas Outperform Functions in Compiler Optimization

In his book "The C Standard Library (Second Edition)," Nicolai Josuttis claims that lambdas enjoy superior compiler optimization compared to ordinary functions. This distinction stems from the nature of lambdas as function objects, allowing for function template instantiation tailored to specific lambda objects.

Inlining Advantage for Lambdas

Unlike plain functions, which pass function pointers to function templates, lambdas are passed as function objects. This triggers the creation of a new function tailored to the specific lambda object. As a result, the compiler can effortlessly inline the lambda call.

Function Pointer Hindrance

In contrast, functions are hindered by their passing mechanism in function templates. Compilers face challenges in inlining calls made through function pointers. While theoretical inlining is possible, it requires the inlining of the enclosing function as well.

Instantiation Example

Consider the function template "map" below:

template <typename Iter, typename F>
void map(Iter begin, Iter end, F f) {
    for (; begin != end; ++begin)
        *begin = f(*begin);
}
Copy after login

Invoking "map" with a lambda:

int a[] = { 1, 2, 3, 4 };
map(begin(a), end(a), [](int n) { return n * 2; });
Copy after login

Generates the following instantiation:

template <>
void map<int*, _some_lambda_type>(int* begin, int* end, _some_lambda_type f) {
    for (; begin != end; ++begin)
        *begin = f.operator()(*begin);
}
Copy after login

The compiler identifies the lambda's operator() and can effortlessly inline calls to it.

However, when "map" is called with a function pointer:

template <>
void map<int*, int (*)(int)>(int* begin, int* end, int (*f)(int)) {
    for (; begin != end; ++begin)
        *begin = f(*begin);
}
Copy after login

The function pointer "f" points to a different location for each invocation of "map," making it intractable for the compiler to inline "f" calls alone. Inlining requires the encompassing "map" call to be inlined as well, enabling the compiler to determine the specific function pointed to by "f."

The above is the detailed content of Why do Lambdas have an Inlining Advantage Over Functions in Compiler Optimization?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

See all articles