Skip to content

perf(schema): eliminate O(N^2) Object::operator[] schema insertions and unnecessary schema AST copies - #257

Open
clockblocker wants to merge 1 commit into
queue/build-cmake-subproject-ergonomicsfrom
queue/perf-schema-object-and-ast
Open

clockblocker wants to merge 1 commit into
queue/build-cmake-subproject-ergonomicsfrom
queue/perf-schema-object-and-ast

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Stacked on: #256 (queue/build-cmake-subproject-ergonomics)

Problem

Schema builders insert fields via rfl::Object::operator[], which scans keys linearly and turns wide schema generation into $O(N^2)$ work.

In addition, parsing::schema::Type lacks a Type(VariantType&&) noexcept move constructor, causing deep subtree copies on every move.

Before: O(N^2) Object::operator[] scans in to_schema + deep subtree copies on schema::Type moves
After:  Object::reserve() + O(1) insert() + out-of-line Type(VariantType&&) noexcept in Type.cpp

Summary

  • Replaces linear Object::operator[] scans with Object::reserve() and $O(1)$ insert(), and adds an out-of-line Type(VariantType&&) noexcept constructor in Type.cpp.
  • Measured speedups (Clang 23 -O3, refl-cpp-workbench#33):
    • 128-field json::to_schema: 5.47x faster
    • tuple_to_object: 23.9x faster; deep nested tuples: 4.42x faster
  • Keeps JSON Schema output byte-identical and drops a bogus std::move on a const comment string.

OSS consumers

Consumer Location Pain point solved
hudson-trading/slang-server gen_config_main.cpp#L43 Speeds up wide config JSON Schema generation.
games-on-whales/wolf api/openapi.cpp#L92 Cuts OpenAPI schema overhead and to_schema.cpp size.
For Agents

…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.
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