I often find myself writing a code that looks something like this:
if(a == nullptr) throw std::runtime_error("error at " __FILE__ ":" S__LINE__);
Should I prefer handling errors with if unlikely?
if unlikely(a == nullptr) throw std::runtime_error("error at " __FILE__ ":" S__LINE__);
Will the compiler automatically deduce which part of the code should be cached or is this an actually useful thing to do? Why do I not see many people handling errors like this?
For cases like that I'd prefer moving code that throws to a standalone extern function that's marked as
noreturn. This way, your actual code isn't "polluted" with lots of exception-related code (or whatever your "hard crashing" code). Contrary to the accepted answer, you don't need to mark it ascold, but you really neednoreturnto make compiler not to try generating code to preserve registers or whatever state and essentially assume that after going there there is no way back.For example, if you write code this way:
compiler will generate lots of instructions that deal with constructing and throwing this exception. You also introduce dependency on
std::runtime_error. Check out how generated code will look like if you have just three checks like that in yourtestfunction:First improvement: to move it to a standalone function:
this way you avoid generating all that exception related code inside your function. Right away generated instructions become simpler and cleaner and reduce effect on the instructions that are generated by your actual code where you perform checks:
There is still room for improvement. Since you know that your
my_runtime_errorwon't return you should let the compiler know about it, so that it wouldn't need to preserve registers before callingmy_runtime_error:When you use it multiple times in your code you can see that generated code is much smaller and reduces effect on instructions that are generated by your actual code:
As you can see, this way compiler doesn't need to preserve registers before calling your
my_runtime_error.I would also suggest against concatenating error strings with
__FILE__and__LINE__into monolithic error message strings. Pass them as standalone parameters and simply make a macro that passes them along!It may seem like there is more code generated per each my_runtime_error call (2 more instructions in case of x64 build), but the total size is actually smaller, as the saved size on constant strings is way larger than the extra code size.
Also, note that these code examples are good for showing benefit of making your "hard crashing" function an extern. Need for
noreturnbecomes more obvious in real code, for example:Generated assembly:
Try to remove
NORETURN, or change__attribute__((noreturn))to__attribute__((cold))and you'll see completely different generated assembly!As a last point (which is obvious IMO and was omitted). You need to define your
my_runtime_errorfunction in some cpp file. Since it's going to be one copy only, you can put whatever code you want in this function.One more point: clang actually recognizes that this type of function would benefit from
noreturnand warns about it if-Wmissing-noreturnwarning was enabled: