Using C++ for Low-Latency Systems

This class was taught in 2016 and is online for historical purposes only.

“Using C++ for Low-Latency Systems” is a two day training course taught by Patrice Roy of Sherbrooke University. It is offered at the Meydenbauer from 9AM to 5PM on Saturday and Sunday, September 17th and 18th (immediately prior to the conference). Lunch is included.

Course Description

To get the kind of performance required by some of the most demanding applications, such as games and high-frequency trading, requires the mastery of low-latency techniques not found in mainstream C++.

This class will give you the skills necessary to create low-latency systems in C++:

  • How to develop components for systems with low-latency constraints
  • How to develop systems that have predictable performance, particularly where worst case execution time comes into play
  • How to perform computation at compile-time instead of at runtime when this is an option
  • How to reduce the space overhead of your programs
  • How to make memory management more efficient, in many ways
  • How to get the most out of cache memory
  • How to get the most out of smart pointers
  • How to measure the performance of your programs in a portable manner

Emphasis will be put on portable code and will use C++11/C++14 features. Examples will be backed by measurement and open discussion is encouraged.

Prerequisites

Participants should have intermediate knowledge of the C++ language; emphasis will be put on robust C++ code with predictable — and fast — runtime behavior.

This course includes exercises, so bring your laptop and your favorite C++11/14 development environment.

Course Topics

This course will focus on techniques programmers can use to get better or more stable performance from systems with low-latency constraints. It is subdivided in sections that cover selected subsets of this important topic. Each section discusses C++ features that help achieving the section’s objectives, with emphasis on C++ 11/14 features wherever that makes sense.

  • C++ for Low-Latency Systems

We define what we mean by “low-latency”; discuss the similarities and differences with real-time systems; introduce worst-case execution time concepts and discuss measurement utilities and techniques. Resilience-oriented aspects of C++ programming will be addressed here, but this will be a recurring theme throughout course.
C++ features used will include static_assert, utilities, variadic templates and automatic type deduction mechanisms.

  • Key Low/No Overhead C++ Features

Some C++ features have strong upsides and little to no downsides for low-latency system development. In this section of the course, we will explore and use some of the most relevant ones.
C++ features used will include unique_ptr and make_unique, constexpr, automatic type deduction mechanisms, type traits, enable_if, interesting uses of the using keyword, and move semantics.

  • C++ Features to Use with Care in Low-Latency Systems

There are C++ features that can be used for low-latency systems but with particular care only, as they have costs that could be higher than their benefits. We will take a reasoned, measurement-based approach to build an informed judgement.
C++ features used will include shared_ptr and make_shared, exceptions, multiple inheritance, RTTI and used-defined replacements thereof. We will also discuss the role noexcept can play in such systems.

  • Leveraging Standard Containers and Algorithms in Low-Latency Systems

Some companies rewrite some or all of the standards containers due to a belief that their needs cannot be satisfied by the tools this library offers. We will explore ways to get the most out of the containers and the algorithms provided by the standard library, and examine ways to enhance them when appropriate.

  • Concurrency and Parallel Programming

Low-latency systems concerns overlap those of parallel and concurrent systems in different ways: reducing reliance on blocking operations and synchronization facilities, for example, is a strong trend in these two areas.
C++ features used will include C++ 11/14 threading and atomics tools, but with an emphasis on usage patterns that allow components to react in a timely manner to events.

  • Dynamic Memory Allocation

C++ lets programmers manage memory allocation manually in various and interesting ways. We will explore ways to use these mechanisms to our advantage when building low-latency systems.
C++ features used will include specializing operators new and delete in many ways, understanding placement new, and how to write allocators before and since C++ 11.

  • Efficient Cache Usage

There have been a number of interesting presentations on cache memory usage and its impact on program performance. We will explore the nuances of this question with examples.
C++ features used will include STL containers and algorithms, alignment control mechanisms, as well as parallel and concurrent programming utilities.

  • Metaprogramming

Of course, when it is practical to do so, the fastest programs are those that do nothing. We will explore some ways to combine metaprogramming techniques with small programs in order to move computation to compile time when there are benefits in so doing.

Course Instructor

Patrice Roy

Patrice Roy has been playing with C++, either professionally, for pleasure or (most of the time) both for over 20 years. After a few years doing R&D and working on military flight simulators, he moved on to academics and has been teaching computer science since 1998. Since 2005, he’s 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. The rapid evolution of C++ in recent years has made his job even more enjoyable.

He’s been a participating member in the ISO C++ Standards Committee since late 2014 and has been involved with the ISO Programming Language Vulnerabilities since late 2015. He has five kids, and his wife ensures their house is home to a continuously changing number of cats, dogs and other animals.