Skip to content

Avoid O(N^2) template instantiations in no_duplicate_field_names - #54

Closed
clockblocker wants to merge 1 commit into
mainfrom
optimize-no-duplicate-field-names
Closed

clockblocker wants to merge 1 commit into
mainfrom
optimize-no-duplicate-field-names

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

Addresses munich-data-engineering#350 (eliminates recursive template instantiations that trigger MSVC C1202 on large field counts).

Replaces the $O(N^2)$ recursive template expansion in rfl::internal::no_duplicate_field_names (iterate_over_i → iterate_over_j → compare_two_fields<Fields, _i, _j>) with a single constexpr sort-and-scan over std::array<std::string_view, N>.

  • Compile-time speedup: For a single 80-field tuple + 80-entry rfl::Literal (-std=c++20, -fsyntax-only), compare_two_fields previously instantiated $\binom{80}{2} = 3{,}160$ function templates per type.
    • Apple Clang 17: 1.21 s → 0.45 s
    • GCC 16: 2.22 s → 1.36 s
  • Cleaner constexpr predicate behavior: Previously, compare_two_fields fired an internal static_assert and no_duplicate_field_names() always returned true. Now it returns false when duplicates exist:
    • rfl::Literal<...>::has_duplicates() actually works as a constexpr bool predicate (returning true on duplicates instead of failing to compile).
    • Existing callers (to_ptr_named_tuple.hpp, FieldVariantParser.hpp, ParserTaggedUnion.hpp) all wrap the check in their own static_assert and now report their own domain-specific messages.
  • Zero extra heavy headers: Uses a small in-place constexpr Shell sort over std::array<std::string_view, N> (no <algorithm> dependency) and evaluates into constexpr bool result so no std::string_view array escapes the lambda.

Verification

  • Self-contained header check (-Wall -Wextra -Werror) on clang++ (Apple Clang 17) and g++-16 in both C++20 and C++23.
  • Added unit test json.test_literal_has_duplicates in tests/json/test_literal.cpp covering unique/duplicate rfl::Literal (up to 32 entries) and rfl::internal::no_duplicate_field_names on empty, single, distinct, and duplicate rfl::Field tuples.
  • Verified compile-time static_assert failures still trigger with the caller-specific messages for duplicate flattened fields, duplicate TaggedUnion tags, and duplicate FieldVariantParser fields.

Base: main (independent of all wave branches).

@clockblocker

Copy link
Copy Markdown
Owner Author

Merged upstream as munich-data-engineering#799; closing fork PR (branch preserved).

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