Skip to content

perf(internal): O(N) remove_fields and balanced define_* type builders - #259

Open
clockblocker wants to merge 1 commit into
mainfrom
queue/perf-define-builders-and-remove-fields
Open

clockblocker wants to merge 1 commit into
mainfrom
queue/perf-define-builders-and-remove-fields

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Problem

The define_*_t type builders (literal, named-tuple, variant, and tagged-union) and remove_fields_t peel type lists one element at a time.

That $O(K^2)$ to $O(K \cdot N^2)$ template recursion spikes compile time and peak RSS on wide structs and query builders.

Before: 1-by-1 recursive peeling (O(K^2) in define_*_t, O(K*N^2) in remove_fields_t)
After:  O(1) flat fast paths + 4-way balanced merge trees + consteval KeepFieldsPlan

Summary

  • Replaces one-by-one recursion across six internal headers with $O(1)$ flat fast paths, 4-way balanced merge trees, and a consteval KeepFieldsPlan.
  • Measured wins (32-element builders & 64-field stress, refl-cpp-workbench#33):
    • GCC 13: -63.7% compile time (2.51 → 0.91 s), -58.0% peak RSS (422 → 177 MB)
    • Clang 23: -16.6% peak RSS (149 → 124 MB), -7.1% compile time
    • Contributes to 64-field flatten/builder -38.5% wall / -36.9% RSS on Clang 23
  • Guards non-NamedTuple inputs to remove_fields_t with an immediate static_assert.

OSS consumers

Consumer Location Pain point solved
munich-data-engineering/sqlgen sqlgen Cuts peak RSS on wide query builder types.
NVIDIA/cuda-quantum cuda-quantum Cuts TaggedUnion::PossibleTags template depth.
For Agents

Replace superlinear recursive template instantiation chains across
remove_fields and the define_{named_tuple,literal,variant,tagged_union}
type-level builders:

- internal/IndexedTypeMap.hpp: add a collision-free O(1)-lookup indexed
  type map (IndexedTypeMapEntry + select_indexed_type_map_entry) for flat
  pack indexing without linear recursive peeling.
- internal/remove_fields.hpp: replace O(K * N^2) recursive field removal
  with a single consteval KeepFieldsPlan and flat IndexedTypeMap expansion
  (O(N)), handling 0 names, empty NamedTuple<>, unknown/duplicate names,
  all-removed, and cvref-qualified NamedTuple inputs. Guard non-NamedTuple
  inputs with an immediate static_assert instead of deep recursion.
- internal/define_{named_tuple,literal,variant,tagged_union}.hpp: replace
  left-fold 1-by-1 peeling (O(N^2)) with O(1) fast paths (all plain Fields,
  single-tag Literals, plain variant alternatives, all NamedTuples) and
  4-way balanced divide-and-conquer via IndexedTypeMap (O(F log M)),
  preserving exact order, duplicates, 1-level nesting, and discriminator
  constraints.

Verification:
- 44/44 standalone header self-containment checks pass on Clang 23.1.6 and
  GCC 13.4.0 (default & -DREFLECTCPP_C_ARRAYS_OR_INHERITANCE, -Wall -Wextra
  -Wextra-semi -Werror).
- Compile-fail checks confirm immediate static_assert on non-NamedTuple
  inputs to remove_fields_t (int, NamedTuple*) and discriminator mismatch
  rejection in define_tagged_union_t on both Clang 23.1.6 and GCC 13.4.0.
- Full CTest suite with -DREFLECTCPP_STRICT_WARNINGS=ON: 773 passed, 1
  skipped (cli locale), 0 warnings on both Clang 23.1.6 and GCC 13.4.0.
- 32-element builders + remove_fields (-fsyntax-only): GCC 2.51s / 422 MB
  -> 0.91s / 177 MB (-63.7% time, -58.0% RSS); Clang 0.70s / 149 MB ->
  0.65s / 124 MB (-16.6% RSS).
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