Repository navigation
perf(internal): O(N) remove_fields and balanced define_* type builders - #259
Open
clockblocker wants to merge 1 commit into
Open
clockblocker wants to merge 1 commit into
clockblocker wants to merge 1 commit into
Conversation
clockblocker
force-pushed
the
queue/perf-define-builders-and-remove-fields
branch
from
December 2, 2026 13:26
6b58472 to
344be98
Compare
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
define_*_ttype builders (literal, named-tuple, variant, and tagged-union) andremove_fields_tpeel 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.
Summary
constevalKeepFieldsPlan.2.51 → 0.91 s), -58.0% peak RSS (422 → 177 MB)149 → 124 MB), -7.1% compile timeNamedTupleinputs toremove_fields_twith an immediatestatic_assert.OSS consumers
munich-data-engineering/sqlgensqlgenNVIDIA/cuda-quantumcuda-quantumTaggedUnion::PossibleTagstemplate depth.For Agents
upstream-queue(7bfe63c5) · Score66/100(S=17,M=22,D=15,R=12) ·+511/-149(6f)queue-tests/json/test_define_builders_and_remove_fields.cpp