Skip to content

perf(parsing): O(1) TupleReader dispatch, strip-name/destructor symbol isolation, stack found_ in ViewReaderWithDefault, and O(1) VectorParser::write size - #251

Open
clockblocker wants to merge 1 commit into
queue/perf-field-name-lookupfrom
queue/perf-tuple-reader-and-vector-write
Open

clockblocker wants to merge 1 commit into
queue/perf-field-name-lookupfrom
queue/perf-tuple-reader-and-vector-write

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Stacked on: #249 (queue/perf-field-name-lookup)

Problem

Tuple reads dispatch elements via an $O(N)$ linear fold, and ViewReaderWithDefault heap-allocates a shared boolean array on every struct read.

Stripped-field readers and destructors also emit $O(N^2)$ symbols, while VectorParser::write computes sizes with $O(N)$ std::distance instead of .size().

Before: O(N) TupleReader fold + make_shared per read + O(N^2) stripped/dtor symbols + std::distance
After:  short-circuit || fold (O(1) jump table) + stack found_ array + free helpers + O(1) .size()

Summary

  • Replaces TupleReader's linear fold with a short-circuit || jump table, moves ViewReaderWithDefault's found_ flags onto the stack, and uses $O(1)$ .size() for sized containers.
  • Extracts free per-field helpers for stripped-field readers and destructors across eight headers and fixes const C-array destruction.
  • Measured wins (Clang 23 -O3, refl-cpp-workbench#33):
    • 64-element tuple compile: -29.5% wall, -19.6% RSS, -59.3% .o, -41.3% symbols
    • 64-element tuple read: 8.22x faster; ViewReaderWithDefault: 0 heap allocs

OSS consumers

Consumer Location Pain point solved
munich-data-engineering/sqlgen sqlgen Cuts .o (-8.0%) and RSS (-7.8%) on test_operations.cpp.
NVIDIA/cuda-quantum cuda-quantum Removes per-read shared_ptr allocs on defaulted structs.
For Agents

…l isolation, stack found_ in ViewReaderWithDefault, and O(1) VectorParser::write size

Eliminates quadratic runtime/symbol scaling and per-read heap allocations across
tuple, stripped-field-name, default-value, destructor, and container parsing paths:

- TupleReader & TupleParser & call_destructors_on_tuple_where_necessary:
  Replace TupleReader's non-short-circuiting comma fold over TupleType-scoped
  `read_impl<0..N-1>` with a short-circuiting `||` fold over `assign_by_index`
  calling namespace-scope free helpers (`read_tuple_element[_inline]`,
  `placement_move_tuple_element`, `handle_missing_tuple_element`) parameterized
  only on each element's type (never `TupleType`), with explicit `#include <new>`
  for placement `new`. GCC 13 and Clang 23 `-O2` now lower 16-tuple dispatch to
  an O(1) jump table (`notrack jmp *%rax` / `jmpq *%rdx`, single bounds check)
  instead of O(N) checks per element. Also remove `TupleType`-scoped
  `add_to_array`/`add_to_schema` helpers in `TupleParser` (reserving schema
  vector capacity), isolate tuple element destruction into
  `destroy_tuple_element_if_set<ElemType>`, and short-circuit all-trivial/empty
  tuples.
- ViewReaderWithDefault:
  Replace `rfl::Ref<std::array<bool, size_>> found_` (which allocated via
  `std::make_shared` on every read of a `DefaultIfMissing` or `rfl::DefaultVal`
  struct) with stack-allocated `mutable std::array<bool, size_> found_{}`.
  Delete copy construction and copy assignment so callers cannot accidentally
  copy `ViewReaderWithDefault` by value and split `found_` state. Preserves
  `found()`'s `const std::array<bool, size_>&` signature, duplicate-key
  tracking, and `ViewReader::assign_by_index`'s existing short-circuit `||` fold.
- ViewReaderWithStrippedFieldNames & ViewReaderWithDefaultAndStrippedFieldNames:
  Remove the N-element static array of `ViewType`-scoped lambdas, member
  `assign_if_field_is_field_i<0..N-1>`, and duplicated `move_to` helpers.
  Reuse the isolated namespace-scope `assign_uninitialized_view_field[_inline]`
  and `assign_default_view_field[_inline]` helpers from `ViewReader.hpp` /
  `ViewReaderWithDefault.hpp` with short-circuiting `||` index dispatch (lowers
  to O(1) jump table at `-O2`, cutting `-O0` symbols from 59 -> 6 each).
- call_destructors_where_necessary:
  Remove the nested per-index inner lambda (which emitted N closure types
  carrying the full `NamedTuple` at `-O0`) and fold directly over
  `_view->values()` via `destroy_view_values_where_necessary`, short-circuiting
  all-trivial/empty views and fixing `const` array element destruction.
- VectorParser::write:
  Use O(1) `_vec.size()` when available (`std::list`, `std::set`, `std::multimap`,
  `std::unordered_*`, etc.) instead of unconditionally traversing the container
  with `std::distance(_vec.begin(), _vec.end())`, falling back to `std::distance`
  for `std::forward_list`.

Note: `include/rfl/parsing/tabular/ArrowReader.hpp` was left untouched because
Apache Arrow headers (`<arrow/api.h>`) are not installed in this environment to
compile-test it; it still benefits automatically from `call_destructors_where_necessary`.

Empirical verification (GCC 13.4.0 & Clang 23.1.6):
- 16-tuple / 16-field scratch TU: `-O2` `.o` 386,872 -> 242,232 bytes (-37.4%);
  `-O0` `.o` 2,318,024 -> 1,797,824 bytes (-22.4%); `TupleReader` `-O0` symbols
  46 -> 9; `ViewReaderWithStrippedFieldNames` 59 -> 6;
  `ViewReaderWithDefaultAndStrippedFieldNames` 59 -> 6;
  `call_destructors_where_necessary` 35 -> 5 (1 carrying `NamedTuple`).
- Direct 16-tuple `TupleReader::read`: GCC 13 `-O2` emits `notrack jmp *%rax`
  (indirect_jmps=1); Clang 23 `-O2` `assign_by_index` emits `cmpl $15` + `jmpq *%rdx`
  (cmps=1, calls=0, indirect_jmps=1).
- `ViewReaderWithDefault`: `_Znwm` calls in `read_object_or_array_with_default`
  dropped from 1 -> 0 for both `DefaultIfMissing` and `DefaultVal`.
- `VectorParser::write` on `std::set<int>`: extra `_Rb_tree_increment` pass
  before `array_as_root` eliminated.
- Full test suite (`-DREFLECTCPP_STRICT_WARNINGS=ON`): Clang 23.1.6 776 passed /
  1 expected locale skip (777 total, 0 warnings); GCC 13.4.0 776 passed / 1
  expected locale skip (777 total, 0 warnings); all 8 headers pass 32/32
  standalone checks on both compilers with and without
  `-DREFLECT_CPP_C_ARRAYS_OR_INHERITANCE`.
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