|
D
oes
software efficiency matter? This is the provocative question with which
Dov Bulka and David Mayhew introduce their readers to the topic of performance
programming in C++. Indeed, we find ourselves in an era where interpreted
languages (such as Java and VB) enjoy increasing attention and where human-readable
data exchange formats (such as XML and HTML) are preferred over binary
formats. In former times we used to shy away from such obvious performance
impediments, but today there is a seemingly unbroken belief prevalent in
the IT industry that processor development will overcome any performance
problems sooner or later. Bulka and Mayhew disagree, and we join their
opposition. There is no excuse for performance-degrading design and programming
style. In many software development domains, execution-time characteristics
still play a significant role. For those C++ programmers who care about
software efficiency,
Efficient C++: Performance Programming Techniques
is an excellent resource.
Unfortunately, C++ has a bad reputation
as an inefficient programming language compared to C or assembler language.
This is partly because in C++ the source-to-assembler code mapping is no
longer a one-to-one identity, which in turn requires that the C++ programmer
understand the cost of C++ language constructs. In C++ you are not going
to achieve good performance accidentally without knowing the pitfalls.
On the other hand, much of the reservation towards C++ stems from its infancy,
when C++ compilers were still immature. Today, however, compiler technology
has greatly matured and compiler builders invest much energy in optimization
strategies. But these realizations have not yet reached the industry in
all cases. There are still developers around?especially in embedded systems
environments?who believe that templates are evil and will result in code
size explosion just because early compilers indeed produced considerable
code bloat. But this is history. Those who are concerned with software
efficiency can greatly profit from the use of modern language features.
Efficient
C++
sheds some light onto this type of misconception.
The book itself is informative and
covers a broad range of efficiency-related topics, including coding style,
design optimizations, concurrency control, and even system architecture
dependencies. It examines all potentially inefficient C++ language constructs,
demonstrates misuse, and suggests remedies.
Specifically:
-
The cost of construction and destruction,
temporaries, type conversions, etc.
While this should be painfully
obvious for every experienced developer, you cannot repeat it often enough.
The authors point out that redundant construction and lazy evaluation are
a solution to certain problems in this area. They discuss return value
optimization and argument and return value passing by value vs. reference.
Everything that is state-of-the-art in this area is covered in this book.
There is also an entire chapter devoted to reference counting.
-
Virtual functions and inlining.
The overhead of virtual function calls should be obvious to the average
C++ programmer, too. However, we are sure that only a few developers are
aware of the fact that the true cost of virtual functions stems from the
inability to inline them. This book devotes two chapters exclusively to
inline functions. Bulka and Meyhew explain in detail the cost of function
calls and the benefits of inlining (like enabling cross-call optimization),
and provide countless practical tips for proper inlining. This is good
because overly aggressive inlining can have an adverse effect and degrade
efficiency. Inlining must therefore be done with care and prudence.
-
The STL.
The STL is an uncommon
combination of flexibility and efficiency, two principles that usually
exclude each other. In the STL they are successfully combined. The basic
message is this: Use it?it's almost impossible to do any better and beat
its performance.
-
Memory allocation and concurrency
control.
The authors explain the cost of NEW and DELETE and give examples
of more efficient alternatives?that is, memory pools for both single- and
multi-threaded environments. Their chapter devoted to multi-threaded programs
discusses data sharing problems and various locking techniques. For a full
coverage of concurrent programming, you should certainly consult a book
like Doug Lea's
Concurrent Programming in Java
, yet it is helpful
to have the main points covered in this book under the aspect of performance
programming.
-
Coding and design optimizations.
This
is a collection of various techniques, such as caching, lazy evaluation,
reference counting, and elimination of useless computations. The latter
is particularly striking. They write, "The fastest code is the one that
never executes." Well, that sounds like common sense, but the authors show
convincing examples that we've seen in practice, too, which prove that
common sense is not so common.
-
Systems architecture.
In the
closing chapter Bulka and Mayhew look at efficiency from yet another vantage
point: how efficient the underlying hardware is. They discuss disk and
memory structures, register sets, caching and virtual memory, the cost
of context switches, and threading choices. It's good to remind software
engineers that not all problems can be solved by proper software design
alone and that there is an interaction between software and hardware.
By and large,
Efficient C++: Performance
Programming Techniques
is brilliant. It approaches software efficiency
from all conceivable sides and provides comprehensive coverage of the topic.
It is full of practical tips and tricks, which prove that the authors have
substantial practical experience in squeezing every ounce of performance
out of large C++ systems. This book is highly recommendable and a must-read
for every serious C++ programmer.
Angelika Langer
develops
and teaches classes on advanced C++, STL, multithreading, internationalization,
and Java. She served on the ANSI/ISO C++ Committee from 1993 to 1998.
Klaus
Kreft
is a software architect and consultant with 15+ years of experience
in industrial software development. He works for Siemens Business Services
in Germany. Langer and Kreft are authors of "Standard C++ IOStreams and
Locales: Advanced Programmer's Guide and Reference" (Addison-Wesley, 1999)
and are columnists for
C++
Report
magazine.
|