Advertisement

Performance with using exceptions in C++ game programming

Started by June 13, 2017 01:07 AM
11 comments, last by Hodgman 7 years, 5 months ago

The belief in not using exceptions in C++ because they are slow is a classic example of cargo cult programming.

Sure, there are old timers (including me) who remembers MSVC6 and wretchedly slow code introduced by exceptions as a result of a ghastly implementation. It's been decades since then, and the benefits of exceptions vs. all the alternatives have been demonstrated again and again, and with numbers to back that up.

If you want to be sure your code is comparatively fast or slow, you run timing benchmarks on alternatives. If you want want to join the school of premature optimization and cargo cult programming, you simply and mindlessly follow dogma.

Stephen M. Webb
Professional Free Software Developer

*cough* https://raw.githubusercontent.com/KhronosGroup/Vulkan-Hpp/master/vulkan/vulkan.hpp Sorry, I couldn't resist :)



#ifdef VULKAN_HPP_NO_EXCEPTIONS

So they allow both, with and without exceptions. Vulkan is aimed to be widely used thought. That could explains that.

Advertisement

You'll find that almost all C++ game middleware will avoid exceptions too

*cough* https://raw.githubusercontent.com/KhronosGroup/Vulkan-Hpp/master/vulkan/vulkan.hpp
Sorry, I couldn't resist :)
That kind of proves my point :)
They use C for a stable ABI, and then in their C++ wrapper, they let you choose between an API with strict preconditions and undefined behaviour if you violate those contracts, or, an API that tolerates invalid code by implementing a contract validation layer that throws exceptions. If they only had the latter API, it would be regarded as broken and unusable by many users, not to mention being unnecessarily bloated (correct code doesn't need the validation layer)... And if they only had the former, it would be regarded as broken and unusable by a different set of users, not to mention being unnecessarily fragile and error-prone. It's as if C++ is actually two (or more) completely distinct languages.
They're doing the right thing by making a multi-layered API that's usable by everyone.

the benefits of exceptions vs. all the alternatives have been demonstrated again and again, and with numbers to back that up.

Exactly; exception-free C++ code has enormous code maintainability benefits, which is the real reason why no one uses them in serious large scale projects.
( :wink: yes I'm trolling you, Bregma )
It is interesting though that our two different communities have completely different common sense here. My experience with gamedevs has common sense saying that C++ exceptions add a huge amount of extra complexity to the entire code base for very little gain, and that a good use for the construct is extremely rare anyway (we tend to have pseudo-realtime loops made up of many parallel and asynchronous tasks, resulting in call-stacks that have no relationship to task hierarchies anyway)... Meanwhile your experience with professional FOSS app dev has pretty much completely opposite lessons / conclusions :|

This topic is closed to new replies.

Advertisement