Repository navigation
perf(tuple): replace O(N^2) calculate_positions fold and per-index Tuple lambdas - #229
Open
clockblocker wants to merge 1 commit into
Open
clockblocker wants to merge 1 commit into
clockblocker wants to merge 1 commit into
Conversation
…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)
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#795
Problem
Every
rfl::Tupleexpanded a quadraticPositions<last, is...>fold and five per-index lambdas, inflating compile time and.osize on wide structs.It also gave trivially destructible views (including
rfl::to_view()) a non-trivial destructor.Summary
N=64Tuple: -75.7%.osize, 5.2x–7.2x faster compile.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.#795across 2 tuple headers (+167/-111LOC) with LLP64-safe offsets, exception-safe copy rollback, and fixedoperator<=>.rfl::Tuple<Trivial...>(includingrfl::to_view()) trivially destructible.OSS consumers
munich-data-engineering/sqlgensqlgen.osize.NVIDIA/cuda-quantumcuda-quantumgames-on-whales/wolfwolfFANUC-CORPORATION/fanuc_driverfanuc_driverFor Agents
upstream-queue(5f85a912) · Score85/100(S=26,M=26,D=21,R=12) ·+167/-111(2f)queue-tests/generic/test_tuple_queue.cpp