Home > Store > Programming > C/C++

larger cover

Add To My Wish List

Efficient C++: Performance Programming Techniques

Register your product to gain access to bonus material or receive a coupon.

  • Description
  • Reviews
  • Sample Content

Product Author Bios

Dov Bulka has spent fifteen years in the trenches of software development delivering large-scale software products to market. He was the performance architect of the IBM Domino-Go Web server that has powered some of the biggest Web sites ever hosted on the Internet, including that of the 1996 Atlanta Olympics. He received his Ph.D. in computer science from Duke University.

David Mayhew is Principal Architect of StarBridge Technologies, Inc., where he concentrates on interconnection fabrics, peer processing, and PCI bus evolution. Previously, he worked for IBM's Networking Software Division. He received his Ph.D. in computer science from Virginia Tech.



0201379503AB04062001

Far too many programmers and software designers consider efficient C++ to be an oxymoron. They regard C++ as inherently slow and inappropriate for performance-critical applications. Consequently, C++ has had little success penetrating domains such as networking, operating system kernels, device drivers, and others.

Efficient C++ explodes that myth. Written by two authors with first-hand experience wringing the last ounce of performance from commercial C++ applications, this book demonstrates the potential of C++ to produce highly efficient programs. The book reveals practical, everyday object-oriented design principles and C++ coding techniques that can yield large performance improvements. It points out common pitfalls in both design and code that generate hidden operating costs.

This book focuses on combining C++'s power and flexibility with high performance and scalability, resulting in the best of both worlds. Specific topics include temporary objects, memory management, templates, inheritance, virtual functions, inlining, reference-counting, STL, and much more.

With this book, you will have a valuable compendium of the best performance techniques at your fingertips.



0201379503B04062001

Customer Reviews

29 of 30 people found the following review helpful
4.0 out of 5 stars Keys to acceptable C++ performance, January 4, 2001
By 
Daniel Mall (San Gabriel, CA United States) - See all my reviews
(REAL NAME)   
This review is from: Efficient C++: Performance Programming Techniques (Paperback)
The authors are developers of network and web server software for IBM. They specifically target performance issues related to C++ programming. The primary topics of discussion are constructors & destructors, virtual functions, return value optimization, temporary objects, inlining, memory managers, and reference counting. Learn how to create efficient constructor functions. Learn how to avoid the costly construction of temporary objects. The authors promote "lazy evaluation" and nail "useless computations" and "obsolete code." Algorithm and data structure discussions are referred to other standard references, however there is coverage of the STL structures (insertion, deletion, traversal, and find operations). An additional bonus is the excellent coverage of multithreading issues.
Help other customers find the most helpful reviews 
Was this review helpful to you? Yes No


23 of 24 people found the following review helpful
4.0 out of 5 stars Recommended, with reservations, June 16, 2000
By 
George V. Reilly (Seattle, Wash, USA) - See all my reviews
(REAL NAME)   
This review is from: Efficient C++: Performance Programming Techniques (Paperback)
I thought the book a useful compendium of performance information, as far as it went: temporaries, constructors and destructors, virtual functions, inlining, reference counting, some STL, multiprocessor scalability, and system architecture dependencies. My reservations are the number of omissions (most of the Standard Library, space efficiency, exceptions, performance measurement, etc.) and the number of typos.
Help other customers find the most helpful reviews 
Was this review helpful to you? Yes No


16 of 17 people found the following review helpful
5.0 out of 5 stars Informative through painful personal experiences, February 25, 2000
By 
Robert A. Gibson "softwareengineer" (Research Triangle Park, NC - USA) - See all my reviews
(REAL NAME)   
This review is from: Efficient C++: Performance Programming Techniques (Paperback)

To start, I have to say that this book is well written. By using examples from personal experience, they also keep you very involved. So far, I have been forced to put the book down, in order to test the examples they describe.

I was astonished with their first example, where a poorly thought out Trace class caused performance to drop 80%.

Talk about bang for the buck! By reading this book, I expect to give a great deal more thought to the performance implications from the start.

Good job guys!

Help other customers find the most helpful reviews 
Was this review helpful to you? Yes No


Share your thoughts with other customers:
 See all 16 customer reviews...

Praise For Efficient C++: Performance Programming Techniques

"You should be able to relate every example in this book to some project on which you have worked. Spend a weekend with this book, and you will significantly increase the efficiency of your existing applications." --Computer.org, May 2001

Table of Contents

(Each chapter concludes with Key Points.)

Preface.


Introduction.


Roots of Software Inefficiency.


Our Goal.


