Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to declare inline functions noexcept?

From what I can tell, the SO community is divided on whether declaring a function noexcept enables meaningful compiler optimizations that would not otherwise be possible. (I'm talking specifically about compiler optimizations, not library implementation optimizations based on move_if_noexcept.) For purposes of this question, let's assume that noexcept does make meaningful code-generation optimizations possible. With that assumption, does it make sense to declare inline functions noexcept? Assuming such functions are actually inlined, this would seem to require that compilers generate the equivalent of a try block around the code resulting from the inline function at the call site, because if an exception arises in that region, terminate must be called. Without noexcept, that try block would seem to be unnecessary.

My original interest was in whether it made sense to declare Lambda functions noexcept, given that they are implicitly inline, but then I realized that the same issues arise for any inline function, not just Lambdas.

like image 353
KnowItAllWannabe Avatar asked Jan 30 '14 17:01

KnowItAllWannabe


People also ask

When it is advisable to use an inline function?

One should use the inline function qualifier only when the function code is small. If the functions are larger you should prefer the normal functions since the saving in memory space is worth the comparatively small sacrifice in execution speed.

Does inline improve performance?

Inline function expansion can speed up execution by eliminating function call overhead. This is particularly beneficial for very small functions that are called frequently. Function inlining involves a tradeoff between execution speed and code size, because the code is duplicated at each function call site.

Is inline always faster?

Unless your "CPU meter" is pegged at 100%, inline functions probably won't make your system faster. (Even in CPU-bound systems, inline will help only when used within the bottleneck itself, and the bottleneck is typically in only a small percentage of the code.)

Why do we need inline function Kotlin?

Inline functions are useful when a function accepts another function or lambda as a parameter. You can use an inline function when you need to prevent "object creation" and have better control flow.


1 Answers

let's assume that noexcept does make meaningful code-generation optimizations possible

OK

Assuming such functions are actually inlined, this would seem to require that compilers generate the equivalent of a try block around the code resulting from the inline function at the call site, because if an exception arises in that region

Not necessarily, because it might be that the compiler can look at the function body and see that it cannot possibly throw anything. Therefore the nominal exception-handling can be elided.

If the function is "fully" inlined (that is, if the inlined code contains no function calls) then I would expect that the compiler can fairly commonly make this determination -- but not for example in a case where there's a call to vector::push_back() and the writer of the function knows that sufficient space has been reserved but the compiler doesn't.

Be aware also that in a good implementation a try block might not actually require any code at all to be executed in the case where nothing is thrown.

With that assumption, does it make sense to declare inline functions noexcept?

Yes, in order to get whatever the assumed optimizations are of noexcept.

like image 165
Steve Jessop Avatar answered Sep 29 '22 09:09

Steve Jessop