Skip to content

perf(tuple): single-pass O(N) tuple_cat, move_from_tuple fast path, wrap_in_fields, and concat_literals - #255

Open
clockblocker wants to merge 1 commit into
mainfrom
queue/perf-tuple-cat-and-move-from-tuple
Open

clockblocker wants to merge 1 commit into
mainfrom
queue/perf-tuple-cat-and-move-from-tuple

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

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

Problem

rfl::tuple_cat, wrap_in_fields, and concat_literals rely on recursive $O(N^2)$–$O(N^3)$ template folds that slow down wide flattened structs.

Meanwhile, move_from_tuple never wired the plain-struct fast path imported in upstream PR #795.

Before: recursive tuple_cat / wrap_in_fields / concat_literals folds; skipped plain-struct fast path
After:  single-pass O(N) TupleRefs concat, non-recursive wrap_in_fields, direct aggregate init,
        and single-pass LiteralTypeTable concatenation

Summary

  • Replaces recursive tuple, field-wrapping, and literal folds across five internal headers with single-pass $O(N)$ helpers and wires direct aggregate initialization in move_from_tuple.
  • Cuts 64-field flatten/builder compile wall time by -38.5% and peak RSS by -36.9% on Clang 23 -O3 (refl-cpp-workbench#33).
  • Fixes const-lvalue tuple_cat and C-arrays inside or after rfl::Flatten.

OSS consumers

Consumer Location Pain point solved
munich-data-engineering/sqlgen sqlgen Speeds up rfl::Flatten and tuple_cat query builders.
games-on-whales/wolf wolf Cuts flattened config struct compile overhead.
For Agents

…rap_in_fields, and concat_literals

Replace quadratic/cubic compile-time and runtime folds across tuple
concatenation, tuple-to-struct reconstruction, field wrapping, and
compile-time literal concatenation with single-pass O(N) algorithms:

- `internal/tuple/concat.hpp`: replace the left-fold of pairwise
  `concat_two` calls (Theta(N^3) compile-time, Theta(N^2) runtime moves)
  with a consteval `(outer, inner)` index table and flat indexed
  reference table (`TupleRefs`), constructing the result `rfl::Tuple` in
  one pack expansion with zero intermediate tuples and strictly one
  move (rvalue) or one copy (all-lvalue) per element. Qualify internal
  helper calls (`rfl::internal::tuple::get_ref`, `extract_concat_element`,
  `concat_impl`) to prevent C++20 ADL hijacking by user element
  namespaces. Also fixes the latent `const Head&` overload bug that
  previously returned `TupleWrapper` instead of `rfl::Tuple`, and
  handles 0/1/empty/mixed lvalue+rvalue/move-only tuples cleanly.
- `internal/move_from_tuple.hpp`: guard `unflatten_ptr_tuple` with
  `has_flatten_fields<ptr_tuple_t<T>>()` and `flatten_c_arrays` with
  `has_arrays<TupleType>()`, fast-pathing plain structs directly into
  aggregate initialization `T{std::move(rfl::get<I>(_t))...}` without
  pointer tuples or `tuple_cat`. Replace the O(N^2) `IndptrHelper` fold
  with a consteval prefix-sum `std::array`, fix nested `rfl::Flatten`
  pointer unwrapping in `calc_flattened_size`, and recurse
  `flatten_c_arrays` into nested `rfl::Flatten` pointer tuples (covering
  C-style arrays inside `rfl::Flatten` and C-arrays declared after
  `rfl::Flatten`; note that the pre-existing `move_from_pointers`
  limitation remains when a C-array precedes an `rfl::Flatten` field in
  the same struct, as expanding a preceding C-array shifts indices in
  `Pointers` relative to `ptr_tuple_t<T>`).
- `internal/wrap_in_fields.hpp` + `internal/calc_flattened_size.hpp`:
  replace recursive parameter-pack peeling and `flattened_ptr_tuple_t`
  materialization with a consteval `name_offsets` array and a single
  non-recursive `index_sequence` expansion.
- `internal/concat_literals.hpp`: preserve the 0/1/all-single fast paths
  and replace the general left-fold (which instantiated O(N)
  intermediate `rfl::Literal` types with O(N^2) NTTP args on structs
  containing `rfl::Flatten`) with a single-pass `(outer, inner)` index
  table over flat `LiteralTypeTable` base packs, with qualified
  `rfl::internal::get_indexed_literal_type` lookups to prevent ADL
  hijacking.

Verified standalone header self-containment (20/20 passed, standard and
`-DREFLECT_CPP_C_ARRAYS_OR_INHERITANCE`) and full CTest suite on both
Clang 23.1.6 and GCC 13.4.0 with `-DREFLECTCPP_STRICT_WARNINGS=ON`
(0 warnings, 782 passed / 1 expected locale skip out of 783).
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