From 5458993b919c2bb6ec01d7075065cd7827d04219 Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Wed, 2 Dec 2026 09:53:51 -0800 Subject: [PATCH] perf(named-tuple): O(1)-instantiation find_index, non-recursive find_extra_fields, and fold-based struct traits Eliminate superlinear template instantiation chains across NamedTuple and struct/tuple trait helpers: - find_index.hpp: replace FieldWrapper + operator| fold chains (O(N^2) Clang / O(N^3) GCC-MSVC) with a single FindIndexImpl specialization per tuple type that scans constexpr std::array in a consteval loop. Preserves first-match semantics, empty-tuple handling, std::tuple support, and the exact static_assert diagnostic. - NamedTuple.hpp: replace the eagerly evaluated O(N)-depth recursive find_extra_fields<>() with a non-recursive consteval loop returning -2 on a second match plus static_assert(pos_extra_fields_ != -2, ...) for the exact duplicate-ExtraFields error. Simplify both make_fields overloads to expand FieldTypes... directly with _is..., removing per-field wrap lambdas and nth_element_t lookups. - has_fields.hpp: inspect member types in a single pass via rfl::internal::apply_to_fields and fold expressions (with a zero-flatten fast path) instead of materializing ptr_tuple_t (rfl::Tuple + calculate_positions), preserving exact semantics for NamedTuple (including empty NamedTuple<> inside rfl::Flatten via `flags{sizeof...(Fields) > 0, true}`), Flatten, raw pointer members, empty structs, non-aggregates, C arrays, inheritance, and mixed-annotation diagnostics. - has_flatten_fields.hpp & has_arrays.hpp: pattern-match rfl::Tuple and std::tuple directly with fold expressions, eliminating integer_sequence, per-element lambdas, and nth_element_t. - is_empty.hpp: check rfl::num_fields == 0 (after stripping cvref/pointers and handling NamedTuple/Tuple directly) instead of materializing ptr_tuple_t. --- include/rfl/NamedTuple.hpp | 74 ++++++------ include/rfl/internal/find_index.hpp | 76 ++++++------ include/rfl/internal/has_arrays.hpp | 26 ++-- include/rfl/internal/has_fields.hpp | 124 ++++++++++++++------ include/rfl/internal/has_flatten_fields.hpp | 28 +++-- include/rfl/internal/is_empty.hpp | 31 ++++- 6 files changed, 218 insertions(+), 141 deletions(-) diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 381fe8811..005d99975 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -2,6 +2,8 @@ #define RFL_NAMEDTUPLE_HPP_ #include +#include +#include #include #include #include @@ -20,6 +22,32 @@ namespace rfl { +namespace internal { + +/// Finds the position of rfl::ExtraFields in a field pack, returning -1 if none +/// is present or -2 if more than one is present. +template +consteval int find_extra_fields() noexcept { + if constexpr (sizeof...(FieldTypes) == 0) { + return -1; + } else { + constexpr std::array flags{ + is_extra_fields_v::Type>...}; + int idx = -1; + for (std::size_t i = 0; i < flags.size(); ++i) { + if (flags[i]) { + if (idx != -1) { + return -2; + } + idx = static_cast(i); + } + } + return idx; + } +} + +} // namespace internal + /// A named tuple behaves like a tuple, /// but the fields have explicit names, which /// allows for reflection. @@ -547,29 +575,6 @@ class NamedTuple { } } - /// @brief Finds the position of the extra fields, or -1 if there aren't any. - /// @tparam _i Current index. - /// @tparam _idx Index of ExtraFields found so far. - /// @return Position of ExtraFields or -1. - template - constexpr static int find_extra_fields() { - if constexpr (_i == size()) { - return _idx; - } else { - using FieldType = internal::nth_element_t<_i, FieldTypes...>; - constexpr bool is_extra_fields = - internal::is_extra_fields_v; - static_assert(_idx == -1 || !is_extra_fields, - "There can only be one rfl::ExtraFields in any struct or " - "named tuple."); - if constexpr (is_extra_fields) { - return find_extra_fields<_i + 1, _i>(); - } else { - return find_extra_fields<_i + 1, _idx>(); - } - } - } - /// @brief Generates the fields for the NamedTuple (rvalue overload). /// @tparam _is Indices of the fields. /// @tparam AdditionalArgs Additional arguments to append. @@ -578,12 +583,9 @@ class NamedTuple { template auto make_fields(std::integer_sequence, AdditionalArgs&&... _args) && { - const auto wrap = [this](Index<_i>) { - using FieldType = internal::nth_element_t<_i, FieldTypes...>; - return FieldType(std::move(rfl::get<_i>(values_))); - }; - return rfl::make_tuple(wrap(Index<_is>{})..., - std::forward(_args)...); + return rfl::make_tuple( + std::remove_cvref_t(std::move(rfl::get<_is>(values_)))..., + std::forward(_args)...); } /// @brief Generates the fields for the NamedTuple (const lvalue overload). @@ -594,11 +596,8 @@ class NamedTuple { template auto make_fields(std::integer_sequence, AdditionalArgs... _args) const& { - const auto wrap = [this](Index<_i>) { - using FieldType = internal::nth_element_t<_i, FieldTypes...>; - return FieldType(rfl::get<_i>(values_)); - }; - return rfl::make_tuple(wrap(Index<_is>{})..., _args...); + return rfl::make_tuple( + std::remove_cvref_t(rfl::get<_is>(values_))..., _args...); } /// @brief Generates a new NamedTuple with one value replaced with a new @@ -702,7 +701,12 @@ class NamedTuple { Values values_; /// @brief The position of rfl::ExtraFields, or -1 if there aren't any. - constexpr static int pos_extra_fields_ = find_extra_fields(); + constexpr static int pos_extra_fields_ = + internal::find_extra_fields(); + + static_assert(pos_extra_fields_ != -2, + "There can only be one rfl::ExtraFields in any struct or " + "named tuple."); }; // ---------------------------------------------------------------------------- diff --git a/include/rfl/internal/find_index.hpp b/include/rfl/internal/find_index.hpp index 04c524e7d..9cc9f88e1 100644 --- a/include/rfl/internal/find_index.hpp +++ b/include/rfl/internal/find_index.hpp @@ -1,63 +1,57 @@ #ifndef RFL_FIND_INDEX_HPP_ #define RFL_FIND_INDEX_HPP_ +#include +#include +#include +#include +#include + #include "../Tuple.hpp" #include "StringLiteral.hpp" namespace rfl { namespace internal { -template -struct FieldWrapper { - constexpr static int i_ = _i; -}; +template +struct FindIndexImpl; -template -constexpr auto operator|(const FieldWrapper& _f1, - const FieldWrapper& _f2) { - if constexpr (F1::name_ == _field_name) { - return _f1; - } else { - return _f2; +template +struct FindIndexImpl> { + static consteval int find(const std::string_view _target) noexcept { + if constexpr (sizeof...(FieldTypes) == 0) { + return -1; + } else { + constexpr std::array names{ + std::remove_cvref_t::name_.string_view()...}; + for (std::size_t i = 0; i < names.size(); ++i) { + if (names[i] == _target) { + return static_cast(i); + } + } + return -1; + } } -} - -template -constexpr auto find_matching_field(const Head& _head, const Tail&... _tail) { - return (_head | ... | _tail); }; -template -constexpr auto wrap_fields(std::integer_sequence) { - return find_matching_field(FieldWrapper, - _field_name, _is>{}...) - .i_; -} +template +struct FindIndexImpl> + : FindIndexImpl> {}; -/// Finds the index of the field signified by _field_name +/// Finds the index of the field signified by _field_name or -1. template -constexpr int find_index() { - constexpr int ix = wrap_fields<_field_name, Fields>( - std::make_integer_sequence>()); - static_assert(rfl::tuple_element_t::name_ == _field_name, - "No matching field found."); +constexpr int find_index_or_minus_one() { + constexpr int ix = FindIndexImpl>::find( + _field_name.string_view()); return ix; } -/// Finds the index of the field signified by _field_name or -1. +/// Finds the index of the field signified by _field_name template -constexpr int find_index_or_minus_one() { - if constexpr (rfl::tuple_size_v == 0) { - return -1; - } else { - constexpr int ix = wrap_fields<_field_name, Fields>( - std::make_integer_sequence>()); - if constexpr (rfl::tuple_element_t::name_ == _field_name) { - return ix; - } else { - return -1; - } - } +constexpr int find_index() { + constexpr int ix = find_index_or_minus_one<_field_name, Fields>(); + static_assert(ix != -1, "No matching field found."); + return ix; } } // namespace internal diff --git a/include/rfl/internal/has_arrays.hpp b/include/rfl/internal/has_arrays.hpp index 896692059..58e61862d 100644 --- a/include/rfl/internal/has_arrays.hpp +++ b/include/rfl/internal/has_arrays.hpp @@ -1,26 +1,30 @@ #ifndef RFL_INTERNAL_HASARRAYS_HPP_ #define RFL_INTERNAL_HASARRAYS_HPP_ +#include #include -#include #include "../Tuple.hpp" #include "is_array.hpp" namespace rfl::internal { +template +struct HasArrays; + +template +struct HasArrays> + : std::bool_constant<(false || ... || + is_array_v>)> {}; + +template +struct HasArrays> + : std::bool_constant<(false || ... || + is_array_v>)> {}; + template constexpr bool has_arrays() { - const auto is_true_for_one = - [](std::integral_constant) -> bool { - using T = std::remove_cvref_t>; - return is_array_v; - }; - - return [&](std::integer_sequence) { - return (false || ... || - is_true_for_one(std::integral_constant{})); - }(std::make_integer_sequence>()); + return HasArrays>::value; } } // namespace rfl::internal diff --git a/include/rfl/internal/has_fields.hpp b/include/rfl/internal/has_fields.hpp index 412b87b09..5d5f43684 100644 --- a/include/rfl/internal/has_fields.hpp +++ b/include/rfl/internal/has_fields.hpp @@ -1,68 +1,116 @@ #ifndef RFL_INTERNAL_HASFIELDS_HPP_ #define RFL_INTERNAL_HASFIELDS_HPP_ +#include #include #include -#include "../Tuple.hpp" -//#include "all_fields.hpp" +#include "bind_to_tuple.hpp" #include "is_field.hpp" #include "is_flatten_field.hpp" -#include "ptr_tuple_t.hpp" #include "is_named_tuple.hpp" - namespace rfl { namespace internal { -template -constexpr bool all_fields_or_flatten() { - const auto is_true_for_one = - [](std::integral_constant) -> bool { - using T = std::remove_cvref_t>; - if constexpr (is_flatten_field_v) { - return all_fields_or_flatten< - ptr_tuple_t::Type>>(); - } else { - return is_field_v; - } - }; +struct FieldAnnotationFlags { + bool some; + bool all; +}; + +template +struct FieldAnnotationResult { + static constexpr bool some = _some; + static constexpr bool all = _all; +}; + +template +struct HasFieldsDeref { + using Type = std::remove_cvref_t; +}; + +template +struct HasFieldsDeref : HasFieldsDeref> {}; - return [&](std::integer_sequence) { - return (true && ... && is_true_for_one(std::integral_constant{})); +template +struct HasFieldsForStruct; + +template +consteval FieldAnnotationFlags has_fields_member_flags() noexcept { + using U = std::remove_cvref_t; + if constexpr (is_flatten_field::value) { + using Inner = + typename HasFieldsDeref>::Type; + return HasFieldsForStruct::flags; + } else { + constexpr bool is_f = is_field::value; + return FieldAnnotationFlags{is_f, is_f}; } - (std::make_integer_sequence>()); } -template -constexpr bool some_fields_or_flatten() { - const auto is_true_for_one = - [](std::integral_constant) -> bool { - using T = std::remove_cvref_t>; - if constexpr (is_flatten_field_v) { - return some_fields_or_flatten< - ptr_tuple_t::Type>>(); - } else { - return is_field_v; +template +consteval FieldAnnotationFlags has_fields_combine_flags() noexcept { + if constexpr (sizeof...(Fields) == 0) { + return FieldAnnotationFlags{false, true}; + } else if constexpr (!(false || ... || + is_flatten_field< + std::remove_cvref_t>::value)) { + return FieldAnnotationFlags{ + (false || ... || is_field>::value), + (true && ... && is_field>::value)}; + } else { + constexpr std::array flags{ + has_fields_member_flags()...}; + bool some = false; + bool all = true; + for (const auto& f : flags) { + some = some || f.some; + all = all && f.all; } - }; - - return [&](std::integer_sequence) { - return (false || ... || - is_true_for_one(std::integral_constant{})); + return FieldAnnotationFlags{some, all}; } - (std::make_integer_sequence>()); } +struct HasFieldsVisitor { + template + constexpr auto operator()(Fields&...) const noexcept { + constexpr auto flags = has_fields_combine_flags(); + return FieldAnnotationResult{}; + } +}; + +template +struct HasFieldsForStruct { + static constexpr FieldAnnotationFlags flags{false, false}; +}; + +template +struct HasFieldsForStruct> { + static constexpr FieldAnnotationFlags flags{sizeof...(Fields) > 0, true}; +}; + +template + requires(!is_named_tuple::value && std::is_class_v +#ifndef REFLECTCPP_USE_CPP26_REFLECTION + && std::is_aggregate_v +#endif + ) +struct HasFieldsForStruct { + using Result = decltype(rfl::internal::apply_to_fields(std::declval(), + HasFieldsVisitor{})); + static constexpr FieldAnnotationFlags flags{Result::some, Result::all}; +}; + template constexpr bool has_fields() { if constexpr (is_named_tuple_v) { return true; } else { - using TupleType = ptr_tuple_t; - if constexpr (some_fields_or_flatten()) { + using U = typename HasFieldsDeref>::Type; + constexpr auto flags = HasFieldsForStruct::flags; + if constexpr (flags.some) { static_assert( - all_fields_or_flatten(), + flags.all, "If some of your fields are annotated using rfl::Field<...>, " "then you must annotate all of your fields. " "Also, you cannot combine annotated and " diff --git a/include/rfl/internal/has_flatten_fields.hpp b/include/rfl/internal/has_flatten_fields.hpp index 869b41535..c62881410 100644 --- a/include/rfl/internal/has_flatten_fields.hpp +++ b/include/rfl/internal/has_flatten_fields.hpp @@ -1,26 +1,30 @@ #ifndef RFL_INTERNAL_HASFLATTENFIELDS_HPP_ #define RFL_INTERNAL_HASFLATTENFIELDS_HPP_ +#include #include -#include #include "../Tuple.hpp" #include "is_flatten_field.hpp" -namespace rfl ::internal { +namespace rfl::internal { + +template +struct HasFlattenFields; + +template +struct HasFlattenFields> + : std::bool_constant<(false || ... || + is_flatten_field_v>)> {}; + +template +struct HasFlattenFields> + : std::bool_constant<(false || ... || + is_flatten_field_v>)> {}; template constexpr bool has_flatten_fields() { - const auto is_true_for_one = - [](std::integral_constant) -> bool { - using T = std::remove_cvref_t>; - return is_flatten_field_v; - }; - - return [&](std::integer_sequence) { - return (false || ... || - is_true_for_one(std::integral_constant{})); - }(std::make_integer_sequence>()); + return HasFlattenFields>::value; } } // namespace rfl::internal diff --git a/include/rfl/internal/is_empty.hpp b/include/rfl/internal/is_empty.hpp index 7434710b0..e3c086896 100644 --- a/include/rfl/internal/is_empty.hpp +++ b/include/rfl/internal/is_empty.hpp @@ -1,22 +1,45 @@ #ifndef RFL_INTERNAL_ISEMPTY_HPP_ #define RFL_INTERNAL_ISEMPTY_HPP_ +#include #include #include "../Tuple.hpp" +#include "../num_fields.hpp" #include "is_named_tuple.hpp" -#include "ptr_tuple_t.hpp" namespace rfl::internal { +template +struct IsEmptyDeref { + using Type = std::remove_cvref_t; +}; + +template +struct IsEmptyDeref : IsEmptyDeref> {}; + +template +struct IsEmptyTuple : std::false_type {}; + +template +struct IsEmptyTuple> : std::true_type { + static constexpr bool empty = (sizeof...(Ts) == 0); +}; + +template +struct IsEmptyTuple> : std::true_type { + static constexpr bool empty = (sizeof...(Ts) == 0); +}; + template constexpr bool is_empty() { - using U = std::remove_cvref_t>; + using U = typename IsEmptyDeref>::Type; if constexpr (is_named_tuple_v) { return U::size() == 0; + } else if constexpr (IsEmptyTuple::value) { + return IsEmptyTuple::empty; } else { - using TupleType = ptr_tuple_t; - return rfl::tuple_size_v == 0; + return rfl::num_fields == 0; } }