Skip to content

perf(named-tuple): O(1)-instantiation find_index, non-recursive find_extra_fields, and fold-based struct traits - #239

Open
clockblocker wants to merge 1 commit into
perf-apply-to-fields-has-default-valfrom
queue/perf-named-tuple-traits
Open

clockblocker wants to merge 1 commit into
perf-apply-to-fields-has-default-valfrom
queue/perf-named-tuple-traits

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Stacked on: #126 (perf-apply-to-fields-has-default-val)

Problem

Core rfl::NamedTuple lookup and struct traits expanded $O(N^2)$–$O(N^3)$ operator-fold chains, recursive extra-field scans, and eager ptr_tuple_t materializations.

On wide aggregates, that template explosion drove significant compile latency and peak RSS across downstream builds.

// Before: O(N^2) FieldWrapper operator| folds + recursive find_extra_fields + ptr_tuple_t
find_matching_field(FieldWrapper<tuple_element_t<_is, Fields>, _field_name, _is>{}...)

// After: single FindIndexImpl consteval scan + fold-based traits via apply_to_fields
FindIndexImpl<std::remove_cvref_t<Fields>>::find(_field_name.string_view());

Summary

  • Replaces recursive templates and per-field lambdas across six internal headers with consteval loops and fold expressions built on apply_to_fields.
  • Preserves all static_assert diagnostics, fixes empty-tuple find_index, and keeps exact empty Flatten<NamedTuple<>>, C-array, and inheritance semantics.
  • Measured impact (Clang 23 Release, real OSS builds; refl-cpp-workbench#33):
    • cuda-quantum: -9.5% wall time, -8.6% peak RSS.
    • sqlgen: -7.5% wall time, -3.4% .o size.
    • fanuc-libs: -4.4% wall time, -4.3% peak RSS.

OSS consumers

Consumer Location Pain point solved
munich-data-engineering/sqlgen sqlgen Cuts compile time and .o size on wide SQL schemas.
NVIDIA/cuda-quantum cuda-quantum Drops YAML config schema compile time and RSS.
FANUC-CORPORATION/fanuc_driver fanuc_driver Reduces RMI packet struct trait compile cost.
For Agents

…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<std::string_view, N> 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<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<Ts...> and std::tuple<Ts...> directly with fold expressions, eliminating integer_sequence, per-element lambdas, and nth_element_t.
- is_empty.hpp: check rfl::num_fields<U> == 0 (after stripping cvref/pointers and handling NamedTuple/Tuple directly) instead of materializing ptr_tuple_t<U>.
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