From eca48632df8d8c0592bc01057d041034d15644d8 Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Wed, 2 Dec 2026 09:54:05 -0800 Subject: [PATCH] perf(processors): eliminate redundant Tuple round-trip copies in case-transform processors Construct the renamed NamedTuple directly from its identical Values tuple via internal::from_values_tuple instead of round-tripping through NamedTuple::transform -> fields() -> apply -> make_from_tuple across all five case-transform processors: - NamedTuple.hpp: add minimal internal::from_values_tuple_t tag and explicit NamedTuple(internal::from_values_tuple_t, Values&& / const Values&) constructors on both NamedTuple and NamedTuple<>. - internal/transform_case.hpp: add transform_case_field and transform_named_tuple_case (rvalue move and const-lvalue copy overloads) preserving xml_content, rfl::Rename, const fields, single-field nested NamedTuples, and empty NamedTuple<> behavior while enabling move-only field values. - SnakeCaseToCamelCase.hpp, SnakeCaseToPascalCase.hpp, SnakeCaseToKebabCase.hpp, CamelCaseToSnakeCase.hpp, ToAllCaps.hpp: forward auto&& _named_tuple directly to transform_named_tuple_case. --- include/rfl/CamelCaseToSnakeCase.hpp | 30 ++------- include/rfl/NamedTuple.hpp | 22 +++++++ include/rfl/SnakeCaseToCamelCase.hpp | 31 ++------- include/rfl/SnakeCaseToKebabCase.hpp | 35 +++-------- include/rfl/SnakeCaseToPascalCase.hpp | 32 ++-------- include/rfl/ToAllCaps.hpp | 28 ++------- include/rfl/internal/transform_case.hpp | 83 +++++++++++++++++++++++++ 7 files changed, 137 insertions(+), 124 deletions(-) diff --git a/include/rfl/CamelCaseToSnakeCase.hpp b/include/rfl/CamelCaseToSnakeCase.hpp index dcc15d9e8..40753f789 100644 --- a/include/rfl/CamelCaseToSnakeCase.hpp +++ b/include/rfl/CamelCaseToSnakeCase.hpp @@ -1,8 +1,8 @@ #ifndef RFL_CAMELCASETOSNAKECASE_HPP_ #define RFL_CAMELCASETOSNAKECASE_HPP_ -#include "Field.hpp" -#include "internal/is_rename.hpp" +#include + #include "internal/transform_case.hpp" namespace rfl { @@ -15,28 +15,10 @@ struct CamelCaseToSnakeCase { /// @param _named_tuple The named tuple containing the struct's fields /// @return A new named tuple with field names converted to snake_case template - static auto process(const auto& _named_tuple) { - return _named_tuple.transform([](const FieldType& _f) { - if constexpr (FieldType::name() != "xml_content" && - !internal::is_rename_v) { - return handle_one_field(_f); - } else { - return _f; - } - }); - } - - private: - /// Applies the camelCase to snake_case transformation to a single field. - /// @tparam FieldType The type of the field being transformed - /// @param _f The field to transform - /// @return A new field with the transformed name and the same value - template - static auto handle_one_field(const FieldType& _f) { - using NewFieldType = - Field(), - typename FieldType::Type>; - return NewFieldType(_f.value()); + static auto process(auto&& _named_tuple) { + return internal::transform_named_tuple_case< + internal::CamelCaseToSnakeCaseTransform>( + std::forward(_named_tuple)); } }; diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 005d99975..524c62498 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -24,6 +24,12 @@ namespace rfl { namespace internal { +struct from_values_tuple_t { + explicit from_values_tuple_t() = default; +}; + +inline constexpr from_values_tuple_t from_values_tuple{}; + /// Finds the position of rfl::ExtraFields in a field pack, returning -1 if none /// is present or -2 if more than one is present. template @@ -109,6 +115,15 @@ class NamedTuple { NamedTuple(const rfl::Tuple& _tup) : NamedTuple(rfl::make_from_tuple>(_tup)) {} + /// @brief Construct NamedTuple directly from its Values tuple (rvalue). + explicit NamedTuple(internal::from_values_tuple_t, Values&& _values) noexcept( + std::is_nothrow_move_constructible_v) + : values_(std::move(_values)) {} + + /// @brief Construct NamedTuple directly from its Values tuple (const lvalue). + explicit NamedTuple(internal::from_values_tuple_t, const Values& _values) + : values_(_values) {} + /// @brief Copy constructor. /// @param _other Another NamedTuple to copy from. NamedTuple(const NamedTuple& _other) = default; @@ -722,6 +737,13 @@ class NamedTuple<> { /// @brief Default constructor for empty NamedTuple. NamedTuple(){}; + /// @brief Construct empty NamedTuple directly from its Values tuple (rvalue). + explicit NamedTuple(internal::from_values_tuple_t, Values&&) noexcept {} + + /// @brief Construct empty NamedTuple directly from its Values tuple (const + /// lvalue). + explicit NamedTuple(internal::from_values_tuple_t, const Values&) noexcept {} + /// @brief Destructor. ~NamedTuple() = default; diff --git a/include/rfl/SnakeCaseToCamelCase.hpp b/include/rfl/SnakeCaseToCamelCase.hpp index d073b0b4a..ecf8c96b1 100644 --- a/include/rfl/SnakeCaseToCamelCase.hpp +++ b/include/rfl/SnakeCaseToCamelCase.hpp @@ -1,8 +1,8 @@ #ifndef RFL_SNAKECASETOCAMELCASE_HPP_ #define RFL_SNAKECASETOCAMELCASE_HPP_ -#include "Field.hpp" -#include "internal/is_rename.hpp" +#include + #include "internal/transform_case.hpp" namespace rfl { @@ -15,29 +15,10 @@ struct SnakeCaseToCamelCase { /// @param _named_tuple The named tuple containing the struct's fields /// @return A new named tuple with field names converted to camelCase template - static auto process(const auto& _named_tuple) { - return _named_tuple.transform([](const FieldType& _f) { - if constexpr (FieldType::name() != "xml_content" && - !internal::is_rename_v) { - return handle_one_field(_f); - } else { - return _f; - } - }); - } - - private: - /// Applies the snake_case to camelCase transformation to a single field. - /// @tparam FieldType The type of the field being transformed - /// @param _f The field to transform - /// @return A new field with the transformed name and the same value - template - static auto handle_one_field(const FieldType& _f) { - using NewFieldType = - Field(), - typename FieldType::Type>; - return NewFieldType(_f.value()); + static auto process(auto&& _named_tuple) { + return internal::transform_named_tuple_case< + internal::SnakeCaseToCamelCaseTransform>( + std::forward(_named_tuple)); } }; diff --git a/include/rfl/SnakeCaseToKebabCase.hpp b/include/rfl/SnakeCaseToKebabCase.hpp index 4b9d09a6f..48554aa1d 100644 --- a/include/rfl/SnakeCaseToKebabCase.hpp +++ b/include/rfl/SnakeCaseToKebabCase.hpp @@ -1,8 +1,8 @@ #ifndef RFL_SNAKECASETOKEBABCASE_HPP_ #define RFL_SNAKECASETOKEBABCASE_HPP_ -#include "Field.hpp" -#include "internal/is_rename.hpp" +#include + #include "internal/transform_case.hpp" namespace rfl { @@ -15,30 +15,13 @@ struct SnakeCaseToKebabCase { /// @param _named_tuple The named tuple containing the struct's fields /// @return A new named tuple with field names converted to kebab-case template - static auto process(const auto& _named_tuple) { - return _named_tuple.transform([](const FieldType& _f) { - // "xml_content" is a reserved name used by the XML Reader/Writer; - // all case-transform processors must leave it unchanged. - if constexpr (FieldType::name() != "xml_content" && - !internal::is_rename_v) { - return handle_one_field(_f); - } else { - return _f; - } - }); - } - - private: - /// Applies the snake_case to kebab-case transformation to a single field. - /// @tparam FieldType The type of the field being transformed - /// @param _f The field to transform - /// @return A new field with the transformed name and the same value - template - static auto handle_one_field(const FieldType& _f) { - using NewFieldType = - Field(), - typename FieldType::Type>; - return NewFieldType(_f.value()); + static auto process(auto&& _named_tuple) { + // "xml_content" is a reserved name used by the XML Reader/Writer; + // all case-transform processors leave it unchanged via + // transform_case_field. + return internal::transform_named_tuple_case< + internal::SnakeCaseToKebabCaseTransform>( + std::forward(_named_tuple)); } }; diff --git a/include/rfl/SnakeCaseToPascalCase.hpp b/include/rfl/SnakeCaseToPascalCase.hpp index f10361da0..101673937 100644 --- a/include/rfl/SnakeCaseToPascalCase.hpp +++ b/include/rfl/SnakeCaseToPascalCase.hpp @@ -1,8 +1,8 @@ #ifndef RFL_SNAKECASETOPASCALCASE_HPP_ #define RFL_SNAKECASETOPASCALCASE_HPP_ -#include "Field.hpp" -#include "internal/is_rename.hpp" +#include + #include "internal/transform_case.hpp" namespace rfl { @@ -15,30 +15,10 @@ struct SnakeCaseToPascalCase { /// @param _named_tuple The named tuple containing the struct's fields /// @return A new named tuple with field names converted to PascalCase template - static auto process(const auto& _named_tuple) { - const auto handle_one = [](const FieldType& _f) { - if constexpr (FieldType::name() != "xml_content" && - !internal::is_rename_v) { - return handle_one_field(_f); - } else { - return _f; - } - }; - return _named_tuple.transform(handle_one); - } - - private: - /// Applies the snake_case to PascalCase transformation to a single field. - /// @tparam FieldType The type of the field being transformed - /// @param _f The field to transform - /// @return A new field with the transformed name and the same value - template - static auto handle_one_field(const FieldType& _f) { - using NewFieldType = - Field(), - typename FieldType::Type>; - return NewFieldType(_f.value()); + static auto process(auto&& _named_tuple) { + return internal::transform_named_tuple_case< + internal::SnakeCaseToPascalCaseTransform>( + std::forward(_named_tuple)); } }; diff --git a/include/rfl/ToAllCaps.hpp b/include/rfl/ToAllCaps.hpp index c86bad16b..b858998fd 100644 --- a/include/rfl/ToAllCaps.hpp +++ b/include/rfl/ToAllCaps.hpp @@ -1,8 +1,8 @@ #ifndef RFL_TOALLCAPS_HPP_ #define RFL_TOALLCAPS_HPP_ -#include "Field.hpp" -#include "internal/is_rename.hpp" +#include + #include "internal/transform_case.hpp" namespace rfl { @@ -15,27 +15,9 @@ struct ToAllCaps { /// @param _named_tuple The named tuple containing the struct's fields /// @return A new named tuple with field names converted to all uppercase template - static auto process(const auto& _named_tuple) { - return _named_tuple.transform([](const FieldType& _f) { - if constexpr (FieldType::name() != "xml_content" && - !internal::is_rename_v) { - return handle_one_field(_f); - } else { - return _f; - } - }); - } - - private: - /// Applies the all-uppercase transformation to a single field. - /// @tparam FieldType The type of the field being transformed - /// @param _f The field to transform - /// @return A new field with the transformed name and the same value - template - static auto handle_one_field(const FieldType& _f) { - using NewFieldType = Field(), - typename FieldType::Type>; - return NewFieldType(_f.value()); + static auto process(auto&& _named_tuple) { + return internal::transform_named_tuple_case( + std::forward(_named_tuple)); } }; diff --git a/include/rfl/internal/transform_case.hpp b/include/rfl/internal/transform_case.hpp index 082f27cc6..df28444fd 100644 --- a/include/rfl/internal/transform_case.hpp +++ b/include/rfl/internal/transform_case.hpp @@ -2,9 +2,16 @@ #define RFL_INTERNAL_TRANSFORMSNAKECASE_HPP_ #include +#include +#include #include +#include +#include +#include "../Field.hpp" +#include "../NamedTuple.hpp" #include "StringLiteral.hpp" +#include "is_rename.hpp" namespace rfl::internal { @@ -80,6 +87,82 @@ consteval auto transform_snake_to_kebab() { return StringLiteral(result); } +struct SnakeCaseToCamelCaseTransform { + template + static consteval auto apply() { + return transform_snake_case<_name, /*_capitalize=*/false>(); + } +}; + +struct SnakeCaseToPascalCaseTransform { + template + static consteval auto apply() { + return transform_snake_case<_name, /*_capitalize=*/true>(); + } +}; + +struct SnakeCaseToKebabCaseTransform { + template + static consteval auto apply() { + return transform_snake_to_kebab<_name>(); + } +}; + +struct CamelCaseToSnakeCaseTransform { + template + static consteval auto apply() { + return transform_camel_case<_name>(); + } +}; + +struct ToAllCapsTransform { + template + static consteval auto apply() { + return to_all_caps<_name>(); + } +}; + +template ::name() != "xml_content" && + !is_rename_v::Type>)> +struct transform_case_field { + using type = std::remove_cvref_t; +}; + +template +struct transform_case_field { + using CleanField = std::remove_cvref_t; + using type = Field(), + typename CleanField::Type>; +}; + +/// Transforms all eligible field names of a NamedTuple in-place by moving its +/// underlying values tuple directly without intermediate field-tuple copies. +template +auto transform_named_tuple_case(NamedTuple&& _named_tuple) { + if constexpr (sizeof...(FieldTypes) == 0) { + return NamedTuple<>(); + } else { + using NewNamedTuple = NamedTuple< + typename transform_case_field::type...>; + return NewNamedTuple(from_values_tuple, std::move(_named_tuple.values())); + } +} + +/// Transforms all eligible field names of a NamedTuple by copying its +/// underlying values tuple directly without intermediate field-tuple copies. +template +auto transform_named_tuple_case(const NamedTuple& _named_tuple) { + if constexpr (sizeof...(FieldTypes) == 0) { + return NamedTuple<>(); + } else { + using NewNamedTuple = NamedTuple< + typename transform_case_field::type...>; + return NewNamedTuple(from_values_tuple, _named_tuple.values()); + } +} + } // namespace rfl::internal #endif