Skip to content

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 - #246

Open
clockblocker wants to merge 1 commit into
mainfrom
queue/perf-nth-element-and-field-names
Open

clockblocker wants to merge 1 commit into
mainfrom
queue/perf-nth-element-and-field-names

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Related munich-data-engineering/reflect-cpp#799

Problem

C++20 struct reflection ran $N$ structured-binding passes per struct, indexed parameter packs with $\Theta(N^2)$ folds, and checked duplicate names with worst-case $\Theta(N^2)$ Shellsort.

On wide aggregates, that redundant work inflated both compile time and peak RSS.

Before: N structured-binding passes + quadratic pack-index folds + Shellsort
After:  1 structured-binding pass (pointer pack) + O(1) pack indexing + constexpr heapsort

Summary

  • Replaces per-field bindings with a single pointer-pack structured binding and switches duplicate-name validation to guaranteed $O(N \log N)$ constexpr heapsort.
  • Uses C++26 Ts...[N] or __type_pack_element for $O(1)$ pack indexing (with a shared multiple-inheritance fallback on GCC ≤ 13 and MSVC), preserving nth_element() value returns.
  • Isolated compile wins (Clang 23 Release — refl-cpp-workbench#33):
    • fanuc-libs (serialization.cpp): -16.5% wall, -8.4% peak RSS
    • cuda-quantum (TargetConfigSchema.cpp): -6.9% wall, -1.8% peak RSS
    • sqlgen: -6.0% wall (slang-server -2.2%, moqx -1.6%)

OSS consumers

Consumer Location Pain point solved
FANUC-CORPORATION/fanuc_driver rmi/packets.hpp#L641-L666 Wide RMI structs (-16.5% wall / -8.4% RSS).
NVIDIA/cuda-quantum TargetConfigYamlAdapters.h#L27-L75 Wide target-config structs (-6.9% wall).
For Agents

…_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<std::index_sequence<Is...>, Ts...> instantiated once per pack and queried in O(1) via ADL-suppressed select_indexed_type<I> (including <utility> 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<T, N, i> + operator| fold (Theta(N^2) instantiations across 0..N-1) with a shared IndexedValuePack<std::index_sequence<Is...>, Ts...> constructed via make_indexed_value_pack (keyed only by Ts...) and selected in O(1) via select_indexed_value<N>.
   - 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<T, n>::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<T, n>::get_ptr with `if constexpr (_i >= 0 && static_cast<std::size_t>(_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<T, 0>, 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<FieldTypes...> 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).
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