The Art of Writing Efficient Programs

Book description

Become a better programmer with performance improvement techniques such as concurrency, lock-free programming, atomic operations, parallelism, and memory management

Key Features

  • Learn proven techniques from a heavyweight and recognized expert in C++ and high-performance computing
  • Understand the limitations of modern CPUs and their performance impact
  • Find out how you can avoid writing inefficient code and get the best optimizations from the compiler
  • Learn the tradeoffs and costs of writing high-performance programs

Book Description

The great free lunch of "performance taking care of itself" is over. Until recently, programs got faster by themselves as CPUs were upgraded, but that doesn't happen anymore. The clock frequency of new processors has almost peaked, and while new architectures provide small improvements to existing programs, this only helps slightly. To write efficient software, you now have to know how to program by making good use of the available computing resources, and this book will teach you how to do that.

The Art of Efficient Programming covers all the major aspects of writing efficient programs, such as using CPU resources and memory efficiently, avoiding unnecessary computations, measuring performance, and how to put concurrency and multithreading to good use. You'll also learn about compiler optimizations and how to use the programming language (C++) more efficiently. Finally, you'll understand how design decisions impact performance.

By the end of this book, you'll not only have enough knowledge of processors and compilers to write efficient programs, but you'll also be able to understand which techniques to use and what to measure while improving performance. At its core, this book is about learning how to learn.

What you will learn

  • Discover how to use the hardware computing resources in your programs effectively
  • Understand the relationship between memory order and memory barriers
  • Familiarize yourself with the performance implications of different data structures and organizations
  • Assess the performance impact of concurrent memory accessed and how to minimize it
  • Discover when to use and when not to use lock-free programming techniques
  • Explore different ways to improve the effectiveness of compiler optimizations
  • Design APIs for concurrent data structures and high-performance data structures to avoid inefficiencies

Who this book is for

This book is for experienced developers and programmers who work on performance-critical projects and want to learn new techniques to improve the performance of their code. Programmers in algorithmic trading, gaming, bioinformatics, computational genomics, or computational fluid dynamics communities will get the most out of the examples in this book, but the techniques are fairly universal. Although this book uses the C++ language, the concepts demonstrated in the book can be easily transferred or applied to other compiled languages such as C, Java, Rust, Go, and more.

