Designing a Slimmer Vector of C++ Variants
summary
Shows a working library that replaces std::vector<std::variant<...>> with a contiguous, bit-packed storage plus compact offset/type metadata, trading some runtime cost for far lower memory use. Benchmarks report reducing a 20 KB pessimistic case to about 45 bytes (≈20% over theoretical min) for the example mix, handling alignment, dynamic offset resizing, and offering bit-packed replacement APIs (do_get/visit/replace) instead of assignment-through-operator[]; insert/erase require careful realignment and overlapping-move handling.
AI-written summary · about
chapters (15)
- 0:00Introduction
- 0:22Motivation for problem
- 6:21Design 1: Heap-allocated variant
- 10:52Design 2: Vector-aware variant
- 11:57Design 3: Tracking offsets and types
- 16:50Memory alignment challenges
- 21:40Design 4: Proper alignment
- 24:28Design 5: Bit-packing metadata
- 33:13Performance implications
- 39:30API design challenges
- 54:14Insert and erase complexities
- 1:05:10Subscript operator issues
- 1:15:01Benchmarks and results
- 1:21:35Final design summary
- 1:28:41Lessons learned
related talks
05mUsing VV = merge_t(std::variant, std::vector)
59mStrongly Typed Containers
57mCache friendly data + functional + ranges = ❤️
57mThis Videogame Programmer Used the STL and You Will Never Guess What Happened Next
03mstd::variant & Blockchain
01h 08mFrom Numerical Cosmology to Efficient Bit Abstractions for the Standard Library
01h 31mNew (& Old) C++ Standard Library Containers - How to Choose the Right Container in C++26 and Beyond
54mGeneric Programming Without (Writing Your Own) Templates
