using c++ efficiently

Recently, I came upon this nice article about why C++ is a wise choice for game development.

The articles discuss why C++ is disliked by the “higher” and “lower” language lovers, I liked the article very much. From the low level side argumentation goes like:

The attacks on C++ come on two flanks.  From the low-level side comes the argument that the language supports too high a level of abstraction, and that we should go back to programming in straight C.

This is the argumentation I hear when talking with a C programmer. C++ supports multiple programming paradigms, generic (using templates), OOP (using classes and virtual functions) and imperative like C. Hence there is no need to use OOP or generic abstractions when you do not need to: it’s all about choosing the right abstraction for the right problem.

When describing complaints from the “higher” level side, the article goes:

The other camp of C++ criticism compares it to higher-level languages and finds C++ wanting. This camp holds that C++ suffers under the burden of backwards compatibility and slow ANSI/ISO standard evolution. It lacks features found in more modern languages, such as aspects, closures, garbage collection, reflection, and native support for concurrency. The paleolithic C++ physical structure–.h include files and .cpp implementation files–results in achingly slow build times and forces programmers to write every function definition twice, once where it’s declared in the header and once where it’s defined. We would be better off, according to this argument, writing our games in C#, Eiffel or Objective Caml.

Some of these abstractions will be part of the next standard (closures and garbage collection), and when they will be part of it, their use (and cost) will be optional. This is the C++ philosophy: you only pay for what you do use.

This article made me think: most of the people who hate the language do not know how to use it efficiently, maybe I should write something about it.

First of all: do not get me wrong, c++ is not a perfect language (none exist so far, hence our struggle to get things done ;-) ) but it is very suited to my damaged brain, and the following text will show you why.

So here are two programming areas and how I use c++ in those.

system programming

Use symmetry ! Put your IPC code for creation inside a constructor and its destruction code inside a destructor. This cleans up the code and provide you with symmetry, this is the Resource Acquisition Is Initialization idiom.

From a more general point of view, every resources allocation/de-allocation should try to use this idiom. It simplifies resources handling.

Exceptions should be used, mainly in RAII constructors. You should not be affraid to use them near system calls: system calls are slow. Keep this in mind when you are afraid to use them for performance reasons. So yes, this may counter intuitive, but syscalls are one of the best code area where exceptions can be used.

I/O processing

One of the main complaints I hear about C++ I/O, is that they are slow, very slow compared to C I/Os. And most of the time the reason is “because formatting takes to much time, this >> operator just sucks.”.

Yeah … Right. one thing 1st: I/Os are the slowest operation you can make when using a computer. yes the slowest, 1000 times slower that accessing memory. So if there is one thing that any language should be able to do as fast as any other language, this is I/O.

So why C++ I/O are considered slower ? Most of the time this is because of the syn_with_stdio flag which is set to true for every iostream you do open. C++ stream have this method, syn_with_stdio, available so you can synchronize your c++ stream with the C FILE* operations. But this is useful only if you do access the same file using these 2 apis at the same time. Most of the time this will never happen.

So next time you will use a C++ stream, check this method out.

Advertisements

About this entry