Repository navigation
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
Conversation
…_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).
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.
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.
Summary
constexprheapsort.Ts...[N]or__type_pack_elementfornth_element()value returns.fanuc-libs(serialization.cpp): -16.5% wall, -8.4% peak RSScuda-quantum(TargetConfigSchema.cpp): -6.9% wall, -1.8% peak RSSsqlgen: -6.0% wall (slang-server-2.2%,moqx-1.6%)OSS consumers
FANUC-CORPORATION/fanuc_driverrmi/packets.hpp#L641-L666-16.5%wall /-8.4%RSS).NVIDIA/cuda-quantumTargetConfigYamlAdapters.h#L27-L75-6.9%wall).For Agents
upstream-queue(b1ec799b) · Score79/100(S=25,M=23,D=21,R=10) ·+250/-91(5f)queue-tests/json/test_nth_element_and_field_names.cpp