Skip to content

Redesign eastl::variant: recursive-union storage, smallest index, compile-time visit (group G) - #88

Merged
jmgilbert2 merged 3 commits into
masterfrom
feat/variant-redesign
Nov 7, 2026
Merged

jmgilbert2 merged 3 commits into
masterfrom
feat/variant-redesign

Conversation

@jmgilbert2

@jmgilbert2 jmgilbert2 commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

SFINAE group G, plus the visitation rewrite: eastl::variant now stores its alternatives in a recursive union and has the smallest index type. eastl::visit now dispatches at compile time like MSVC STL's std::visit.

Index type

How each of the three standard libraries does it (MSVC STL 14.51, libc++ at llvm main 635f308, libstdc++ at gcc master 5700237):

Library Index type variant_npos stored as index()
MSVC STL smallest signed type: signed char below 127 alternatives, then short, then int -1 one sign extension
libstdc++ unsigned char up to 255 alternatives, then unsigned short 255 / 65535 sign extension for up to 127 alternatives, otherwise +1/-1
libc++ stable ABI: unsigned int. ABI v2 only: smallest unsigned type max value compare to npos, then select
EASTL before size_t, before the storage -1 load

All three put the index after the storage. MSVC's encoding is the cheapest: one byte for up to 126 alternatives, index() is a single movsx, and valueless is a sign test. libstdc++ does the same thing for up to 127 alternatives. EASTL now uses MSVC's encoding, after the storage. Sizes:

Type Before After MSVC
variant<char, bool> 16 2 2
variant<int, float> 16 8 8

Storage

The alternatives live in a recursive union: each level holds the first remaining alternative or the union of the rest. This is how all three libraries do it. Before, EASTL used an aligned_union buffer written with placement new and read with reinterpret_cast.

Alternative I is reached by I member accesses that are resolved at compile time and are all at offset 0. So get, visit and the special members never search the sub-objects at runtime, and variant works in constant expressions:

  • Every mode: construction, get, get_if, holds_alternative, visit and the comparisons.
  • C++20 only (P2231): copy, move, assignment, emplace, swap and destruction with non-trivial alternatives. These use std::construct_at from <memory>, which is included only in C++20 mode.

Group G's case 191 (constexpr variant<float, long> v(0)) now compiles.

Visitation

Before: every visit call went through a table of function pointers. MSVC rebuilt that table on the stack on every call. Each extra variant in a multi-variant visit added another indirect call, and the call re-checked the index in eastl::get.

