Skip to content

feat(schema): add opt-in zero-bloat rfl::Metadata<T> for out-of-band field/struct documentation - #119

Open
clockblocker wants to merge 3 commits into
perf-parser-symbol-isolationfrom
feat-rfl-metadata
Open

clockblocker wants to merge 3 commits into
perf-parser-symbol-isolationfrom
feat-rfl-metadata

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

Stacked on PR #118 (perf-parser-symbol-isolation, follow-up to munich-data-engineering#795 & munich-data-engineering#799).

Motivation (real-world adoption context)

While prototyping a reflect-cpp migration of ccache's 48-field ConfigData struct using rfl::Description<StringLiteral, rfl::DefaultVal<T>> field wrappers, Debug .o sizes exploded to 118.98 MiB (65.17 MiB __debug_str + 52.2 MiB mangled symbol names) and RelWithDebInfo .o sizes reached 34.73 MiB (~5.5 s/TU). Even with PR #118's internal symbol isolation, embedding long documentation prose as StringLiteral non-type template parameters inside every field's C++ type permanently bloats NamedTuple<FieldTypes...> manglings, DWARF debug info, and compile times (and requires modifying the struct's C++ member types). Similar large-struct pressure appeared in slang-server, transmission (81-field TorrentInfo / 53-field SessionSetParams), and osmium-tool.

What this PR adds

Opt-in, zero-bloat out-of-band metadata via rfl::Metadata<T> (include/rfl/Metadata.hpp, included by include/rfl.hpp):

struct ConfigData {
  std::string cache_dir = "/var/cache/app";
  int max_files = 100;
  bool legacy_gzip = false;
};

template <>
struct rfl::Metadata<ConfigData> {
  static constexpr std::string_view description = "Cache configuration";
  static constexpr std::array<rfl::FieldMeta, 3> fields = {{
      {"cache_dir", "Root cache directory"},
      {"max_files", "Maximum number of cached files"},
      {"legacy_gzip", "Legacy compression flag", "Use compression_codec instead"},
  }};
};
  • Zero impact on read / write / struct layout: rfl::Metadata<T> is consulted only during to_schema() — field types stay plain C++ types with zero StringLiteral NTTP bloat.
  • Flexible declaration: fields can be a static constexpr contiguous sized range (std::array, std::span, C array) or a static function fields() returning a borrowed contiguous sized range (e.g., std::span<const rfl::FieldMeta> or const auto& — allowing definitions to live out-of-line in a .cpp file; by-value containers that would dangle are rejected at compile time via static_assert). Optional struct-level description also supports both variable and function forms.
  • Processor & wrapper compatibility:
    • Matches exact processed wire field names first, falling back to pre-processor C++ member names (rfl::SnakeCaseToCamelCase, rfl::Rename, rfl::AddStructName, rfl::Flatten, rfl::NoFieldNames, rfl::DefaultVal).
    • Automatically unwraps rfl::TaggedUnion internal wrappers (TaggedUnionWrapperWithFields / TaggedUnionWrapperNoFields) so rfl::Metadata<Alternative> works seamlessly on tagged union alternatives.
    • Supports struct-level Metadata<T>::description on ReflectionType / Reflector<T> types as well as aggregate structs.
  • Cross-format schema hygiene:
    • src/rfl/json/to_schema.cpp: Type::Deprecated now sets annotations.description only when non-empty (preserving an inner inline rfl::Description when out-of-band FieldMeta adds only deprecation_message, and omitting "description" rather than emitting "description":"" when empty).
    • src/rfl/avro/to_schema.cpp, include/rfl/capnproto/is_named_type.hpp, and src/rfl/capnproto/to_schema.cpp: recursively unwrap Type::Description and Type::Deprecated in is_named_type and Cap'n Proto is_literal_type.

Benchmark (48-field ConfigData with 48 full-sentence field descriptions, AppleClang 17 arm64, median of 3 runs)

Build & Variant Wall time .o size __text __debug_str Symbol-name bytes
Debug (-O0 -g) — origin/main (290bdb24), inline rfl::Description 3.93 s 118.98 MiB 522.9 KiB 66,731.2 KiB 52,212.7 KiB
Debug (-O0 -g) — PR 1 (perf-parser-symbol-isolation), inline rfl::Description 3.60 s (-8.4%) 93.30 MiB (-21.6%) 518.8 KiB 53,492.8 KiB 39,226.2 KiB
Debug (-O0 -g) — PR 2 (rfl::Metadata<T>) 2.02 s (-48.6%) 9.07 MiB (-92.4% / 13.1×) 337.2 KiB 5,241.0 KiB (-92.1%) 2,181.1 KiB (-95.8%)
Debug (-O0 -g) — control: 48-field plain struct on PR 1 (no descriptions) 2.01 s 9.00 MiB 327.8 KiB 5,222.1 KiB 2,173.5 KiB
RelWithDebInfo (-O2 -g) — origin/main (290bdb24), inline rfl::Description 5.51 s 34.73 MiB 145.4 KiB 28,505.7 KiB 4,244.7 KiB
RelWithDebInfo (-O2 -g) — PR 1 (perf-parser-symbol-isolation), inline rfl::Description 5.19 s (-5.8%) 20.04 MiB (-42.3%) 147.5 KiB 17,076.4 KiB 629.1 KiB
RelWithDebInfo (-O2 -g) — PR 2 (rfl::Metadata<T>) 2.78 s (-49.5%) 4.23 MiB (-87.8% / 8.2×) 67.5 KiB (-53.6%) 2,832.2 KiB (-90.1%) 131.2 KiB (-96.9%)
RelWithDebInfo (-O2 -g) — control: 48-field plain struct on PR 1 (no descriptions) 2.69 s 4.15 MiB 63.0 KiB 2,815.4 KiB 129.2 KiB

Adding all 48 full-sentence field descriptions via rfl::Metadata<T> adds only +73 KiB (+0.8%) in Debug and +81 KiB (+1.9%) in RelWithDebInfo over an undescribed plain struct.

Testing

  • Full CTest suite (generic, json, json_c_arrays_and_inheritance, cli, env, xml): 673/673 passed under AppleClang 17 -Wall -Werror + REFLECTCPP_STRICT_WARNINGS=ON.
  • Standalone header checks passed for rfl/Metadata.hpp, rfl/parsing/NamedTupleParser.hpp, rfl/parsing/tagged_union_wrapper_no_ptr.hpp, rfl/parsing/Parser_default.hpp, and rfl/capnproto/is_named_type.hpp.
  • Added unit tests in tests/json/test_metadata.cpp and tests/json_c_arrays_and_inheritance/test_c_array_schema_defaults.cpp covering parity with inline Description/Deprecated, DefaultVal, runtime fields() / description(), SnakeCaseToCamelCase, Rename, AddStructName, wire-name precedence, inline-description preservation, plain deprecation omitting empty "description", TaggedUnion alternatives, Flatten, empty structs, NoFieldNames, ReflectionType, C-arrays, and concept static_asserts.

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