Skip to content

perf(parsing): isolate per-field parser helpers from enclosing NamedTuple/ViewType - #118

Open
clockblocker wants to merge 3 commits into
mainfrom
perf-parser-symbol-isolation
Open

clockblocker wants to merge 3 commits into
mainfrom
perf-parser-symbol-isolation

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

Follow-up to munich-data-engineering#795 ("More optimizations; particularly for tuples") and munich-data-engineering#799 ("Avoid O(N^2) template instantiations in no_duplicate_field_names") with zero public API changes.

Real-world adoption of reflect-cpp on large configuration and RPC structs surfaced severe compile-time, symbol-table, and DWARF debug-info bloat:

  • slang-server (production SystemVerilog LSP server with large LSP/config structs such as LspTypes.h / Config.cpp) saw measurable compile-time and .o size improvements from this change with zero source changes.
  • transmission (81-field TorrentInfo / 53-field SessionSetParams JSON-RPC 2.0 structs), osmium-tool (23 rfl::cli command translation units), and ccache (48-field ConfigData prototype) confirmed that per-field template instantiations carrying the entire enclosing NamedTuple<FieldTypes...> pack scale quadratically ($O(N^2)$ in symbol/DWARF bytes) on large structs.

What changed (all internal, zero public API changes)

  1. Per-field symbol & DWARF isolation (rfl::parsing::detail):
    • include/rfl/parsing/NamedTupleParser.hpp: extract write_named_tuple_field, build_named_tuple_field_schema, handle_missing_named_tuple_field, extract_schema_field_ptr, and scalar fast-path to_default_generic as free helpers parameterized only by the individual field type — never the enclosing NamedTuple<FieldTypes...> pack.
    • include/rfl/parsing/ViewReader.hpp & ViewReaderWithDefault.hpp: extract placement_move_field, move_assign_field, assign_uninitialized_view_field, assign_default_view_field, and extra-field helpers parameterized only by field type (never ViewType).
    • include/rfl/parsing/call_destructors_where_necessary.hpp: replace call_destructor_on_one_if_necessary<ViewType, _size, _i> with detail::destroy_field_if_set(bool, const OriginalType&) and skip trivially destructible types/C-arrays at compile time.
    • include/rfl/internal/copy_flattened_tuple_to_named_tuple.hpp: inline copy_one_element into copy_flattened_tuple_to_named_tuple to eliminate $2N$ instantiations mangling the full FieldNames + flattened tuple types.
  2. Shared field-name lookup (include/rfl/parsing/find_field_index.hpp):
    • Factor field-name lookup into detail::FieldNameLookup<typename ViewType::Names> keyed only on the rfl::Literal<names...> pack (independent of field value/wrapper types and shared across ViewReader, ViewReaderWithDefault, readers, writers, and processors): linear scan for <= 8 fields, constexpr $O(N \log N)$ heapsort + binary search for > 8 fields.
  3. $O(\log N)$ aggregate field counting (include/rfl/internal/cpp20/num_fields.hpp):
    • Replace linear probing in CountFieldsHelper::count_max_args_in_agg_init() with exponential + binary search (find_first_constructible + expand_and_search + binary_search_max) bounded cleanly by sizeof(T).

Note: two internal-only symbols with no in-repo callers (rfl::parsing::call_destructor_on_one_if_necessary and rfl::internal::copy_one_element) were replaced by the isolated helpers above.

Benchmark (origin/main 290bdb24 vs this PR, AppleClang 17 arm64, median of 3 runs)

48-field ConfigData struct (read + write + JSON schema, zero user code changes):

TU & Build Metric origin/main (290bdb24) This PR Delta
48-field plain (-O0 -g Debug) Wall time 2.23 s 2.02 s -9.4%
48-field plain (-O0 -g Debug) .o size 13.85 MiB 9.00 MiB -35.0%
48-field plain (-O0 -g Debug) __debug_str 7,712.5 KiB 5,222.1 KiB -32.3%
48-field plain (-O0 -g Debug) Symbol-name bytes 4,487.6 KiB 2,173.5 KiB -51.6%
48-field plain (-O2 -g RelWithDebInfo) Wall time 3.08 s 2.71 s -12.0%
48-field plain (-O2 -g RelWithDebInfo) .o size 6.49 MiB 4.15 MiB -36.0%
48-field plain (-O2 -g RelWithDebInfo) __text code size 95.8 KiB 63.0 KiB -34.2%
48-field plain (-O2 -g RelWithDebInfo) Symbol-name bytes 1,025.0 KiB 129.2 KiB -87.4%
48-field Description<..., DefaultVal> (-O0 -g Debug) Wall time 4.14 s 3.63 s -12.3%
48-field Description<..., DefaultVal> (-O0 -g Debug) .o size 118.98 MiB 93.30 MiB -21.6%
48-field Description<..., DefaultVal> (-O2 -g RelWithDebInfo) Wall time 5.78 s 5.20 s -10.0%
48-field Description<..., DefaultVal> (-O2 -g RelWithDebInfo) .o size 34.73 MiB 20.04 MiB -42.3%
48-field Description<..., DefaultVal> (-O2 -g RelWithDebInfo) Symbol-name bytes 4,244.7 KiB 629.1 KiB -85.2%

Testing

  • Full CTest suite (generic, json, json_c_arrays_and_inheritance, cli, env, xml): 661/661 passed under AppleClang 17 -Wall -Werror + REFLECTCPP_STRICT_WARNINGS=ON.
  • Standalone header compilation verified under -std=c++20 -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror for all 7 modified/added headers.
  • Added edge-case tests in tests/json/test_parser_isolation_edge_cases.cpp covering num_fields binary-search boundary cases, duplicate keys for both small (<= 8) and wide (> 8) structs with NoExtraFields/ExtraFields, to_default_generic scalar edge cases (uint64_t > INT64_MAX, char, bool, float, std::vector<std::string>, std::optional), Rename<..., DefaultVal<...>>, and AddStructName.

…uple/ViewType and optimize num_fields/ViewReader lookup
… Judge nits

- Restore rfl::ExtraFields + schemaful::IsSchemafulReader static_assert guards in
  ViewReader::read(integral_constant) and assign_to_matching_index (chained with
  else if constexpr for NoExtraFields).
- Factor field-name lookup into shared detail::FieldNameLookup<ViewType::Names>
  (linear scan for <=8 fields, constexpr std::sort + binary search for >8 fields).
- Move new free helpers into rfl::parsing::detail and qualify internal calls to
  avoid ADL collisions.
- Guard empty-struct size_ > 0 paths, tighten direct standard includes, drop
  unreachable set_schema_default_value recursion and unnecessary RFL_ALWAYS_INLINE.
- Add edge-case unit tests in tests/json/test_parser_isolation_edge_cases.cpp.
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