Skip to content

perf(parsing): O(log K) lookup and symbol isolation for FieldVariantReader, FieldVariantParser, and ParserTaggedUnion - #250

Open
clockblocker wants to merge 1 commit into
queue/perf-field-name-lookupfrom
queue/perf-tagged-and-field-variant-dispatch
Open

clockblocker wants to merge 1 commit into
queue/perf-field-name-lookupfrom
queue/perf-tagged-and-field-variant-dispatch

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

Field-variant and tagged-union parsers scan all $K$ alternatives linearly on every read.

They also check duplicate tags via $O(K^2)$ define_literal peeling and emit $O(K^2)$ member symbols repeating the full alternative pack.

Before: O(K) linear scans + O(K^2) define_literal peeling + O(K^2) member symbols
After:  O(log K) sorted lookup (FieldNameLookup / TaggedUnionTagLookup) + free per-alt helpers

Summary

  • Replaces linear scans and quadratic member templates across four headers with $O(\log K)$ sorted lookup tables (FieldNameLookup / TaggedUnionTagLookup) and free per-alternative helpers.
  • Measured wins (Clang 23 -O3, refl-cpp-workbench#33):
    • Runtime: 64-alt TaggedUnion read 3.31x faster; 32-alt 1.62x–1.67x faster
    • Binary: -23.6% .o, -39.0% symbol bytes (-11.7% .o on cuda-quantum)
  • Preserves exact error messages, Error::details() paths, no-field-name handling, multi-value tags, and zero-copy tag writes.

OSS consumers

Consumer Location Pain point solved
NVIDIA/cuda-quantum TargetConfigYamlAdapters.h#L27-L75 Cuts .o (-11.7%) and symbols (-12.5%) on target unions.
games-on-whales/wolf partials/spec.json#L939-L948 Shrinks TaggedUnion parser symbols on REST handlers.
For Agents

…eader, FieldVariantParser, and ParserTaggedUnion

Eliminates O(K) non-short-circuiting linear scans, O(K^2) member/lambda symbol
instantiations, and `define_literal` O(K^2) template peeling across
externally tagged (`FieldVariantReader`/`FieldVariantParser`) and internally
tagged (`ParserTaggedUnion`) union parsers:

- `find_field_index.hpp`: adds `find_in_field_name_table` (linear scan with
  `field_name_equals` for <= 8 entries, binary search for > 8) and non-cold
  `FieldNameLookup::find_direct` while keeping `FieldNameLookup::find` marked
  `RFL_COLD` for `ViewReader`'s out-of-order fallback.
- `FieldVariantReader.hpp` & `FieldVariantParser.hpp`: dispatches field names
  via `FieldNameLookup::find_direct` in O(log K), short-circuits via `||` fold
  over lockstep `(_is, FieldTypes)...`, and isolates per-alternative read,
  write (`FieldVariantWriter`), and schema (`field_variant_alternative_to_schema`)
  helpers at namespace scope without repeating `FieldTypes...`. Preserves exact
  duplicate/unknown/empty error messages and nested `Error::details()` paths.
- `ParserTaggedUnion.hpp`: adds `TaggedUnionTagLookup` built directly from
  `internal::tag_t<_discriminator, AlternativeTypes>...` (supporting single-
  and multi-value `rfl::Literal` tags), checks duplicate tags on the
  constexpr-sorted table in O(M) without `define_literal` peeling, looks up
  discriminators in O(log M), and isolates discriminator extraction, read,
  write (`TaggedUnionWriter`), and schema helpers at namespace scope. Preserves
  zero-copy writing when the discriminator is already a struct field,
  declaration-order allowed-tag lists, `NoFieldNames` handling, and nested
  `Error::details()` paths.
- `tests/json/test_tagged_and_field_variant_dispatch.cpp`: adds focused
  correctness and scale tests for 24-alternative `TaggedUnion` (including
  multi-value `rfl::Literal` tags, `NoFieldNames`, `Error::details()`, and
  schema) and 24-alternative `FieldVariantReader`/`FieldVariantParser`.

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`).
- `tests/json/test_tagged_and_field_variant_dispatch.cpp` compiled with
  `-std=c++20 -O2 -Wall -Wextra -Wextra-semi -Werror` and passed (2/2 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