Repository navigation
perf(schema): eliminate O(N^2) Object::operator[] schema insertions and unnecessary schema AST copies - #257
Open
clockblocker wants to merge 1 commit into
Conversation
…nd unnecessary schema AST copies
Rebased onto `upstream-queue` (`9b382c62`), which already includes explicit
out-of-line Rule-of-Five copy/move special members on `rfl::parsing::schema::Type`
and warning fixes in `src/rfl/json/to_schema.cpp` (`b6407224`). This commit
completes the schema move and O(N) insertion optimizations:
- `parsing::schema::Type` (`Type.hpp` / `Type.cpp`): adds out-of-line
`Type(VariantType&& _variant) noexcept` alongside the existing Rule-of-Five
special members so temporary `Type{std::move(...)}` construction moves
instead of deep-copying the underlying variant.
- `rfl::Object` (`Object.hpp`): adds `reserve(std::size_t)` to preallocate
backing storage without changing `sizeof(rfl::Object<T>)` or wire formats.
- O(N) unique-key schema insertions: switches `NamedTupleParser::to_schema`,
`src/rfl/json/to_schema.cpp` (`Type::Object`), and
`src/rfl/parsing/schemaful/tuple_to_object.cpp` from quadratic
`Object::operator[]` scans to `reserve(...)` + O(1) `insert(...)`, preserving
exact declaration order byte-for-byte. `NamedTupleParser::to_schema` computes
the exact non-skipped, non-`ExtraFields` count at compile time.
- Move & in-place lowering (`make.hpp`, `NamedTupleParser.hpp`,
`src/rfl/json/to_schema.cpp`): switches `parsing::schema::make`,
`NamedTupleParser::to_schema`, and JSON Schema lowering (`DefaultVal`,
`Deprecated`, `Description`, `Optional`, `Tuple`, `Object`,
`DescribedLiteral`, validation combinators, and final `JSONSchema`) to
in-place annotation mutation, `std::move`, vector `reserve`, and sorted
`emplace_hint(definitions.end(), ...)`, while preserving `b6407224`'s
shadowing and unreachable-code warning fixes.
- `tests/json/test_schema_scale.cpp`: adds scale and trait tests covering
`Object::reserve`, `parsing::schema::Type` noexcept move/constructibility,
`tuple_to_object`, wide 64-field structs, and nested variant/tagged-union/
tuple JSON schemas.
Verified:
- Standalone self-containment checks on all 4 touched headers on Clang 23.1.6
and GCC 13.4.0 (`-std=c++20 -Wall -Wextra -Wextra-semi -Werror`, default and
`-DREFLECTCPP_C_ARRAYS_OR_INHERITANCE`).
- `src/rfl/parsing/schema/Type.cpp`, `src/rfl/json/to_schema.cpp`, and
`src/rfl/parsing/schemaful/tuple_to_object.cpp` compiled cleanly on Clang
23.1.6 (`-Wall -Wextra -Wshadow -Wextra-semi -Werror`) and GCC 13.4.0
(`-Wall -Wextra -Wextra-semi -Werror`, plus `-Wshadow` clean in all touched
sources/headers).
- `tests/json/test_schema_scale.cpp` compiled with
`-std=c++20 -O2 -Wall -Wextra -Wextra-semi -Werror` and passed (5/5 tests)
on both Clang 23.1.6 and GCC 13.4.0.
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
Schema builders insert fields via$O(N^2)$ work.
rfl::Object::operator[], which scans keys linearly and turns wide schema generation intoIn addition,
parsing::schema::Typelacks aType(VariantType&&) noexceptmove constructor, causing deep subtree copies on every move.Summary
Object::operator[]scans withObject::reserve()andinsert(), and adds an out-of-lineType(VariantType&&) noexceptconstructor inType.cpp.-O3, refl-cpp-workbench#33):json::to_schema: 5.47x fastertuple_to_object: 23.9x faster; deep nested tuples: 4.42x fasterstd::moveon aconstcomment string.OSS consumers
hudson-trading/slang-servergen_config_main.cpp#L43games-on-whales/wolfapi/openapi.cpp#L92to_schema.cppsize.For Agents
upstream-queue(96883cfb) · Score88/100(S=26,M=27,D=22,R=13) ·+97/-44(7f)#212,#227,#228,#241queue-tests/json/test_schema_scale.cpp