Skip to content

Avoid instantiating named_tuple_t<T> in HasDefaultVal via apply_to_fields - #126

Open
clockblocker wants to merge 2 commits into
mainfrom
perf-apply-to-fields-has-default-val
Open

clockblocker wants to merge 2 commits into
mainfrom
perf-apply-to-fields-has-default-val

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

rfl::internal::HasDefaultVal<T> previously inherited from HasDefaultVal<named_tuple_t<T>> for every aggregate struct T, forcing instantiation of remove_ptrs_nt<view_t<T>> and a full value-type rfl::NamedTuple<Field<...>...> just to inspect whether any field carries rfl::DefaultVal.

This PR:

  1. Generalizes bind_to_tuple (both C++20 and C++26, plus scripts/bind_to_tuple.py) into rfl::internal::apply_to_fields(T&, F&&) and implements bind_to_tuple as a one-line wrapper around it.
  2. Replaces HasDefaultVal<T> : HasDefaultVal<named_tuple_t<T>> with unevaluated decltype(apply_to_fields(std::declval<T&>(), HasDefaultValVisitor{})), unwrapping rfl::Field and recursing into rfl::Flatten (including pointer-wrapped Flatten used by TaggedUnionWrapper) with zero Tuple/NamedTuple/get_field_names instantiation and dropping the named_tuple_t.hpp include from has_default_val_v.hpp.
  3. Adds unit tests covering empty structs, anonymous-namespace structs, rfl::Rename, rfl::Field, single/nested/pointer/NamedTuple rfl::Flatten, non-flattened nested structs, non-default-constructible/move-only/const/C-array members, inheritance, std::array, C arrays, unions, non-aggregates, and direct apply_to_fields usage.

Zero public API changes.

Measured Impact (AppleClang 17, C++20, interleaved user+sys CPU)

  • Trait-only (has_default_val_v, 100 × 20-field structs, -fsyntax-only): 5.34s → 2.30s (−57.0%)
  • rfl::json::read front-end (30 × 20-field structs, -fsyntax-only): 6.92s → 6.63s (−4.2%)
  • Also serves as a clean zero-NamedTuple foundation for future Parser_default compile-time optimizations.

Verification

  • Standalone strict header syntax checks (-std=c++20 -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror) pass on all modified C++20 headers.
  • 609 / 609 CTests pass (json, json_c_arrays_and_inheritance, generic, cli, env) under -Wall -Werror.

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