From 6d1cb35a1f7e69a64285421f3d42662dd0b3852c Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Sun, 29 Nov 2026 10:40:26 -0800 Subject: [PATCH] perf(parsing): add FieldNameLookup cursor-hint and binary-search field dispatch Parent: `perf(parsing): isolate per-field helpers in ViewReader and ViewReaderWithDefault` Adds `rfl::parsing::FieldNameLookup>` in `include/rfl/parsing/find_field_index.hpp` and wires it into `ViewReader` and `ViewReaderWithDefault` for structs with > 4 fields: - Lookup tables are keyed solely on `ViewType::Names` (`rfl::Literal<...>`), so field wrappers like `rfl::Description` or different formats/processors share the same lookup table without duplicating instantiations. - Sequential-key cursor hint (`find_with_hint`) hits O(1) when keys arrive in declaration order; `RFL_COLD` fallback uses a linear scan for <= 8 fields and a constexpr-heapsorted binary search for > 8 fields (avoiding reliance on constexpr `std::sort` in older libc++ releases). - Keeps the small-struct (<= 4 fields) inline fold path and positional compile-time index path unchanged, and preserves exact `ExtraFields` / `NoExtraFields` / duplicate-key semantics with zero `[[maybe_unused]]` annotations. - Adds focused tests in `tests/json/test_field_name_lookup.cpp` covering small, medium (5..8), wide (> 8), out-of-order, duplicate-key, varied key lengths, and empty `Literal<>` instantiations. Benchmarks (N=3 interleaved Wave 1 vs upstream/main, Clang 23.1.6 / GCC 13.4.0): - Synthetic wide structs (`compile_stress_wide.cpp`, 32/64/128 fields): -17.0% wall / -14.9% RSS / -32.4% .o (Clang Debug); -20.0% wall / -13.0% RSS / -74.8% .o (Clang Release); -9.2% wall / -26.2% .o (GCC Debug); -7.7% wall / -31.4% .o (GCC Release). - Real OSS consumers: `slang-server` `SlangServer.cpp` -6.1% wall / -15.0% .o (Clang Debug), -8.6% wall / -8.0% RSS (GCC Release), `Config.cpp` -46.1% .o (Clang Release); `moqx` `Loader.cpp` -17.5% .o (Clang Debug), -5.4% wall (GCC Release); `fanuc-libs` `serialization.cpp` -9.9% wall / -27.1% .o (Clang Debug); `wolf` `unix_socket_server.cpp` -19.4% .o (Clang Release). - Runtime parity (`reflect-cpp-bench-json` Release): `BM_licenses` -11.1% faster GCC (+0.9% Clang), `Record2` +22.8% faster GCC (-2.3% Clang), `Record16` +11.0% faster GCC (-1.8% Clang / +0.1% FailFast), `BM_person_read` -2.5% faster Clang (+0.7% GCC). --- include/rfl/parsing/ViewReader.hpp | 37 ++-- include/rfl/parsing/ViewReaderWithDefault.hpp | 43 +++-- include/rfl/parsing/find_field_index.hpp | 181 ++++++++++++++++++ 3 files changed, 229 insertions(+), 32 deletions(-) create mode 100644 include/rfl/parsing/find_field_index.hpp diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index 1e1d2ac9f..b43ef6e15 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -15,6 +15,7 @@ #include "../internal/no_extra_fields_v.hpp" #include "FieldErrors.hpp" #include "Parser_base.hpp" +#include "find_field_index.hpp" #include "schemaful/IsSchemafulReader.hpp" #include "wrap_error.hpp" @@ -156,6 +157,7 @@ class ViewReader { std::make_integer_sequence()); } else { assign_to_matching_name(*r_, _name, _var, view_, errors_, found_, set_, + next_idx_, std::make_integer_sequence()); } } @@ -299,23 +301,20 @@ class ViewReader { const R& _r, const std::string_view _current_name, const InputVarType& _var, ViewType* _view, FieldErrors* _errors, std::array* _found, - std::array* _set, std::integer_sequence) { - using Fields = typename ViewType::Fields; - const bool matched = - (((is != ViewType::pos_extra_fields() && - _current_name == tuple_element_t::name()) - ? (!std::get(*_found) - ? (std::get(*_found) = true, - assign_uninitialized_view_field< - R, W, typename tuple_element_t::Type, - ProcessorsType>( - _r, _var, tuple_element_t::name(), - rfl::get(*_view), std::get(*_set), _errors, - size_), - true) - : false) - : false) || - ...); + std::array* _set, size_t& _next_idx, + std::integer_sequence _seq) { + bool matched = false; + if constexpr (size_ > 0) { + if (const int idx = FieldNameLookup:: + find_with_hint(_current_name, _next_idx); + idx >= 0 && idx != ViewType::pos_extra_fields()) { + if (!(*_found)[static_cast(idx)]) { + (*_found)[static_cast(idx)] = true; + assign_by_index(_r, idx, _var, _view, _errors, _set, _seq); + matched = true; + } + } + } if constexpr (ViewType::pos_extra_fields() != -1) { if (!matched) { handle_extra_field_name(_r, _current_name, _var, _view, _errors, _found, @@ -412,6 +411,10 @@ class ViewReader { /// Stores field errors according to the selected processor policy. FieldErrors* errors_; + + /// Sequential-key cursor hint for O(1) lookup when keys arrive in + /// declaration order. + mutable size_t next_idx_ = 0; }; } // namespace rfl::parsing diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index 1df6b30ec..450125c6a 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -17,6 +17,7 @@ #include "../internal/no_extra_fields_v.hpp" #include "FieldErrors.hpp" #include "Parser_base.hpp" +#include "find_field_index.hpp" #include "wrap_error.hpp" namespace rfl::parsing { @@ -158,6 +159,7 @@ class ViewReaderWithDefault { std::make_integer_sequence()); } else { assign_to_matching_field(*r_, _name, _var, view_, errors_, found_.get(), + next_idx_, std::make_integer_sequence()); } } @@ -209,22 +211,29 @@ class ViewReaderWithDefault { const R& _r, const std::string_view _current_name, const InputVarType& _var, ViewType* _view, FieldErrors* _errors, std::array* _found, - std::integer_sequence) { + size_t& _next_idx, std::integer_sequence) { using Fields = typename ViewType::Fields; - const bool matched = - (((is != ViewType::pos_extra_fields() && - _current_name == tuple_element_t::name()) - ? (!std::get(*_found) - ? (std::get(*_found) = true, - assign_default_view_field< - R, W, typename tuple_element_t::Type, - ProcessorsType>( - _r, _var, tuple_element_t::name(), - rfl::get(*_view), _errors), - true) - : false) - : false) || - ...); + bool matched = false; + if constexpr (size_ > 0) { + if (const int idx = FieldNameLookup:: + find_with_hint(_current_name, _next_idx); + idx >= 0 && idx != ViewType::pos_extra_fields()) { + if (!(*_found)[static_cast(idx)]) { + (*_found)[static_cast(idx)] = true; + static_cast( + ((idx == is + ? (assign_default_view_field< + R, W, typename tuple_element_t::Type, + ProcessorsType>(_r, _var, + tuple_element_t::name(), + rfl::get(*_view), _errors), + true) + : false) || + ...)); + matched = true; + } + } + } if constexpr (ViewType::pos_extra_fields() != -1) { if (!matched) { handle_extra_field(_r, _current_name, _var, _view, _errors); @@ -273,6 +282,10 @@ class ViewReaderWithDefault { /// Stores field errors according to the selected processor policy. FieldErrors* errors_; + + /// Sequential-key cursor hint for O(1) lookup when keys arrive in + /// declaration order. + mutable size_t next_idx_ = 0; }; } // namespace rfl::parsing diff --git a/include/rfl/parsing/find_field_index.hpp b/include/rfl/parsing/find_field_index.hpp new file mode 100644 index 000000000..dd4827390 --- /dev/null +++ b/include/rfl/parsing/find_field_index.hpp @@ -0,0 +1,181 @@ +#ifndef RFL_PARSING_FIND_FIELD_INDEX_HPP_ +#define RFL_PARSING_FIND_FIELD_INDEX_HPP_ + +#include +#include +#include +#include +#include + +#include "../Literal.hpp" +#include "../internal/StringLiteral.hpp" +#include "../internal/always_inline.hpp" +#include "../internal/cold.hpp" + +namespace rfl::parsing { + +struct FieldNameEntry { + std::string_view name; + int field_idx; +}; + +RFL_ALWAYS_INLINE bool field_name_equals(const std::string_view _a, + const std::string_view _b) noexcept { + const std::size_t n = _a.size(); + if (n != _b.size()) { + return false; + } + if (n == 0) { + return true; + } + const char* p = _a.data(); + const char* q = _b.data(); + if (n >= 8) { + if (n <= 16) { + std::uint64_t a0 = 0; + std::uint64_t b0 = 0; + std::uint64_t a1 = 0; + std::uint64_t b1 = 0; + std::memcpy(&a0, p, 8); + std::memcpy(&b0, q, 8); + std::memcpy(&a1, p + n - 8, 8); + std::memcpy(&b1, q + n - 8, 8); + return ((a0 ^ b0) | (a1 ^ b1)) == 0; + } + return std::memcmp(p, q, n) == 0; + } + if (n >= 4) { + std::uint32_t a0 = 0; + std::uint32_t b0 = 0; + std::uint32_t a1 = 0; + std::uint32_t b1 = 0; + std::memcpy(&a0, p, 4); + std::memcpy(&b0, q, 4); + std::memcpy(&a1, p + n - 4, 4); + std::memcpy(&b1, q + n - 4, 4); + return ((a0 ^ b0) | (a1 ^ b1)) == 0; + } + return p[0] == q[0] && p[n >> 1] == q[n >> 1] && p[n - 1] == q[n - 1]; +} + +/// O(N log N) constexpr heapsort for FieldNameEntry tables (avoids relying on +/// C++20 constexpr std::sort support in older libc++ releases). +template +constexpr void sort_field_name_table( + std::array& _table) noexcept { + auto sift_down = [&](std::size_t _root, const std::size_t _end) { + while (true) { + std::size_t child = _root * 2 + 1; + if (child >= _end) { + break; + } + if (child + 1 < _end && _table[child].name < _table[child + 1].name) { + ++child; + } + if (_table[_root].name < _table[child].name) { + const auto tmp = _table[_root]; + _table[_root] = _table[child]; + _table[child] = tmp; + _root = child; + } else { + break; + } + } + }; + for (std::size_t start = _n / 2; start > 0; --start) { + sift_down(start - 1, _n); + } + for (std::size_t end = _n; end > 1; --end) { + const auto tmp = _table[0]; + _table[0] = _table[end - 1]; + _table[end - 1] = tmp; + sift_down(0, end - 1); + } +} + +/// Compile-time field name lookup keyed solely on a NamedTuple's `Names` +/// (`rfl::Literal<_names...>`), so the lookup table is independent of field +/// value types (e.g., `rfl::Description` wrappers) and shared across +/// `ViewReader`, `ViewReaderWithDefault`, readers, writers, and processors. +template +struct FieldNameLookup; + +template <> +struct FieldNameLookup> { + static constexpr std::size_t size_ = 0; + static constexpr std::array decl_names_{}; + + static RFL_COLD int find(const std::string_view) noexcept { return -1; } + + static RFL_ALWAYS_INLINE int find_with_hint(const std::string_view, + std::size_t&) noexcept { + return -1; + } +}; + +template +struct FieldNameLookup> { + static constexpr std::size_t size_ = sizeof...(_names); + + static constexpr std::array decl_names_ = { + _names.string_view()...}; + + static constexpr auto make_table() { + std::array table{}; + int idx = 0; + ((table[static_cast(idx)] = + FieldNameEntry{_names.string_view(), idx}, + ++idx), + ...); + if constexpr (size_ > 8) { + sort_field_name_table(table); + } + return table; + } + + static RFL_COLD int find(const std::string_view _name) noexcept { + if constexpr (size_ <= 8) { + for (std::size_t i = 0; i < size_; ++i) { + if (field_name_equals(decl_names_[i], _name)) { + return static_cast(i); + } + } + return -1; + } else { + static constexpr auto table = make_table(); + std::size_t lo = 0; + std::size_t hi = table.size(); + while (lo < hi) { + const std::size_t mid = lo + (hi - lo) / 2; + if (table[mid].name < _name) { + lo = mid + 1; + } else { + hi = mid; + } + } + if (lo < table.size() && field_name_equals(table[lo].name, _name)) { + return table[lo].field_idx; + } + return -1; + } + } + + static RFL_ALWAYS_INLINE int find_with_hint(const std::string_view _name, + std::size_t& _next_idx) noexcept { + if (_next_idx < size_ && field_name_equals(decl_names_[_next_idx], _name)) + [[likely]] { + const int idx = static_cast(_next_idx); + ++_next_idx; + return idx; + } + const int idx = find(_name); + if (idx >= 0) { + _next_idx = static_cast(idx) + 1; + } + return idx; + } +}; + +} // namespace rfl::parsing + +#endif