std::generator in C++23: When to use, and how to improve it
summary
std::generator is type‑erased, allocates heap storage and shows measurable overhead vs inlinable code: a tiny two‑yield generator allocates 96 bytes on GCC and 64 bytes on Clang; microbenchmarks (Fibonacci-like loop, Google Benchmark with forced consumption) put its performance near virtual/indirect calls, not the inlinable case. Implementing a batched generator via a custom promise type and an awaitable suspend_if reduces per‑item cost, but compiler register spilling prevents full gains until manual assembly removes spills and cuts hot‑loop instructions by about 50%.
AI-written summary · about
chapters (8)
- 0:00Introduction
- 1:49What is std::generator
- 15:33Performance considerations
- 26:28When to use std::generator
- 31:09Improving performance with batching
- 37:35Custom generator internals
- 49:15Why generators are slow
- 55:21Summary
related talks
01h 36mUnderstanding C++ Coroutines by Example, Part 2: Generators
01h 26mC++ Coroutines - Don’t Try This at Home
04mLightning Talk: operator for - C++ Generator Ranges Without Coroutine Overhead
01h 30mTaming C++23 - The Things About C++23 You Do Not Know
01h 06mC++ Coroutines from scratch
01h 01mGetting Allocators out of Our Way
54mGeneric Programming Without (Writing Your Own) Templates
01h 31mC++ Coroutines Demystified
