Skip to content

perf(variant): inline switch-based Variant::visit, O(1) get_if/element_index, and O(K) TaggedUnion::operator== - #254

Open
clockblocker wants to merge 1 commit into
mainfrom
queue/perf-rfl-variant-visit
Open

clockblocker wants to merge 1 commit into
mainfrom
queue/perf-rfl-variant-visit

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Problem

rfl::Variant::visit expands 12 non-short-circuiting fold overloads with std::optional boxing, and rfl::get_if routes through it.

Meanwhile, TaggedUnion::operator== nests two visits into $O(K^2)$ instantiations, bloating libreflectcpp.a and consumer object files.

Before: 12 do_visit_* fold overloads + std::optional boxing + O(K^2) TaggedUnion::operator==
After:  inline 1/2/3/4/8/16-way SwitchDispatch + O(1) consteval get_if/element_index + O(K) operator==

Summary

  • Replaces the fold overloads with an inline 1/2/3/4/8/16-way SwitchDispatch, $O(1)$ consteval get_if/element_index, and $O(K)$ TaggedUnion::operator==.
  • Measured wins (Clang 23 / GCC 13 -O3, refl-cpp-workbench#33):
    • Runtime: TaggedUnion::operator== 7.27x faster; visit() 1.01x–1.14x
    • Size: variant symbols -17% / -6%; code+data -34.9% / -9.8%; libreflectcpp.a -20.6% (1.63 → 1.29 MiB)
  • Preserves non-ref-qualified visit() compatibility with void, reference, and non-movable returns, and fixes swap, emplace<I>, and copy/assign edge cases.

OSS consumers

Consumer Location Pain point solved
hudson-trading/slang-server LspTypes.h#L3816-L3976 Shrinks variant code/symbols across 412 sites.
FANUC-CORPORATION/fanuc_driver rmi/packets.hpp#L641-L666 Shrinks rmi binary -8.9% across 192 packets.
For Agents

…t_index, and O(K) TaggedUnion::operator==

Replace quadratic/cubic template-fold dispatch in rfl::Variant and
rfl::TaggedUnion with inline multi-way switch dispatch and consteval
index/size loops (zero function-pointer trampoline tables or extra
symbols):

- include/rfl/internal/element_index.hpp: compute the first matching
  cvref-stripped alternative index via a single consteval loop over
  std::array<bool, sizeof...(AlternativeTypes)>.
- include/rfl/internal/variant/find_max_size.hpp: replace SizeWrapper
  operator| fold instantiations with a consteval std::size_t loop over
  sizeof(Types)..., handling empty packs cleanly.
- include/rfl/internal/variant/dispatch.hpp,
  include/rfl/internal/variant/result_t.hpp & include/rfl/Variant.hpp:
  - Replace the 12 do_visit_* non-short-circuiting fold overloads and
    std::optional return boxing with SwitchDispatch (inline 1/2/3/4/8/16-way
    switch specialization chain with RFL_ALWAYS_INLINE on Clang/GCC and
    plain inline on MSVC to avoid C4714 under /W4 /WX, O(K/16)
    instantiations, zero nth_element_t, zero address-taken trampolines).
    LLVM/GCC merge the switches into a single flat jump/lookup table and
    inline/constant-fold identically to baseline without emitting trampoline
    symbols on small variants.
  - Keep baseline non-ref-qualified Variant::visit(F&&) and
    Variant::visit(F&&) const overloads (preserving lvalue-reference
    forwarding for temporary/rvalue Variants and TaggedUnions) with SFINAE
    invocability constraints (visit_invocable / visit_const_invocable) that
    reject non-invocable, partial, mismatched-return, and value-category-
    mismatched visitors cleanly without breaking mutating generic lambdas,
    const-only visitors, or void/reference/non-movable return types.
  - Implement rfl::get_if<T> and rfl::get_if<_i> directly via
    internal::variant::Access in O(1) runtime and compile time without
    visit(), with cv-exact matching first (e.g. Variant<int, const int> and
    Variant<const int, int>) and safe qualification-adding fallback (e.g.
    get_if<const T> on Variant<T, ...> or const Variant<T, ...>), handling
    nullptr, absent types, and duplicate alternatives.
  - Replace visit()+element_index in copy_from_other, move_from_other,
    and destroy_if_necessary with SwitchDispatch (skipping destruction
    when all alternatives are trivially destructible).
  - Fix copy_from_type exception propagation (not noexcept), constrain
    Variant(T&&) and operator=(T&&) with !is_lvalue_reference_v<T> &&
    !is_const_v<T> so lvalues/const values use copy-and-swap
    operator=(const T&), keep Variant(T&&) conditionally noexcept while
    keeping Variant::operator=(T&&) noexcept, and destroy *this before
    moving in Variant::swap.
- include/rfl/TaggedUnion.hpp: restore SFINAE constraints on
  TaggedUnion::visit (both mutable and const overloads) and replace nested
  lhs.visit(rhs.visit(...)) in TaggedUnion::operator== with an index check +
  inline SwitchDispatch via Access::equals (O(K) compile time, zero extra
  symbols, 5.8x faster at K=64).
- tests/generic/test_rfl_variant.cpp: cover element_index, find_max_size,
  value categories (including rvalue Variant/TaggedUnion lvalue-ref source
  compat), return types, SFINAE detectability & mutating generic lambdas,
  move-only/non-default types, exception safety, get_if edge cases (nullptr,
  duplicates, cv-exact Variant<int, const int> / Variant<const int, int> /
  const Variant), 16/32-alt scale, and TaggedUnion::operator==.
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