~/talk

How to Design a Slimmer Vector of Variants in C++

Talkat CppCon 2024 · on Sunday, 2024-09-15· [frontend-architecture]
links:none on file
slides:none on file

Starts from a real case where std::vector<std::variant<...>> blows up from ~20 KB to a theoretical minimum of 37 bytes and builds a working C++20 replacement that stores values contiguously with a bit‑packed index and a compact offset table that dynamically widens, yielding about 45 bytes in the demo and ~40–60% memory savings in benchmarks. Implementation uses PMR ideas, UBSan for alignment checks, and exposes do_get/visit APIs because subscript assignment can’t mutate in place; it is alignment‑safe but makes insert/erase and capacity semantics more complex.

AI-written summary · about

  1. 0:00Introduction
  2. 2:55Vector of variant memory problem
  3. 7:08Heap allocation design
  4. 10:18Custom mixed-type vector
  5. 11:40Metadata: types and offsets
  6. 15:06Memory alignment challenges
  7. 19:15Correct alignment with alignof
  8. 22:03Bit packing metadata overhead
  9. 27:40Static vector and special members
  10. 30:27API for mixed-type storage
  11. 34:56Subscript operator assignment issues
  12. 43:20Subscript operator workarounds
  13. 45:05Runtime performance benchmark
  14. 48:14Memory usage benchmark
  15. 49:38Final design and challenges
  16. 51:22Q&A
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.