From 406d0a251b8541dbb4508b81b766426a45ca8c7c Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Sun, 29 Nov 2026 22:39:13 -0800 Subject: [PATCH] perf(internal): O(1) nth_element/nth_element_t, single-bind C++20 get_field_names, and guaranteed O(N log N) no_duplicate_field_names Eliminate quadratic template instantiation and worst-case compile-time blowups across internal pack indexing, C++20 field-name extraction, and duplicate field-name validation: 1. nth_element_t.hpp: - Prefer C++26 pack indexing (Ts...[N]) when __cpp_pack_indexing >= 202311L in C++26+ mode (guarded against -Wc++26-extensions on Clang in C++20/23 mode). - Guard __type_pack_element with __has_builtin(__type_pack_element) instead of __clang__ so GCC 14+ also uses the O(1) builtin. - Replace the O(N) fallback for GCC <= 13 / MSVC with a shared multiple-inheritance IndexedPack, Ts...> instantiated once per pack and queried in O(1) via ADL-suppressed select_indexed_type (including only in this fallback branch). Handles cv-qualified, reference, pointer, array, void, function, incomplete, and duplicate types. 2. nth_element.hpp: - Prefer C++26 pack indexing (`_ts...[N]`) when `__cpp_pack_indexing >= 202311L` in C++26+ mode, and otherwise replace the per-target ElementWrapper + operator| fold (Theta(N^2) instantiations across 0..N-1) with a shared IndexedValuePack, Ts...> constructed via make_indexed_value_pack (keyed only by Ts...) and selected in O(1) via select_indexed_value. - Preserve decayed value-return semantics (`constexpr auto nth_element(Ts&&... _ts)` without unconditional `noexcept`): non-selected arguments are bound by reference only and never copied or moved, only the selected N-th argument is copied/moved into the prvalue return (supporting move-only rvalues and safe temporary lifetime extension via `const auto&`). 3. cpp20/get_ith_field_from_fake_object.hpp & cpp20/get_field_names.hpp: - Add single-bind field_extractor::get_ptrs / get_field_ptrs_from_fake_object returning a flat multiple-inheritance FieldPtrPack, and refactor cpp20::get_field_names to bind the fake object once per struct instead of N separate structured bindings + N nth_element calls. - Guard select_field_ptr in field_extractor::get_ptr with `if constexpr (_i >= 0 && static_cast(_i) < n)` for clean out-of-bounds static_assert diagnostics. - Preserve exact source_location/__PRETTY_FUNCTION__/__FUNCSIG__ formatting across Clang, GCC, and MSVC, handle empty structs without instantiating field_extractor, and keep get_ith_field_from_fake_object / get_ith_field_ptr / ith_field_is_const intact and O(1) per lookup. 4. no_duplicate_field_names.hpp: - Pattern-match TupleLike directly (zero tuple_element_t or index_sequence instantiations, trivial O(1) specializations for 0 and 1 fields) and replace gap /= 2 Shellsort (Theta(N^2) worst case) with a self-contained constexpr heapsort (guaranteed O(N log N) worst case, shared per arity N). --- .../rfl/internal/cpp20/get_field_names.hpp | 38 ++++--- .../cpp20/get_ith_field_from_fake_object.hpp | 60 ++++++++-- .../rfl/internal/no_duplicate_field_names.hpp | 104 ++++++++++++------ include/rfl/internal/nth_element.hpp | 81 ++++++++++---- include/rfl/internal/nth_element_t.hpp | 67 +++++++++-- 5 files changed, 258 insertions(+), 92 deletions(-) diff --git a/include/rfl/internal/cpp20/get_field_names.hpp b/include/rfl/internal/cpp20/get_field_names.hpp index 707a0590a..e60222023 100644 --- a/include/rfl/internal/cpp20/get_field_names.hpp +++ b/include/rfl/internal/cpp20/get_field_names.hpp @@ -3,6 +3,7 @@ #ifndef REFLECTCPP_USE_CPP26_REFLECTION +#include #include #include @@ -104,6 +105,24 @@ consteval auto get_field_name() { #pragma clang diagnostic ignored "-Wundefined-inline" #endif +template +#if __GNUC__ +#ifndef __clang__ +[[gnu::no_sanitize_undefined]] +#endif +#endif +consteval auto extract_field_names(std::index_sequence<_is...>) { + constexpr auto ptrs = get_field_ptrs_from_fake_object(); +#if defined(__clang__) + return concat_literals( + get_field_name(ptrs))>()...); +#else + return concat_literals( + get_field_name(ptrs)>()...); +#endif +} + template #if __GNUC__ #ifndef __clang__ @@ -113,21 +132,12 @@ template consteval auto get_field_names() { using Type = std::remove_cvref_t; if constexpr (std::is_pointer_v) { - return get_field_names>(); + return get_field_names>(); + } else if constexpr (num_fields == 0) { + return rfl::Literal<>(); } else { -#if defined(__clang__) - const auto get = [](std::index_sequence<_is...>) { - return concat_literals( - get_field_name())>()...); - }; -#else - const auto get = [](std::index_sequence<_is...>) { - return concat_literals( - get_field_name()>()...); - }; -#endif - return get(std::make_index_sequence>()); + return extract_field_names( + std::make_index_sequence>()); } } diff --git a/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp b/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp index d29949677..4062dfad4 100644 --- a/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp +++ b/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp @@ -5,18 +5,40 @@ #include #include +#include #include "../../always_false.hpp" -#include "../nth_element.hpp" #include "get_fake_object.hpp" #include "num_fields.hpp" namespace rfl::internal::cpp20 { +template +struct IndexedFieldPtr { + Ptr ptr_; +}; + +template +struct FieldPtrPack; + +template +struct FieldPtrPack, Ptrs...> + : IndexedFieldPtr<_is, Ptrs>... {}; + +template +constexpr auto make_field_ptr_pack_from_refs(Refs&... _refs) noexcept { + return FieldPtrPack, Refs*...>{{&_refs}...}; +} + +template +constexpr Ptr select_field_ptr(const IndexedFieldPtr<_i, Ptr>& _p) noexcept { + return _p.ptr_; +} + template struct field_extractor { - template - static consteval auto get_ptr(U&) { + template + static consteval auto get_ptrs(U&) { static_assert( rfl::always_false_v, "\n\nThis error occurs for one of two reasons:\n\n" @@ -30,19 +52,30 @@ struct field_extractor { "classes or custom parsers in the documentation " "for solutions to this problem.\n\n"); } + + template + static consteval auto get_ptr(U& _obj) { + return get_ptrs(_obj); + } }; #define RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( \ n, ...) \ template \ struct field_extractor { \ + template \ + static consteval auto get_ptrs(U& _obj) { \ + auto& [__VA_ARGS__] = _obj; \ + return rfl::internal::cpp20::make_field_ptr_pack_from_refs(__VA_ARGS__); \ + } \ template \ static consteval auto get_ptr(U& _obj) { \ - auto& [__VA_ARGS__] = _obj; \ - const auto get_ptrs = [](auto&... _refs) { \ - return nth_element<_i>(&_refs...); \ - }; \ - return get_ptrs(__VA_ARGS__); \ + static_assert(_i >= 0, "N out of bounds."); \ + static_assert(static_cast(_i) < n, "N out of bounds."); \ + if constexpr (_i >= 0 && static_cast(_i) < n) { \ + return rfl::internal::cpp20::select_field_ptr< \ + static_cast(_i)>(get_ptrs(_obj)); \ + } \ } \ } @@ -2817,6 +2850,17 @@ RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( #undef RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS +template +consteval auto get_field_ptrs(U& _obj) { + return field_extractor>::get_ptrs(_obj); +} + +template +consteval auto get_field_ptrs_from_fake_object() { + using Type = std::remove_cvref_t; + return get_field_ptrs(get_fake_object()); +} + template consteval auto get_ith_field_ptr(U& _obj) { return field_extractor>::template get_ptr<_i>(_obj); diff --git a/include/rfl/internal/no_duplicate_field_names.hpp b/include/rfl/internal/no_duplicate_field_names.hpp index 86bf5f671..986dcb6b2 100644 --- a/include/rfl/internal/no_duplicate_field_names.hpp +++ b/include/rfl/internal/no_duplicate_field_names.hpp @@ -5,48 +5,82 @@ #include #include #include -#include -#include "../Tuple.hpp" +namespace rfl::internal { -namespace rfl { -namespace internal { +template +constexpr bool has_no_duplicate_names( + std::array _arr) noexcept { + if constexpr (_n <= 1) { + return true; + } else { + const auto sift_down = [&](std::size_t _root, std::size_t _end) noexcept { + while (true) { + const std::size_t left = 2 * _root + 1; + if (left >= _end) { + break; + } + std::size_t largest = left; + const std::size_t right = left + 1; + if (right < _end && _arr[largest] < _arr[right]) { + largest = right; + } + if (!(_arr[_root] < _arr[largest])) { + break; + } + const auto tmp = _arr[_root]; + _arr[_root] = _arr[largest]; + _arr[largest] = tmp; + _root = largest; + } + }; -template -constexpr inline bool no_duplicate_field_names() { - constexpr std::size_t num_fields = rfl::tuple_size_v; + for (std::size_t i = _n / 2; i > 0; --i) { + sift_down(i - 1, _n); + } - if constexpr (num_fields <= 1) { + for (std::size_t end = _n - 1; end > 0; --end) { + const auto tmp = _arr[0]; + _arr[0] = _arr[end]; + _arr[end] = tmp; + sift_down(0, end); + } + + for (std::size_t i = 1; i < _n; ++i) { + if (_arr[i] == _arr[i - 1]) { + return false; + } + } return true; - } else { - constexpr bool result = - [](std::index_sequence<_is...>) { - std::array arr{ - std::remove_cvref_t>::name_ - .string_view()...}; - for (std::size_t gap = arr.size() / 2; gap > 0; gap /= 2) { - for (std::size_t i = gap; i < arr.size(); ++i) { - const auto key = arr[i]; - std::size_t j = i; - while (j >= gap && key < arr[j - gap]) { - arr[j] = arr[j - gap]; - j -= gap; - } - arr[j] = key; - } - } - for (std::size_t i = 1; i < arr.size(); ++i) { - if (arr[i] == arr[i - 1]) { - return false; - } - } - return true; - }(std::make_index_sequence()); - return result; } } -} // namespace internal -} // namespace rfl +template +struct NoDuplicateFieldNames; + +template