Optimizing with Modern C++ is a two-day onsite training course with programming examples, taught by Patrice Roy. It is offered at the Gaylord Rockies from 09:00 to 17:00 Aurora time (MDT) on Saturday and Sunday, September 14th and 15th, 2024 (immediately prior to the conference). Lunch is included.
Course Description
C++ is seen by many as a language one uses to get more from a program: more speed, more deterministic behavior, lower resource consumption, etc. Interestingly, C++ does not necessarily make programs faster or smaller; what C++ brings to programmers is control, and that control can lead to all sorts of optimizations.
Optimization is a vast and multifaceted topic no-one could hope cover completely in the span of a 15-hours course. For this reason, this course focuses on how to leverage “modern” C++ (C++ as we’ve come to know and love it since the C++11 revolution came upon us all) to make programs faster, smaller and otherwise more efficient.
This course will bring participants to:
- Identify those locations in a program that could benefit from optimizations, including gathering information from profiling tools
- Perform fecund optimizations of a function or of the entire program: reducing memory consumption, increase average execution speed, reducing latency on some key operations, increasing compilation speed, etc. This will include a look at programming practices that make code better even though they are not optimizations per se
- Benefit from compiler flags to add to the optimizations resulting from our efforts on the source code itself
Topics covered in this course include:
- Fundamentals (approx. 60 minutes)
- What do we mean by “optimization”?
- Optimizing for what / towards what end?
- Average execution speed
- Worst-case execution speed
- Smallest average deviation
- Reducing latency when some event occurs
- Memory consumption
- Layout of objects in memory
- Understanding the interdependencies between these aspects
- Impact of optimizing one aspect on the others
- Measuring (before and after)
- Detecting regressions
- How we will measure execution speed
- How we will estimate memory consumption for one or many objects
- This is a delicate topic
- Exercises
Note: this first section will be limited to a broad overview of the topics covered therein.
- Profiling and identification of “hot spots”
- This section will emphasize identifying those locations in a program where optimization could bring benefits. We will use two approaches:
- Analytical approach, reading code
- Identifying locations where the need for optimization is evident
- Examining examples where the need for specific gestures is a lot less evident
- Examining examples where optimizations would lead to local or systemic performance regressions
- Profiling using tools
- The goal here is to make it clear that investing a lot of effort at the wrong place in a program can be an inefficient use of our time
- Analytical approach, reading code
- Exercises (integrated throughout the section)
- This section will emphasize identifying those locations in a program where optimization could bring benefits. We will use two approaches:
Note: the topics in this section will be reinvested and integrated throughout the rest of this training.
- Leveraging modern C++: techniques and language features
- Impact of standard algorithms and lambda expressions
- These have been called “negative cost abstractions” in the sense that, if well used, they make programs that use them faster than programs that do not
- Impact of object size and layout on program size and execution speed. In particular, we will discuss:
- Alignment
- The empty base optimization and the [[no_unique_address]] attribute
- The impact of AoS (array of structs) and SoA (struct of arrays) representations
- True sharing and false sharing
- Bit fields
- Classical object-oriented techniques and modern alternatives
- Generic approaches (templates, traits, concepts)
- Static polymorphism
- Using std::variant
- Leveraging the type system to move computation from run-time to compile-time
- Compile-time evaluation
- If constexpr
- constexpr
- consteval
- constinit
- Argument passing and return value-related topics
- Move semantics
- Return value optimization (named or not)
- Perfect forwarding
- Memory management
- Smart pointers (and using them wisely)
- Overloading operators new and delete
- Allocators (pre-C++ 11, since C++ 11, PMR)
- Impact of standard algorithms and lambda expressions
Note: given the size of C++, this section will not apply a topic-per-topic approach, preferring exercises that lead to an understanding of how several features and techniques can work together to make programs more efficient (what “efficient” means will depend on the examples and exercises).
Prerequisites
The target audience is junior to intermediate-level developers who:
- Are familiar with modern C++, meaning C++11 and more in this case, without necessarily being experts
- Seek to make better use of today’s C++ to make programs faster and less resource-consuming
- Want the programs they write to be at once faster, safer and more correct
Since this class will insist on problem solving and reflection based on concrete cases, even developers coming to C++ from another language might find the course surprisingly instructive: indeed, some parts of analyzing source code and investigating performance problems are eminently transferable to other languages, although this course’s main goal is to leverage the strengths of C++. More advanced developers might also enjoy this course if looking for a greater familiarity with modern C++ and its usefulness in their daily tasks.
This course will concentrate on standard C++ and avoid most compiler-specific and operating system-specific techniques and tools. We will however spend a little time on optimizations resulting from adequate use of compiler flags, which will lead us momentarily out of our preferred “portable code zone”. Also note that the course will occasionally address concurrency issues and will use templates quite frequently; as such, familiarity with concurrent and generic programming, while not necessary, will help participants benefit more from the experience.
Register Here
Course Instructor
Patrice Roy has been playing with C++ either professionally, for pleasure or (most of the time) both for over 30 years. After a few years doing R&D and working on military flight simulators, he has moved on to academics and has been teaching computer science since 1998. Since 2005, he has been involved more specifically in helping graduate students and professionals from the fields of real-time systems and game programming develop the skills they need to face today’s challenges.
Patrice been a participating member in the ISO C++ Standards Committee since late 2014 and has been involved with the ISO Programming Language Vulnerabilities working group since late 2015. He has five kids, and his wife ensures their house is home to a continuously changing numbers of cats, dogs and other animals.