Table of contents

  1. The Art of Writing Efficient Programs
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who is this book for?
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Section 1 – Performance Fundamentals
  7. Chapter 1: Introduction to Performance and Concurrency
    1. Why focus on performance?
    2. Why performance matters
    3. What is performance?
      1. Performance as throughput
      2. Performance as power consumption
      3. Performance for real-time applications
      4. Performance as dependent on context
    4. Evaluating, estimating, and predicting performance
    5. Learning about high performance
    6. Summary
    7. Questions
  8. Chapter 2: Performance Measurements
    1. Technical requirements
    2. Performance measurements by example
    3. Performance benchmarking
      1. C++ chrono timers
      2. High-resolution timers
    4. Performance profiling
      1. The perf profiler
      2. Detailed profiling with perf
      3. The Google Performance profiler
      4. Profiling with call graphs
      5. Optimization and inlining
      6. Practical profiling
    5. Micro-benchmarking
      1. Basics of micro-benchmarking
      2. Micro-benchmarking and compiler optimizations
      3. Google Benchmark
      4. Micro-benchmarks are lies
    6. Summary
    7. Questions
  9. Chapter 3: CPU Architecture, Resources, and Performance
    1. Technical requirements
    2. The performance begins with the CPU
    3. Probing performance with micro-benchmarks
      1. Visualizing instruction-level parallelism
    4. Data dependencies and pipelining
    5. Pipelining and branches
      1. Branch prediction
      2. Profiling for branch mispredictions
    6. Speculative execution
    7. Optimization of complex conditions
    8. Branchless computing
      1. Loop unrolling
      2. Branchless selection
      3. Branchless computing examples
    9. Summary
    10. Questions
  10. Chapter 4: Memory Architecture and Performance
    1. Technical requirements
    2. The performance begins with the CPU but does not end there
    3. Measuring memory access speed
      1. Memory architecture
      2. Measuring memory and cache speeds
    4. The speed of memory: the numbers
      1. The speed of random memory access
      2. The speed of sequential memory access
      3. Memory performance optimizations in hardware
    5. Optimizing memory performance
      1. Memory-efficient data structures
      2. Profiling memory performance
      3. Optimizing algorithms for memory performance
    6. The ghost in the machine
      1. What is Spectre?
      2. Spectre by example
      3. Spectre, unleashed
    7. Summary
    8. Questions
  11. Chapter 5: Threads, Memory, and Concurrency
    1. Technical requirements
    2. Understanding threads and concurrency
      1. What is a thread?
      2. Symmetric multi-threading
      3. Threads and memory
      4. Memory-bound programs and concurrency
    3. Understanding the cost of memory synchronization
    4. Why data sharing is expensive
    5. Learning about concurrency and order
      1. The need for order
      2. Memory order and memory barriers
      3. Memory order in C++
    6. Memory model
    7. Summary
    8. Questions
  12. Section 2 – Advanced Concurrency
  13. Chapter 6: Concurrency and Performance
    1. Technical requirements
    2. What is needed to use concurrency effectively?
    3. Locks, alternatives, and their performance
      1. Lock-based, lock-free, and wait-free programs
      2. Different locks for different problems
      3. Lock-based versus lock-free, what is the real difference?
    4. Building blocks for concurrent programming
      1. The basics of concurrent data structures
      2. Counters and accumulators
      3. Publishing protocol
      4. Smart pointers for concurrent programming
    5. Summary
    6. Questions
  14. Chapter 7: Data Structures for Concurrency
    1. Technical requirements
    2. What is a thread-safe data structure?
      1. The best kind of thread safety
      2. The real thread safety
    3. The thread-safe stack
      1. Interface design for thread safety
      2. Performance of mutex-guarded data structures
      3. Performance requirements for different uses
      4. Stack performance in detail
      5. Performance estimates for synchronization schemes
      6. Lock-free stack
    4. The thread-safe queue
      1. Lock-free queue
      2. Non-sequentially consistent data structures
      3. Memory management for concurrent data structures
    5. The thread-safe list
      1. Lock-free list
    6. Summary
    7. Questions
  15. Chapter 8: Concurrency in C++
    1. Technical requirements
    2. Concurrency support in C++11
    3. Concurrency support in C++17
    4. Concurrency support in C++20
      1. The foundations of coroutines
      2. Coroutine C++ syntax
      3. Coroutine examples
    5. Summary
    6. Questions
  16. Section 3 – Designing and Coding High-Performance Programs
  17. Chapter 9: High-Performance C++
    1. Technical requirements
    2. What is the efficiency of a programming language?
    3. Unnecessary copying
      1. Copying and argument passing
      2. Copying as an implementation technique
      3. Copying to store data
      4. Copying of return values
      5. Using pointers to avoid copying
      6. How to avoid unnecessary copying
    4. Inefficient memory management
      1. Unnecessary memory allocations
      2. Memory management in concurrent programs
      3. Avoiding memory fragmentation
    5. Optimization of conditional execution
    6. Summary
    7. Questions
  18. Chapter 10: Compiler Optimizations in C++
    1. Technical requirements
    2. Compilers optimizing code
      1. Basics of compiler optimizations
      2. Function inlining
      3. What does the compiler really know?
      4. Lifting knowledge from runtime to compile time
    3. Summary
    4. Questions
  19. Chapter 11: Undefined Behavior and Performance
    1. Technical requirements
    2. What is undefined behavior?
    3. Why have undefined behavior?
    4. Undefined behavior and C++ optimization
    5. Using undefined behavior for efficient design
    6. Summary
    7. Questions
  20. Chapter 12: Design for Performance
    1. Technical requirements
    2. Interaction between the design and performance
    3. Design for performance
      1. The minimum information principle
      2. The maximum information principle
    4. API design considerations
      1. API design for concurrency
      2. Copying and sending data
    5. Design for optimal data access
    6. Performance trade-offs
      1. Interface design
      2. Component design
      3. Errors and undefined behavior
    7. Making informed design decisions
    8. Summary
    9. Questions
  21. Assessments
    1. Chapter 1:
    2. Chapter 2:
    3. Chapter 3:
    4. Chapter 4:
    5. Chapter 5:
    6. Chapter 6:
    7. Chapter 7:
    8. Chapter 8:
    9. Chapter 9:
    10. Chapter 10:
    11. Chapter 11:
    12. Chapter 12:
    13. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: The Art of Writing Efficient Programs
  • Author(s): Fedor G. Pikus
  • Release date: October 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800208117