Skip to content

perf(parsing): add FieldNameLookup cursor-hint and binary-search field dispatch - #249

Open
clockblocker wants to merge 1 commit into
queue/perf-view-reader-isolationfrom
queue/perf-field-name-lookup
Open

clockblocker wants to merge 1 commit into
queue/perf-view-reader-isolationfrom
queue/perf-field-name-lookup

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Related munich-data-engineering/reflect-cpp#537 · Related munich-data-engineering/reflect-cpp#675

Stacked on: #234 (queue/perf-view-reader-isolation)

Problem

Deserializing structs with more than four fields expands a linear fold that compares every field name in sequence.

That lookup code is duplicated across every format, processor, and field wrapper combination, bloating object files and compile times.

Lookup strategy (> 4 fields, table keyed solely on field names):
  1. Sequential cursor hint -> O(1) fast path when keys arrive in declaration order
  2. Cold fallback          -> linear scan (<= 8) or constexpr-heapsorted binary search (> 8)

Measured vs main (3-run interleaved medians, combined with 7003ead7 & effb0f7c):
  Wide structs (32/64/128f): Clang Release -74.94% .o (4.44M->1.11M), -20.0% wall, -12.29% RSS
                             Clang Debug   -32.4% .o, -17.0% wall | GCC Release -31.4% .o, -7.7% wall
  Real OSS TUs: slang-server SlangServer.cpp -15.0% .o / -6.1% wall (Config.cpp -46.1% .o);
                moqx Loader.cpp -17.5% .o; fanuc-libs -27.1% .o / -9.9% wall; wolf -19.4% .o
  JSON runtime: Clang Record2 +10.15%, citm_read +4.54% | GCC Record2 +22.76%, Record16 +14.20%, BM_licenses -12.01%

Summary

  • Shares one FieldNameLookup table per field-name list across wrappers (like rfl::Description) and formats, leaving structs with <= 4 fields and positional reads unchanged.
  • Checks the next expected field in $O(1)$ before falling back to a small linear scan or an inline constexpr-heapsorted binary search portable to older libc++.
  • Preserves exact ExtraFields, NoExtraFields, and duplicate-key semantics (measurements in refl-cpp-workbench#34).
For Agents

…d dispatch

Parent: `perf(parsing): isolate per-field helpers in ViewReader and ViewReaderWithDefault`

Adds `rfl::parsing::FieldNameLookup<rfl::Literal<_names...>>` in
`include/rfl/parsing/find_field_index.hpp` and wires it into `ViewReader`
and `ViewReaderWithDefault` for structs with > 4 fields:

- Lookup tables are keyed solely on `ViewType::Names` (`rfl::Literal<...>`),
  so field wrappers like `rfl::Description` or different formats/processors
  share the same lookup table without duplicating instantiations.
- Sequential-key cursor hint (`find_with_hint`) hits O(1) when keys arrive in
  declaration order; `RFL_COLD` fallback uses a linear scan for <= 8 fields
  and a constexpr-heapsorted binary search for > 8 fields (avoiding reliance
  on constexpr `std::sort` in older libc++ releases).
- Keeps the small-struct (<= 4 fields) inline fold path and positional
  compile-time index path unchanged, and preserves exact `ExtraFields` /
  `NoExtraFields` / duplicate-key semantics with zero `[[maybe_unused]]`
  annotations.
- Adds focused tests in `tests/json/test_field_name_lookup.cpp` covering
  small, medium (5..8), wide (> 8), out-of-order, duplicate-key, varied key
  lengths, and empty `Literal<>` instantiations.

Benchmarks (N=3 interleaved Wave 1 vs upstream/main, Clang 23.1.6 / GCC 13.4.0):
- Synthetic wide structs (`compile_stress_wide.cpp`, 32/64/128 fields):
  -17.0% wall / -14.9% RSS / -32.4% .o (Clang Debug); -20.0% wall / -13.0% RSS /
  -74.8% .o (Clang Release); -9.2% wall / -26.2% .o (GCC Debug); -7.7% wall /
  -31.4% .o (GCC Release).
- Real OSS consumers: `slang-server` `SlangServer.cpp` -6.1% wall / -15.0% .o
  (Clang Debug), -8.6% wall / -8.0% RSS (GCC Release), `Config.cpp` -46.1% .o
  (Clang Release); `moqx` `Loader.cpp` -17.5% .o (Clang Debug), -5.4% wall
  (GCC Release); `fanuc-libs` `serialization.cpp` -9.9% wall / -27.1% .o
  (Clang Debug); `wolf` `unix_socket_server.cpp` -19.4% .o (Clang Release).
- Runtime parity (`reflect-cpp-bench-json` Release): `BM_licenses` -11.1%
  faster GCC (+0.9% Clang), `Record2` +22.8% faster GCC (-2.3% Clang),
  `Record16` +11.0% faster GCC (-1.8% Clang / +0.1% FailFast), `BM_person_read`
  -2.5% faster Clang (+0.7% GCC).
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