Now it works like MSVC STL:

  • The variants' indices form one combined state (a mixed-radix number).
  • A switch on that state calls the visitor with that state's alternatives. Each case is inlined, and the optimizer emits a jump table or a few compares.
  • Only more than 256 combined states fall back to a table of function pointers.
  • With exceptions off (EASTL's default), no variant can be valueless, so there is no valueless state to dispatch on. That makes it one compare or bias leaner than std::visit.

visit now also takes variants of different types (the old code had a static_assert against that) and no variants at all, like std.

Release codegen of the probe functions, as static instruction counts. Each count covers the function itself plus every out-of-line function it reaches through a call, a tail jump or a table of function pointers. Data such as jump-table entries is not counted. "Indirect calls per visit" counts the indirect calls executed for one visit; a switch's jump is not counted.

cl (MSVC 14.51):

Function MSVC std EASTL before EASTL after
visit over variant<int, short, long long> 20 (+1 throw stub) 13 + 3 visitors × 3 = 22, 1 indirect call per visit 11
visit over 5 structs, calling a member 35 (+1 throw stub) 17 + 5 visitors × 2–4 = 32, 1 indirect 24
visit that writes the alternative 13 (+1 throw stub) 17 + 5 visitors × 2 = 27, 1 indirect 8
visit of 2 variants 65 (+7 throw stubs) 14 + 3 × 11 + 9 visitors × 3–4 = 75, 2 indirect 26
visit of 3 variants 5-instruction wrapper that tail-jumps to a 98-instruction _Visit_impl (+19 throw stubs) 13 + 6 × 9 + 8 visitors × 4–8 = 118, 3 indirect 55
visit<long> 18 (+1 throw stub) 13 + 3 visitors × 2 = 19, 1 indirect 9
visit over 20 alternatives 22 (+1 throw stub) 48 + 20 visitors × 2 = 88, 1 indirect 13
operator< 38 20 + 3 × 9 = 47, 1 indirect 29
destructor of variant<int, string> 6 (tail jump to _Tidy_deallocate) 11 + 1 + 6 = 18, 1 indirect 11 (string destructor inlined)

clang-cl 22:

Function MSVC std EASTL before EASTL after
visit over variant<int, short, long long> 18 (+1 throw stub, +12 throw function) 20 + 3 × 3 = 29, 1 indirect 11
visit over 5 structs, calling a member 30 (+stub, +throw) 20 + 5 × 2–4 = 35, 1 indirect 20
visit that writes the alternative 8 (+throw) 20 + 5 × 2 = 30, 1 indirect 2
visit of 2 variants 38 (+7 stubs, +throw) 21 + 3 × 3 + 9 × 3–4 = 58, 2 indirect 24
visit of 3 variants 71 (+19 stubs, +throw) 22 + 6 × 3 + 8 × 4–6 = 83, 3 indirect 49
visit<long> 16 (+stub, +throw) 20 + 3 × 2 = 26, 1 indirect 9
visit over 20 alternatives 22 (+stub, +throw) 20 + 20 × 2 = 60, 1 indirect 4 (a lookup table)
operator< 30 30 + 3 × 8 = 54, 1 indirect 22
destructor of variant<int, string> 32 19 + 1 + 6 = 26, 1 indirect 8

How to read these:

  • MSVC std inlines the visitor bodies into its switch. Its out-of-line functions are only throw stubs (sub rsp, 40; call _Throw_bad_variant_access; int 3), one per combined state in which some variant is valueless: 1 for one variant, (3+1)² − 3² = 7 for two, 3³ − 2³ = 19 for three. They never run on valid variants.
  • MSVC std doesn't inline its 3-variant _Visit_impl into the caller, but the visitor bodies are inlined inside it.
  • EASTL before: every visit made one indirect call per variant into a non-inlined visitor of 2–8 instructions. With cl, the caller first stored the whole table of function pointers on the stack: 5 lea/mov pairs for 5 alternatives, 20 pairs for 20.
  • EASTL after: nothing is out of line. The index is loaded with movsx, then a compare chain or jump table leads to the inlined bodies. Without exceptions there is no valueless state, so there are no throw stubs.

Special members, assignment, swap, comparisons

These follow MSVC STL, including its noexcept specifications:

  • Copy assignment across alternatives: if the copy can throw and the move can't, copy first, then move.
  • Converting assignment: same order (convert first, then move).
  • swap: three strategies.
    • Trivially swappable alternatives: swap the object representations.
    • Fewer than 32 alternatives: visit both variants.
    • Otherwise: swap in place if both hold the same alternative, else move through a temporary.
  • Comparisons: one compile-time visit after the index check.

Also in this PR

  • eastl::addressof is constexpr (__builtin_addressof, as all three libraries use). get_if needs it.
  • The non-trivial special-member layers shared with optional are EA_CPP20_CONSTEXPR. Nothing changes for optional until its own functions become constexpr.
  • The legacy EA_DEPRECATIONS_FOR_2025_APRIL-disabled branches of variant.h are removed (decided 2026-11-07). variant always has the conforming behavior. optional.h's legacy branches are untouched.
  • EASTL.natvis reads the new layout. Not checked in a debugger.
  • Compile cost: master's TestVariant.cpp compiles in 3.16 s with the new header vs 3.52 s with master's (cl, latest, Debug), with the same object size. The new tests (343-state table, 121- and 64-state switches, 127-alternative sizes) bring that file to 6.3 s.

Tests

  • New tests:
    • Sizes and index-type boundaries (126 vs 127 alternatives).
    • constexpr static_asserts for the C++17 and C++20 parts.
    • Every dispatch strategy: 4/16/64/256-state switches, and the table at 343 states.
    • Variants of different types, and no variants.
    • Value categories passed to the visitor.
    • All three swap strategies.
  • Red on master: the new tests fail to compile on master (msvc-latest-Debug and clangcl-17-Debug).
  • 8 configs (VS 2026 cl and clang-cl 22, C++17 and latest, Debug and Release): 8/8 pass, no warning change vs the master baseline (m36 = 21ac870). The only warnings the first build added were C4623 on the two reference-holding helper structs; they are silenced locally, as Make disabled hash specializations unusable, like std #85 did.
  • SFINAE audit c15 vs c14 (EASTL side, all six probe files): the only change this PR makes is case 191 (group G), HARD -> ok in all 6 modes. Nothing regressed.

🤖 Generated with Claude Code

jmgilbert2 and others added 3 commits November 7, 2026 15:43
The reinterpret_cast implementation can't be evaluated in a constant expression,
which made get_if (constexpr in std since C++17) and anything else taking the
address of an object non-constant. __builtin_addressof, which MSVC STL, libc++
and libstdc++ all use, ignores an overloaded operator& the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… in C++20

C++20 (P2231) makes the copy and move constructors and assignments of variant
constexpr when the alternatives' are. The layers call the base's ConstructFrom
and AssignFrom, so they must be constexpr too; for optional, whose base
functions aren't constexpr yet, nothing changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pile-time visit

Storage: the alternatives live in a recursive union (like MSVC STL, libc++ and
libstdc++) instead of an aligned buffer written with placement new and read with
reinterpret_cast. Alternative I is reached by member accesses resolved at compile
time, so variant is usable in constant expressions: construction, get, get_if,
holds_alternative, visit and the comparisons in C++17, and copy, move,
assignment, emplace, swap and destruction of non-trivial alternatives in C++20
(std::construct_at from <memory>, included in C++20 only).

Index: the smallest signed type that holds every index, after the storage, with
variant_npos as -1 (MSVC STL's encoding; libstdc++ uses the same one for up to
127 alternatives, libc++'s stable ABI uses unsigned int). index() is one sign
extension; sizeof(variant<char, bool>) goes from 16 to 2, variant<int, float>
from 16 to 8.

Visitation: like MSVC STL, the variants' indices form one combined state and a
switch on it calls the visitor with that state's alternatives, each case inlined
(a jump table or a few compares once optimized). Only more than 256 combined
states use a table of function pointers. Before, every call went through a table
of function pointers, which MSVC rebuilt on the stack on each call, and each
level of a multi-variant visit was another indirect call. Without exceptions
there is no valueless state to dispatch on. visit now also accepts variants of
different types and no variants at all, like std.

The special members, emplace, the converting assignment, swap (including MSVC
STL's swap of the object representations for trivially swappable alternatives)
and the comparisons follow MSVC STL, as do the noexcept specifications. The
legacy EA_DEPRECATIONS_FOR_2025_APRIL-disabled branches of variant.h are
removed: variant always has the conforming behavior. natvis reads the new
layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jmgilbert2
jmgilbert2 merged commit bfa0d3b into master Nov 7, 2026
28 checks passed
jmgilbert2 added a commit that referenced this pull request Nov 7, 2026
Brings in PRs #83 to #88: constrained unique_ptr/shared_ptr deleter
constructors and any's in-place constructors, disabled hash
specializations like std, compressed_pair moves its members, the C++20/23
traits, iterator, pointer_traits, span and string_view fixes of SFINAE
group J, and the variant redesign (recursive-union storage, the smallest
signed index, MSVC-style compile-time visit, constexpr variant). Master's
test changes don't apply to this branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant