Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions include/rfl/parsing/ViewReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -156,6 +157,7 @@ class ViewReader {
std::make_integer_sequence<int, size_>());
} else {
assign_to_matching_name(*r_, _name, _var, view_, errors_, found_, set_,
next_idx_,
std::make_integer_sequence<int, size_>());
}
}
Expand Down Expand Up @@ -299,23 +301,20 @@ class ViewReader {
const R& _r, const std::string_view _current_name,
const InputVarType& _var, ViewType* _view,
FieldErrors<ProcessorsType>* _errors, std::array<bool, size_>* _found,
std::array<bool, size_>* _set, std::integer_sequence<int, is...>) {
using Fields = typename ViewType::Fields;
const bool matched =
(((is != ViewType::pos_extra_fields() &&
_current_name == tuple_element_t<is, Fields>::name())
? (!std::get<is>(*_found)
? (std::get<is>(*_found) = true,
assign_uninitialized_view_field<
R, W, typename tuple_element_t<is, Fields>::Type,
ProcessorsType>(
_r, _var, tuple_element_t<is, Fields>::name(),
rfl::get<is>(*_view), std::get<is>(*_set), _errors,
size_),
true)
: false)
: false) ||
...);
std::array<bool, size_>* _set, size_t& _next_idx,
std::integer_sequence<int, is...> _seq) {
bool matched = false;
if constexpr (size_ > 0) {
if (const int idx = FieldNameLookup<typename ViewType::Names>::
find_with_hint(_current_name, _next_idx);
idx >= 0 && idx != ViewType::pos_extra_fields()) {
if (!(*_found)[static_cast<size_t>(idx)]) {
(*_found)[static_cast<size_t>(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,
Expand Down Expand Up @@ -412,6 +411,10 @@ class ViewReader {

/// Stores field errors according to the selected processor policy.
FieldErrors<ProcessorsType>* errors_;

/// Sequential-key cursor hint for O(1) lookup when keys arrive in
/// declaration order.
mutable size_t next_idx_ = 0;
};

} // namespace rfl::parsing
Expand Down
43 changes: 28 additions & 15 deletions include/rfl/parsing/ViewReaderWithDefault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -158,6 +159,7 @@ class ViewReaderWithDefault {
std::make_integer_sequence<int, size_>());
} else {
assign_to_matching_field(*r_, _name, _var, view_, errors_, found_.get(),
next_idx_,
std::make_integer_sequence<int, size_>());
}
}
Expand Down Expand Up @@ -209,22 +211,29 @@ class ViewReaderWithDefault {
const R& _r, const std::string_view _current_name,
const InputVarType& _var, ViewType* _view,
FieldErrors<ProcessorsType>* _errors, std::array<bool, size_>* _found,
std::integer_sequence<int, is...>) {
size_t& _next_idx, std::integer_sequence<int, is...>) {
using Fields = typename ViewType::Fields;
const bool matched =
(((is != ViewType::pos_extra_fields() &&
_current_name == tuple_element_t<is, Fields>::name())
? (!std::get<is>(*_found)
? (std::get<is>(*_found) = true,
assign_default_view_field<
R, W, typename tuple_element_t<is, Fields>::Type,
ProcessorsType>(
_r, _var, tuple_element_t<is, Fields>::name(),
rfl::get<is>(*_view), _errors),
true)
: false)
: false) ||
...);
bool matched = false;
if constexpr (size_ > 0) {
if (const int idx = FieldNameLookup<typename ViewType::Names>::
find_with_hint(_current_name, _next_idx);
idx >= 0 && idx != ViewType::pos_extra_fields()) {
if (!(*_found)[static_cast<size_t>(idx)]) {
(*_found)[static_cast<size_t>(idx)] = true;
static_cast<void>(
((idx == is
? (assign_default_view_field<
R, W, typename tuple_element_t<is, Fields>::Type,
ProcessorsType>(_r, _var,
tuple_element_t<is, Fields>::name(),
rfl::get<is>(*_view), _errors),
true)
: false) ||
...));
matched = true;
}
}
}
if constexpr (ViewType::pos_extra_fields() != -1) {
if (!matched) {
handle_extra_field(_r, _current_name, _var, _view, _errors);
Expand Down Expand Up @@ -273,6 +282,10 @@ class ViewReaderWithDefault {

/// Stores field errors according to the selected processor policy.
FieldErrors<ProcessorsType>* errors_;

/// Sequential-key cursor hint for O(1) lookup when keys arrive in
/// declaration order.
mutable size_t next_idx_ = 0;
};

} // namespace rfl::parsing
Expand Down
181 changes: 181 additions & 0 deletions include/rfl/parsing/find_field_index.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#ifndef RFL_PARSING_FIND_FIELD_INDEX_HPP_
#define RFL_PARSING_FIND_FIELD_INDEX_HPP_

#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string_view>

#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 <std::size_t _n>
constexpr void sort_field_name_table(
std::array<FieldNameEntry, _n>& _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 <class NamesLiteral>
struct FieldNameLookup;

template <>
struct FieldNameLookup<Literal<>> {
static constexpr std::size_t size_ = 0;
static constexpr std::array<std::string_view, 0> 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 <internal::StringLiteral... _names>
struct FieldNameLookup<Literal<_names...>> {
static constexpr std::size_t size_ = sizeof...(_names);

static constexpr std::array<std::string_view, size_> decl_names_ = {
_names.string_view()...};

static constexpr auto make_table() {
std::array<FieldNameEntry, size_> table{};
int idx = 0;
((table[static_cast<std::size_t>(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<int>(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<int>(_next_idx);
++_next_idx;
return idx;
}
const int idx = find(_name);
if (idx >= 0) {
_next_idx = static_cast<std::size_t>(idx) + 1;
}
return idx;
}
};

} // namespace rfl::parsing

#endif
Loading