Balgy
Balgy

Reputation: 498

Pointer Protection and Performance

I protect my pointers with if statements when developing to avoid crashing my application even if in theory the pointer should not be null.

My question is, will removing these if checks after I am convinced that the pointer will indeed not be null under any circumstance improve the performance of my application by a non - trivial ammount?

Upvotes: 1

Views: 129

Answers (1)

Jacob Leach
Jacob Leach

Reputation: 76

If you're worried about performance but safety is still a concern you may consider adding the __builtin_expect (which most modern compilers support) flag to the if statements. This would preserve the safety precautions you have set up in your program but would tell the compiler to optimize the branch's jump labels for the condition where the pointer is not null.

However this still entails some slowdown because the branch conditions are still being computed. If the pointer is never explicitly declared as null it would be extremely advantageous to use a reference instead.

Upvotes: 2

Related Questions