Skip to content

perf(tuple): replace O(N^2) calculate_positions fold and per-index Tuple lambdas - #229

Open
clockblocker wants to merge 1 commit into
mainfrom
queue/perf-tuple-positions-and-lambdas
Open

clockblocker wants to merge 1 commit into
mainfrom
queue/perf-tuple-positions-and-lambdas

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

Every rfl::Tuple expanded a quadratic Positions<last, is...> fold and five per-index lambdas, inflating compile time and .o size on wide structs.

It also gave trivially destructible views (including rfl::to_view()) a non-trivial destructor.

// Before: O(N^2) recursive operator+ fold + 5 per-index lambdas + non-trivial ~Tuple()
return (Positions<0>{} + ... + PositionWrapper<Types>{}).to_array();

// After: O(N) consteval offset array + lockstep pack folds + trivial ~Tuple()
~Tuple() requires(std::is_trivially_destructible_v<Types> && ...) = default;

Summary

  • Measured impact (Clang 23 Release; refl-cpp-workbench#33):
    • N=64 Tuple: -75.7% .o size, 5.2x–7.2x faster compile.
    • Real OSS builds: wolf -12.6% wall, sqlgen -4.6% wall / -3.7% .o, cuda-quantum -4.0% wall / -4.8% RSS, fanuc-libs -3.3% wall / -4.4% RSS.
  • Extends upstream #795 across 2 tuple headers (+167/-111 LOC) with LLP64-safe offsets, exception-safe copy rollback, and fixed operator<=>.
  • Makes rfl::Tuple<Trivial...> (including rfl::to_view()) trivially destructible.

OSS consumers

Consumer Location Pain point solved
munich-data-engineering/sqlgen sqlgen Cuts SQL query tuple compile time and .o size.
NVIDIA/cuda-quantum cuda-quantum Reduces YAML schema compile time and RSS.
games-on-whales/wolf wolf Speeds up wide config/API tuple builds.
FANUC-CORPORATION/fanuc_driver fanuc_driver Cuts RMI packet tuple compile overhead.
For Agents

…ple lambdas

Root cause:
- rfl::internal::tuple::calculate_positions() previously built
  Positions<last, is...> via a left-fold of operator+ over PositionWrapper<T>,
  instantiating N distinct class/operator templates carrying Theta(N^2) NTTP
  arguments on every rfl::Tuple, NamedTuple, and View.
- rfl::Tuple's constructors, destructor, equality, and three-way comparison
  each instantiated per-index generic lambdas (copy_one, move_one,
  destroy_one, is_same, compare), per-index pos<_i>() helpers, and repeated
  nth_element_t<_i, Types...> lookups, creating Theta(N^2) symbol-name and
  DWARF bloat.

Fix:
- Rewrite calculate_positions<Types...>() as a single O(N) consteval loop
  returning std::array<std::size_t, sizeof...(Types) + 1> with identical
  alignment/padding offsets (including empty pack handling and LLP64-safe
  size_t arithmetic).
- Expand Types... (and OtherTypes...) directly in lockstep with
  std::index_sequence<_is...> using positions_[_is] across copy, move,
  destroy, operator==, and operator<=>, eliminating all per-index lambdas,
  pos<_i>(), and internal nth_element_t instantiations.
- Add C++20 constrained destructors so rfl::Tuple is trivially destructible
  whenever all elements are trivially destructible (e.g. to_view() pointer
  tuples), and dispatch non-trivial element destruction to a namespace-scope
  rfl::internal::tuple::destroy_element<T>() helper decoupled from the
  enclosing Tuple type.
- Roll back already-constructed prefix elements if a mid-tuple copy
  constructor throws (guarded for -fno-exceptions and skipped at compile
  time for single-element, nothrow-copy, or trivially-destructible tuples).
- Add unit tests in tests/generic/test_tuple.cpp covering empty/single/wide
  (64-element) tuples, mixed/over-aligned layouts, move-only and const
  elements, cross-type == and <=>, ctor/dtor counts, and exception rollback.

Verification:
- Standalone header syntax checks (-std=c++20 -Wall -Wextra -Wextra-semi
  -Wpedantic -Wshadow -Wconversion -Wsign-conversion -Werror, plus
  -DREFLECTCPP_C_ARRAYS_OR_INHERITANCE and -fno-exceptions): 0 warnings on
  Clang 23.1.6 and GCC 13.4.0.
- Full CMake/CTest suite (JSON + YAML, REFLECTCPP_STRICT_WARNINGS=ON):
  Clang 23.1.6: 778/778 passed (1 locale test skipped), 0 warnings.
  GCC 13.4.0: 778/778 passed (1 locale test skipped), 0 warnings.
- N=64 Tuple micro-check (two 64-element tuples, trivial + std::string):
  Clang 23 -O2 -g: 2310 ms -> 444 ms (5.2x), .o 1,076,960 B -> 262,152 B (-75.7%)
  Clang 23 -O0 -g: 988 ms -> 385 ms (2.6x), .o 2,389,072 B -> 579,728 B (-75.7%), symbols 1215 -> 384 (Tuple members 282 -> 29)
  GCC 13   -O2:    5098 ms -> 706 ms (7.2x), .o 38,672 B -> 29,840 B (-22.8%)
  GCC 13   -O0 -g: 3438 ms -> 729 ms (4.7x), .o 3,761,704 B -> 787,760 B (-79.1%), symbols 1817 -> 477 (Tuple members 678 -> 40, lambdas 388 -> 0)
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