C++ High Performance - Second Edition

Book description

A comprehensive guide to help aspiring and professional C++ developers elevate the performance of their apps by allowing them to run faster and consume fewer resources. Purchase of the print or Kindle book includes a free eBook in PDF format.

Key Features

  • Updated to C++20 with completely revised code and more content on error handling, benchmarking, memory allocators, and concurrent programming
  • Explore the latest C++20 features including concepts, ranges, and coroutines
  • Utilize C++ constructs and techniques to carry out effective data structure optimization and memory management

Book Description

C++ High Performance, Second Edition guides you through optimizing the performance of your C++ apps. This allows them to run faster and consume fewer resources on the device they're running on without compromising the readability of your codebase.

The book begins by introducing the C++ language and some of its modern concepts in brief. Once you are familiar with the fundamentals, you will be ready to measure, identify, and eradicate bottlenecks in your C++ codebase. By following this process, you will gradually improve your style of writing code. The book then explores data structure optimization, memory management, and how it can be used efficiently concerning CPU caches.

After laying the foundation, the book trains you to leverage algorithms, ranges, and containers from the standard library to achieve faster execution, write readable code, and use customized iterators. It provides hands-on examples of C++ metaprogramming, coroutines, reflection to reduce boilerplate code, proxy objects to perform optimizations under the hood, concurrent programming, and lock-free data structures. The book concludes with an overview of parallel algorithms.

By the end of this book, you will have the ability to use every tool as needed to boost the efficiency of your C++ projects.

What you will learn

  • Write specialized data structures for performance-critical code
  • Use modern metaprogramming techniques to reduce runtime calculations
  • Achieve efficient memory management using custom memory allocators
  • Reduce boilerplate code using reflection techniques
  • Reap the benefits of lock-free concurrent programming
  • Gain insights into subtle optimizations used by standard library algorithms
  • Compose algorithms using ranges library
  • Develop the ability to apply metaprogramming aspects such as constexpr, constraints, and concepts
  • Implement lazy generators and asynchronous tasks using C++20 coroutines

Who this book is for

