Repository navigation
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
Open
clockblocker wants to merge 1 commit into
clockblocker wants to merge 1 commit into
Conversation
…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==.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
rfl::Variant::visitexpands 12 non-short-circuiting fold overloads withstd::optionalboxing, andrfl::get_ifroutes through it.Meanwhile,$O(K^2)$ instantiations, bloating
TaggedUnion::operator==nests two visits intolibreflectcpp.aand consumer object files.Summary
SwitchDispatch,constevalget_if/element_index, andTaggedUnion::operator==.-O3, refl-cpp-workbench#33):TaggedUnion::operator==7.27x faster;visit()1.01x–1.14xlibreflectcpp.a-20.6% (1.63 → 1.29 MiB)visit()compatibility with void, reference, and non-movable returns, and fixesswap,emplace<I>, and copy/assign edge cases.OSS consumers
hudson-trading/slang-serverLspTypes.h#L3816-L3976FANUC-CORPORATION/fanuc_driverrmi/packets.hpp#L641-L666rmibinary-8.9%across 192 packets.For Agents
upstream-queue(2b9873a5) · Score69/100(S=18,M=21,D=20,R=10) ·+567/-394(6f)queue-tests/generic/test_rfl_variant.cpp