Repository navigation
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
Conversation
…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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Tuple reads dispatch elements via an$O(N)$ linear fold, and
ViewReaderWithDefaultheap-allocates a shared boolean array on every struct read.Stripped-field readers and destructors also emit$O(N^2)$ symbols, while $O(N)$
VectorParser::writecomputes sizes withstd::distanceinstead of.size().Summary
TupleReader's linear fold with a short-circuit||jump table, movesViewReaderWithDefault'sfound_flags onto the stack, and uses.size()for sized containers.constC-array destruction.-O3, refl-cpp-workbench#33):.o, -41.3% symbolsViewReaderWithDefault: 0 heap allocsOSS consumers
munich-data-engineering/sqlgensqlgen.o(-8.0%) and RSS (-7.8%) ontest_operations.cpp.NVIDIA/cuda-quantumcuda-quantumshared_ptrallocs on defaulted structs.For Agents
upstream-queue(7fc3375d) · Score74/100(S=20,M=23,D=20,R=11) ·+398/-327(8f)#228,#234queue-tests/json/test_parsing_dispatch_and_isolation.cpp