Daniel Lemire's blog
Speeding up C++ code with template lambdas
Let us consider a simple C++ function which divides all values in a range of integers:
If the divisor d is known at compile-time, this function can be much faster. E.g., if d is 2, the compiler might optimize away the division and use a shift and a few cheap instructions instead. The same is true with all compile-time constant: the compiler can often do better knowing the constant.
In C++, a template function is defined using the template keyword followed by a parameter (usually a type parameter) enclosed in angle brackets < >. The template parameter acts as a placeholder that gets replaced with actual data type when the function is called.
In C++, you can turn the division parameter into a template parameter:
The template function is not itself a function, but rather a recipe to generate functions: we provide the integer d and a function is created. This allows the compiler to work with a compile-time constant, producing faster code.
If you expect the divisor to be between 2 and 6, you can call the template function from a general-purpose function like so:
You could do it with a switch/case if you prefer but it does not simplify the code significantly.
Unfortunately we have to expose a template function, which creates noise in our code base. We would prefer to keep all the logic inside one function. We can do so with lambda functions.
In C++, a lambda function(or lambda expression) is an anonymous, inline function that you can define on-the-fly, typically for short-term use. Starting with C++20, you have template lambda expressions.
We can almost do it like so:
Unfortunately, it does not quite work. Given template lambda expressions, you cannot directly pass template parameters, and you need something ugly (‘template operator()<params>’):
In practice, it might still be a good choice. It keeps all the messy optimization hidden inside your function.
source
Speeding up C++ code with template lambdas
Let us consider a simple C++ function which divides all values in a range of integers:
void divide(std::span<int> i, int d) {
for (auto& value : i) {
value /= d;
}
}
If the divisor d is known at compile-time, this function can be much faster. E.g., if d is 2, the compiler might optimize away the division and use a shift and a few cheap instructions instead. The same is true with all compile-time constant: the compiler can often do better knowing the constant.
In C++, a template function is defined using the template keyword followed by a parameter (usually a type parameter) enclosed in angle brackets < >. The template parameter acts as a placeholder that gets replaced with actual data type when the function is called.
In C++, you can turn the division parameter into a template parameter:
template <int d>
void divide(std::span<int> i) {
for (auto& value : i) {
value /= d;
}
}
The template function is not itself a function, but rather a recipe to generate functions: we provide the integer d and a function is created. This allows the compiler to work with a compile-time constant, producing faster code.
If you expect the divisor to be between 2 and 6, you can call the template function from a general-purpose function like so:
void divide_fast(std::span<int> i, int d) {
if(d == 2) {
return divide<2>(i);
}
if(d == 3) {
return divide<3>(i);
}
if(d == 4) {
return divide<4>(i);
}
if(d == 5) {
return divide<5>(i);
}
if(d == 6) {
return divide<6>(i);
}
for (auto& value : i) {
value /= d;
}
}
You could do it with a switch/case if you prefer but it does not simplify the code significantly.
Unfortunately we have to expose a template function, which creates noise in our code base. We would prefer to keep all the logic inside one function. We can do so with lambda functions.
In C++, a lambda function(or lambda expression) is an anonymous, inline function that you can define on-the-fly, typically for short-term use. Starting with C++20, you have template lambda expressions.
We can almost do it like so:
void divide_fast(std::span<int> i, int d) {
auto f = [&i]<int divisor>() {
for (auto& value : i) {
value /= divisor;
}
};
if(d == 2) {
return f<2>();
}
if(d == 3) {
return f<3>();
}
if(d == 4) {
return f<4>();
}
if(d == 5) {
return f<5>();
}
if(d == 6) {
return f<6>();
}
for (auto& value : i) {
value /= d;
}
}
Unfortunately, it does not quite work. Given template lambda expressions, you cannot directly pass template parameters, and you need something ugly (‘template operator()<params>’):
void divide_fast(std::span<int> i, int d) {
auto f = [&i]<int divisor>() {
for (auto& value : i) {
value /= divisor;
}
};
if(d == 2) {
return f.template operator()<2>();
}
if(d == 3) {
return f.template operator()<3>();
}
if(d == 4) {
return f.template operator()<4>();
}
if(d == 5) {
return f.template operator()<5>();
}
if(d == 6) {
return f.template operator()<6>();
}
for (auto& value : i) {
value /= d;
}
}
In practice, it might still be a good choice. It keeps all the messy optimization hidden inside your function.
source