If you're a C++ developer looking to improve the efficiency of your code or just keen to upgrade your skills to the next level, this book is for you.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. Get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  2. A Brief Introduction to C++
    1. Why C++?
      1. Zero-cost abstractions
        1. Programming languages and machine code abstractions
        2. Abstractions in other languages
        3. The zero-overhead principle
      2. Portability
      3. Robustness
      4. C++ of today
    2. C++ compared with other languages
      1. Competing languages and performance
      2. Non-performance-related C++ language features
        1. Value semantics
        2. Const correctness
        3. Object ownership
        4. Deterministic destruction in C++
        5. Avoiding null objects using C++ references
      3. Drawbacks of C++
    3. Libraries and compilers used in this book
    4. Summary
  3. Essential C++ Techniques
    1. Automatic type deduction with the auto keyword
      1. Using auto in function signatures
        1. Forwarding the return type using decltype(auto)
      2. Using auto for variables
        1. A const reference
        2. A mutable reference
        3. A forwarding reference
        4. Practices for ease of use
      3. Const propagation for pointers
    2. Move semantics explained
      1. Copy-construction, swap, and move
        1. Copy-constructing an object
      2. Resource acquisition and the rule of five
      3. Named variables and rvalues
      4. Default move semantics and the rule of zero
        1. Rule of zero in a real code base
        2. A common pitfall – moving non-resources
      5. Applying the && modifier to class member functions
      6. Don't move when copies are elided anyway
      7. Pass by value when applicable
        1. Cases where pass-by-value is not applicable
        2. Moving constructor parameters
    3. Designing interfaces with error handling
      1. Contracts
        1. Class invariants
        2. Maintaining contracts
      2. Error handling
        1. Programming error or runtime error?
        2. Programming errors (bugs)
        3. Recoverable runtime errors
    4. Function objects and lambda expressions
      1. The basic syntax of a C++ lambda
      2. The capture clause
        1. Capture by reference versus capture by value
        2. Similarities between a lambda and a class
        3. Initializing variables in capture
        4. Mutating lambda member variables
        5. Capture all
      3. Assigning C function pointers to lambdas
      4. Lambda types
      5. Lambdas and std::function
        1. Implementing a simple Button class with std::function
        2. Performance consideration of std::function
      6. Generic lambdas
    5. Summary
  4. Analyzing and Measuring Performance
    1. Asymptotic complexity and big O notation
      1. Growth rates
      2. Amortized time complexity
    2. What to measure and how?
      1. Performance properties
      2. Speedup of execution time
      3. Performance counters
      4. Performance testing — best practices
    3. Knowing your code and hot spots
      1. Instrumentation profilers
      2. Sampling profilers
    4. Microbenchmarking
      1. Amdahl's law
      2. Pitfalls of microbenchmarking
      3. A microbenchmark example
    5. Summary
  5. Data Structures
    1. The properties of computer memory
    2. The standard library containers
      1. Sequence containers
        1. Vectors and arrays
        2. Deque
        3. List and forward_list
        4. The basic_string
      2. Associative containers
        1. Ordered sets and maps
        2. Unordered sets and maps
      3. Container adaptors
        1. Priority queues
    3. Using views
      1. Avoiding copies with string_view
        1. Eliminating array decay with std::span
    4. Some performance considerations
      1. Balancing between complexity guarantees and overhead
      2. Knowing and using the appropriate API functions
    5. Parallel arrays
    6. Summary
  6. Algorithms
    1. Introducing the standard library algorithms
      1. Evolution of the standard library algorithms
      2. Solving everyday problems
        1. Iterating over a sequence
        2. Generating elements
        3. Sorting elements
        4. Finding elements
        5. Finding using binary search
        6. Testing for certain conditions
        7. Counting elements
        8. Minimum, maximum, and clamping
    2. Iterators and ranges
      1. Introducing iterators
      2. Sentinel values and past-the-end iterators
      3. Ranges
      4. Iterator categories
    3. Features of the standard algorithms
      1. Algorithms do not change the size of the container
      2. Algorithms with output require allocated data
      3. Algorithms use operator==() and operator<() by default
        1. Custom comparator functions
      4. Constrained algorithms use projections
      5. Algorithms require move operators not to throw
      6. Algorithms have complexity guarantees
      7. Algorithms perform just as well as C library function equivalents
    4. Writing and using generic algorithms
      1. Non-generic algorithms
      2. Generic algorithms
      3. Data structures that can be used by generic algorithms
    5. Best practices
      1. Using the constrained algorithms
      2. Sorting only for the data you need to retrieve
        1. Use cases
        2. Performance evaluation
      3. Use standard algorithms over raw for-loops
        1. Example 1: Readability issues and mutable variables
        2. Example 2: Unfortunate exceptions and performance problems
        3. Example 3: Exploiting the standard library optimizations
      4. Avoiding container copies
    6. Summary
  7. Ranges and Views
    1. The motivation for the Ranges library
      1. Limitations of the Algorithm library
    2. Understanding views from the Ranges library
      1. Views are composable
      2. Range views come with range adaptors
      3. Views are non-owning ranges with complexity guarantees
      4. Views don't mutate the underlying container
      5. Views can be materialized into containers
      6. Views are lazy evaluated
    3. Views in the standard library
      1. Range views
        1. Generating views
        2. Transforming views
        3. Sampling views
        4. Utility views
      2. Revisiting std::string_view and std::span
    4. The future of the Ranges library
    5. Summary
  8. Memory Management
    1. Computer memory
      1. The virtual address space
      2. Memory pages
      3. Thrashing
    2. Process memory
      1. Stack memory
      2. Heap memory
    3. Objects in memory
      1. Creating and deleting objects
        1. Placement new
        2. The new and delete operators
      2. Memory alignment
      3. Padding
    4. Memory ownership
      1. Handling resources implicitly
      2. Containers
      3. Smart pointers
        1. Unique pointer
        2. Shared pointer
        3. Weak pointer
    5. Small object optimization
    6. Custom memory management
      1. Building an arena
      2. A custom memory allocator
      3. Using polymorphic memory allocators
      4. Implementing a custom memory resource
    7. Summary
  9. Compile-Time Programming
    1. Introduction to template metaprogramming
      1. Creating templates
      2. Using integers as template parameters
      3. Providing specializations of a template
      4. How the compiler handles a template function
      5. Abbreviated function templates
      6. Receiving the type of a variable with decltype
    2. Type traits
      1. Type trait categories
      2. Using type traits
    3. Programming with constant expressions
      1. Constexpr functions in a runtime context
      2. Declaring immediate functions using consteval
      3. The if constexpr statement
        1. Comparison with runtime polymorphism
        2. Example of the generic modulus function using if constexpr
      4. Checking programming errors at compile time
        1. Using assert to trigger errors at runtime
        2. Using static_assert to trigger errors at compile time
    4. Constraints and concepts
      1. An unconstrained version of a Point2D template
        1. Generic interfaces and bad error messages
      2. A syntactic overview of constraints and concepts
        1. Defining new concepts
        2. Constraining types with concepts
        3. Function overloading
      3. A constrained version of the Point2D template
      4. Adding constraints to your code
      5. Concepts in the standard library
    5. Real-world examples of metaprogramming
      1. Example 1: creating a generic safe cast function
      2. Example 2: hash strings at compile time
        1. The advantages of the compile-time hash sum calculation
        2. Implementing and verifying a compile-time hash function
        3. Constructing a PrehashedString class
        4. Evaluating PrehashedString
        5. Evaluating get_bitmap_resource() with PrehashedString
    6. Summary
  10. Essential Utilities
    1. Representing optional values with std::optional
      1. Optional return values
      2. Optional member variables
      3. Avoiding empty states in enums
      4. Sorting and comparing std::optional
    2. Fixed size heterogenous collections
      1. Using std::pair
      2. The std::tuple
        1. Accessing the members of a tuple
        2. Iterating std::tuple members
        3. Unrolling the tuple
        4. Accessing tuple elements
        5. The variadic template parameter pack
    3. Dynamically sized heterogenous collections
      1. The std::variant
        1. Exception safety of std::variant
        2. Visiting variants
      2. Heterogenous collections using variant
      3. Accessing the values in our variant container
        1. Global function std::get()
    4. Some real-world examples
      1. Example 1: projections and comparison operators
      2. Example 2: reflection
        1. Making a class reflect its members
        2. C++ libraries that simplify reflection
        3. Using reflection
        4. Conditionally overloading global functions
        5. Testing reflection capabilities
    5. Summary
  11. Proxy Objects and Lazy Evaluation
    1. Introducing lazy evaluation and proxy objects
      1. Lazy versus eager evaluation
      2. Proxy objects
    2. Avoiding constructing objects using proxy objects
      1. Comparing concatenated strings using a proxy
      2. Implementing the proxy
      3. The rvalue modifier
      4. Assigning a concatenated proxy
      5. Performance evaluation
    3. Postponing sqrt computations
      1. A simple two-dimensional vector class
      2. The underlying mathematics
      3. Implementing the LengthProxy object
      4. Comparing lengths with LengthProxy
      5. Calculating length with LengthProxy
        1. Preventing the misuse of LengthProxy
      6. Performance evaluation
    4. Creative operator overloading and proxy objects
      1. The pipe operator as an extension method
        1. The pipe operator
    5. Summary
  12. Concurrency
    1. Understanding the basics of concurrency
    2. What makes concurrent programming hard?
    3. Concurrency and parallelism
      1. Time slicing
      2. Shared memory
      3. Data races
        1. Example: A data race
        2. Avoiding data races
      4. Mutex
      5. Deadlock
      6. Synchronous and asynchronous tasks
    4. Concurrent programming in C++
      1. The thread support library
        1. Threads
        2. Thread states
        3. Joinable thread
        4. Protecting critical sections
        5. Avoiding deadlocks
        6. Condition variables
        7. Returning data and handling errors
        8. Tasks
      2. Additional synchronization primitives in C++20
        1. Using latches
        2. Using barriers
        3. Signalling and resource counting using semaphores
      3. Atomic support in C++
        1. The lock-free property
        2. Atomic flags
        3. Atomic wait and notify
        4. Using shared_ptr in a multithreaded environment
        5. Atomic references
      4. The C++ memory model
        1. Instruction reordering
        2. Atomics and memory orders
    5. Lock-free programming
      1. Example: A lock-free queue
    6. Performance guidelines
      1. Avoid contention
      2. Avoid blocking operations
      3. Number of threads/CPU cores
      4. Thread priorities
      5. Thread affinity
      6. False sharing
    7. Summary
  13. Coroutines and Lazy Generators
    1. A few motivating examples
    2. The coroutine abstraction
      1. Subroutines and coroutines
      2. Executing subroutines and coroutines on the CPU
        1. CPU registers, instructions, and the stack
        2. Call and return
        3. Suspend and resume
      3. Stackless versus stackful coroutines
        1. Performance cost
        2. Memory footprint
        3. Context switching
      4. What you have learned so far
    3. Coroutines in C++
      1. What's included in standard C++ (and what's not)?
      2. What makes a C++ function a coroutine?
      3. A minimal but complete example
        1. The coroutine return object
        2. The promise type
        3. Awaitable types
        4. Passing our coroutine around
      4. Allocating the coroutine state
      5. Avoiding dangling references
        1. Passing parameters to coroutines
        2. Member functions that are coroutines
        3. Lambdas that are coroutines
        4. Guidelines to prevent dangling references
      6. Handling errors
      7. Customization points
    4. Generators
      1. Implementing a generator
      2. Using the Generator class
        1. Solving generator problems
        2. An iterator implementation
        3. A solution using the Ranges library
        4. Conclusion
      3. A real-world example using generators
        1. The problem
        2. Delta encoding
    5. Performance
    6. Summary
  14. Asynchronous Programming with Coroutines
    1. Awaitable types revisited
      1. The implicit suspend points
    2. Implementing a rudimentary task type
      1. Handling return values and exceptions
      2. Resuming an awaiting coroutine
      3. Supporting void tasks
      4. Synchronously waiting for a task to complete
        1. Implementing sync_wait()
        2. Implementing SyncWaitTask
      5. Testing asynchronous tasks with sync_wait()
    3. Wrapping a callback-based API
    4. A concurrent server using Boost.Asio
      1. Implementing the server
      2. Running and connecting to the server
      3. What we have achieved with the server (and what we haven't)
    5. Summary
  15. Parallel Algorithms
    1. The importance of parallelism
    2. Parallel algorithms
      1. Evaluating parallel algorithms
      2. Amdahl's law revisited
      3. Implementing parallel std::transform()
        1. Naive implementation
        2. Divide and conquer
      4. Implementing parallel std::count_if()
      5. Implementing parallel std::copy_if()
        1. Approach 1: Use a synchronized write position
        2. Approach 2: Split the algorithm into two parts
        3. Performance evaluation
    3. Parallel standard library algorithms
      1. Execution policies
        1. Sequenced policy
        2. Parallel policy
        3. Unsequenced policy
        4. Parallel unsequenced policy
      2. Exception handling
      3. Additions and changes to parallel algorithms
        1. std::accumulate() and std::reduce()
        2. std::for_each()
      4. Parallelizing an index-based for-loop
        1. Combining std::for_each() with std::views::iota()
        2. Simplifying construction via a wrapper
    4. Executing algorithms on the GPU
      1. Summary
  16. Other Books You May Enjoy
  17. Index

Product information

  • Title: C++ High Performance - Second Edition
  • Author(s): Björn Andrist, Viktor Sehr
  • Release date: December 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781839216541