Software Efficiency: Does It Matter?


Terminology.


Organization of This Book.


1. The Tracing War Story.

Our Initial Trace Implementation.

What Went Wrong.

The Recovery Plan.



2. Constructors and Destructors.

Inheritance.

Composition.

Lazy Construction.

Redundant Construction.

Key Points.



3. Virtual Functions.

Virtual Function Mechanics.

Templates and Inheritance.

Hard Coding.

Inheritance.

Templates.



4. The Return Value Optimization.

The Mechanics of Return-by-Value.

The Return Value Optimization.

Computational Constructors.



5. Temporaries.

Object Definition.

Type Mismatch.

Pass by Value.

Return by Value.

Eliminate Temporaries with op=().



6. Single-Threaded Memory Pooling.

Version 0: The Global new() and delete().

Version 1: Specialized Rational Memory Manager.

Version 2: Fixed-Size Object Memory Pool.

Version 3: Single-Threaded Variable-Size Memory Manager.



7. Multithreaded Memory Pooling.

Version 4: Implementation.

Version 5: Faster Locking.



8. Inlining Basics.

What Is Inlining?

Method Invocation Costs.

Why Inline?

Inlining Details.

Inlining Virtual Methods.

Performance Gains from Inlining.



9. Inlining—Performance Considerations.

Cross-Call Optimization.

Why Not Inline?

Development and Compile-Time Inlining

Considerations. Profile-Based

Inlining.

Inlining Rules.

Singletons.

Trivials.



10. Inlining Tricks.

Conditional Inlining.

Selective Inlining.

Recursive Inlining.

Inlining with Static Local Variables.

Architectural Caveat: Multiple Register Sets.



11. Standard Template Library.

Asymptotic Complexity.

Insertion.

Deletion.

Traversal.

Find.

Function Objects.

Better than STL?



12. Reference Counting

Implementation Details.

Preexisting Classes.

Concurrent Reference Counting.



13. Coding Optimizations.

Caching.

Precompute.

Reduce Flexibility.

80-20 Rule: Speed Up the Common Path.

Lazy Evaluation.

Useless Computations.

System Architecture.

Memory Management.

Library and System Calls.

Compiler Optimization.



14. Design Optimizations.

Design Flexibility.

Caching.

Web Server Timestamps.

Data Expansion.

The Common Code Trap.

Efficient Data Structures.

Lazy Evaluation.

getpeername().

Useless Computations.

Obsolete Code.



15. Scalability.

The SMP Architecture.

Amdahl’s Law.

Multithreaded and Synchronization Terminology.

Break Up a Task into Multiple Subtasks.

Cache Shared Data.

Share Nothing.

Partial Sharing.

Lock Granularity.

False Sharing.

Thundering Herd.

Reader/Writer Locks.



16. System Architecture Dependencies

Memory Hierarchies.

Registers: Kings of Memory.

Disk and Memory Structures.

Cache Effects.

Cache Thrash.

Avoid Branching.

Prefer Simple Calculations to Small Branches.

Threading Effects.

Context Switching.

Kernel Crossing.

Threading Choices.



Bibliography.


Index. 0201379503T04062001

Preface

If you conducted an informal survey of software developers on the issue of C++ performance, you would undoubtedly find that the vast majority of them view performance issues as the Achilles' heel of an otherwise fine language. We have heard it repeatedly ever since C++ burst on the corporate scene: C++ is a poor choice for implementing performance-critical applications. In the mind of developers, this particular application domain was ruled by plain C and, occasionally, even assembly language.

As part of that software community we had the opportunity to watch that myth develop and gather steam. Years ago, we participated in the wave that embraced C++ with enthusiasm. All around us, many development projects plunged in headfirst. Some time later, software solutions implemented in C++ began rolling out. Their performance was typically less than optimal, to put it gently. Enthusiasm over C++ in performance-critical domains has cooled. We were in the business of supplying networking software whose execution speed was not up for negotiation--speed was top priority. Since networking software is pretty low on the software food-chain, its performance is crucial. Large numbers of applications were going to sit on top of it and depend on it. Poor performance in the low levels ripples all the way up to higher level applications.

Our experience was not unique. All around, early adopters of C++ had difficulties with the resulting performance of their C++ code. Instead of attributing the difficulties to the steep learning curve of the new object-oriented software development paradigm, we blamed it on C++, the dominant language for the expression of the paradigm. Even though C++ compilers were still essentially in their infancy, the language was branded as inherently slow. This belief spread quickly and is now widely accepted as fact. Software organizations that passed on C++ frequently pointed to performance as their key concern. That concern was rooted in the perception that C++ cannot match the performance delivered by its C counterpart. Consequently, C++ has had little success penetrating software domains that view performance as top priority: operating system kernels, device drivers, networking systems (routers, gateways, protocol stacks), and more.

