Single Producer Single Consumer Lock-free FIFO From the Ground Up - Charles Frasch - CppCon 2023

Learn how to build a high-performance, lock-free Single Producer Single Consumer (SPSC) FIFO from scratch using C++ and take a deep dive into the optimization techniques used, including Godbolt testing and perf benchmarking.

Key takeaways
  • Single Producer Single Consumer (SPSC) FIFOs are used in trading engines and high-performance software to handle large volumes of data.
  • Lock-free SPSC FIFOs use atomic operations to avoid synchronization and improve performance.
  • The presenter demonstrates how to implement a lock-free SPSC FIFO from scratch using C++.
  • The FIFO is implemented with a ring buffer and uses memory order relax to load the push cursor.
  • False sharing is avoided by padding the ring buffer with spare space.
  • The presenter uses Godbolt to test and optimize the implementation.
  • The FIFO is tested with perf to record the number of instructions retired per cycle.
  • The implementation is released on GitHub.
  • The presenter recommends using sequentially consistent ordering for synchronization.
  • Memory order relax is used to load the push cursor.
  • The FIFO is implemented with a single producer and single consumer.
  • The presenter suggests that the implementation may perform worse with larger data types.
  • The implementation uses atomic variables to avoid synchronization.
  • The presenter recommends using the std::atomic library.
  • The implementation is tested with thread sanitizer and passes all tests.
  • The presenter recommends using G++12 with O3 to optimize the implementation.
  • The implementation is compared to other SPSC FIFOs, including Boost.
  • The presenter suggests that the implementation may not work well with G++12 unless the – Pedantic flag is used.
  • The implementation is tested with perf and shows good performance.
  • The presenter recommends using GCC 12 to optimize the implementation.
  • The implementation is compared to Dmitry Yukov’s Lock-free SPSC FIFOs.
  • The presenter suggests that the implementation may not work well with older versions of GCC.
  • The implementation is tested with unit tests and passes all tests.
  • The presenter recommends using GCC 12 with O3 to optimize the implementation.