We have spent years dissecting large systems of C and C++ code trying to squeeze every ounce of performance out of them. It is through our experience of slugging it out in the trenches that we have come to appreciate the potential of C++ to produce highly efficient programs. We've seen it done in practice. This book is our attempt to share that experience and document the many lessons we have learned in our own pursuit of C++ efficiency. Writing efficient C++ is not trivial, nor is it rocket science. It takes the understanding of some performance principles, as well as information on C++ performance traps and pitfalls.

The 80-20 rule is an important principle in the world of software construction. We adopt it in the writing of this book as well: 20% of all performance bugs will show up 80% of the time. We therefore chose to concentrate our efforts where it counts the most. We are interested in those performance issues that arise frequently in industrial code and have significant impact. This book is not an exhaustive discussion of the set of all possible performance bugs and their solutions; hence, we will not cover what we consider esoteric and rare performance pitfalls.

Our point of view is undoubtedly biased by our practical experience as programmers of server-side, performance-critical communications software. This bias impacts the book in several ways:

  • The profile of performance issues that we encounter in practice may be slightly different in nature than those found in scientific computing, database applications, and other domains. That's not a problem. Generic performance principles transcend distinct domains, and apply equally well in domains other than networking software.
  • At times, we invented contrived examples to drive a point home, although we tried to minimize this. We have made enough coding mistakes in the past to have a sizable collection of samples taken from real production-level code that we have worked on. Our expertise was earned the hard way--by learning from our own mistakes as well as those of our colleagues. As much as possible, we illustrated our points with real code samples.
  • We do not delve into the asymptotic complexity of algorithms, data structures, and the latest and greatest techniques for accessing, sorting, searching, and compressing data. These are important topics, but they have been extensively covered elsewhere Knu73, BR95, KP74. Instead, we focus on simple, practical, everyday coding and design principles that yield large performance improvements. We point out common design and coding practices that lead to poor performance, whether it be through the unwitting use of language features that carry high hidden costs or through violating any number of subtle (and not so subtle) performance principles.

So how do we separate myth from reality? Is C++ performance truly inferior to that of C? It is our contention that the common perception of inferior C++ performance is invalid. We concede that in general, when comparing a C program to a C++ version of what appears to be the same thing, the C program is generally faster. However, we also claim that the apparent similarity of the two programs typically is based on their data handling functionality, not their correctness, robustness, or ease of maintenance. Our contention is that when C programs are brought up to the level of C++ programs in these regards, the speed differences disappear, or the C++ versions are faster.

Thus C++ is inherently neither slower nor faster. It could be either, depending on how it is used and what is required from it. It's the way it is used that matters: If used properly, C++ can yield software systems exhibiting not just acceptable performance, but yield superior software performance.

We would like to thank the many people who contributed to this work. The toughest part was getting started and it was our editor, Marina Lang, who was instrumental in getting this project off the ground. Julia Sime made a significant contribution to the early draft and Yomtov Meged contributed many valuable suggestions as well. He also was the one who pointed out to us the subtle difference between our opinions and the absolute truth. Although those two notions may coincide at times, they are still distinct.

Many thanks to the reviewers hired by Addison-Wesley; their feedback was extremely valuable.

Thanks also to our friends and colleagues who reviewed portions of the manuscript. They are, in no particular order, Cyndy Ross, Art Francis, Scott Snyder, Tricia York, Michael Fraenkel, Carol Jones, Heather Kreger, Kathryn Britton, Ruth Willenborg, David Wisler, Bala Rajaraman, Don "Spike" Washburn, and Nils Brubaker.

Last but not least, we would like to thank our wives, Cynthia Powers Bulka and Ruth Washington Mayhew.



0201379503P04062001

 
Buy

Book  $39.99  $31.99

Usually ships in 24 hours.

This book includes free shipping!

Purchase Reward: One Month Free Subscription
By completing any purchase on InformIT, you become eligible for an unlimited access one-month subscription to Safari Books Online.

Get access to thousands of books and training videos about technology, professional development and digital media from more than 40 leading publishers, including Addison-Wesley, Prentice Hall, Cisco Press, IBM Press, O'Reilly Media, Wrox, Apress, and many more. If you continue your subscription after your 30-day trial, you can receive 30% off a monthly subscription to the Safari Library for up to 12 months. That's a total savings of $199.