Skip to content

perf(internal): use O(log N) exponential + binary search in cpp20::num_fields - #233

Open
clockblocker wants to merge 1 commit into
mainfrom
queue/perf-num-fields-binary-search
Open

clockblocker wants to merge 1 commit into
mainfrom
queue/perf-num-fields-binary-search

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Related munich-data-engineering/reflect-cpp#799

Problem

In C++20 mode, rfl::num_fields counts an aggregate's fields by probing constructibility linearly from $0$ up to $N$.

On wide structs (20–36+ fields) or translation units reflecting hundreds of structs, linear SFINAE recursion inflates template depth and compile time.

36-field aggregate struct (all default-initializable):
  Before (linear):    probes 0, 1, 2, ..., 36, 37         -> 38 SFINAE checks, depth 37
  After  (O(log N)):  expands 0, 1, 2, 4, 8, 16, 32, 64,  -> ~13 SFINAE checks, O(log N) depth
                      then binary-searches [32, 64) -> 36

Summary

  • Finds the minimum constructible arity, then uses exponential doubling and binary search to locate the exact field count in $O(\log N)$ probes.
  • Keeps existing non-aggregate static_assert diagnostics intact.

OSS consumers

Consumer Location Pain point solved
openmoq/moqx ConfigSerializerTest.cpp#L26-L128 Calls rfl::num_fields 27 times in one TU over wide config structs.
hudson-trading/slang-server lsp/LspTypes.h#L3816-L3976 Reflects 376 LSP structs with up to 36 fields each in C++20 mode.
FANUC-CORPORATION/fanuc_driver rmi/packets.hpp#L641-L666 Reflects 23-field robot packet structs during serialization.
For Agents

…m_fields

Replaces the O(N) linear SFINAE recursion in `rfl::internal::cpp20::num_fields`
with an O(log N) exponential probe + binary search, cutting wide-struct C++20
reflection compile time and bounding template instantiation depth in +49/-4 LOC
with zero public API changes.

Downstream wide-struct / `num_fields` consumers:
- openmoq/moqx invokes `num_fields` 27x in one TU:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/test/config/ConfigSerializerTest.cpp#L26-L128
- hudson-trading/slang-server defines 376 LSP structs up to 36 fields:
  https://github.com/hudson-trading/slang-server/blob/1b413a4655877a55b3f8a450c23e4ba6c2bf4267/include/lsp/LspTypes.h#L3816-L3976
- FANUC-CORPORATION/fanuc_driver has 23-field packet structs:
  https://github.com/FANUC-CORPORATION/fanuc_driver/blob/da5db3f611cdc9dffe687e056c4e79bea584bf5c/fanuc_libs/rmi/include/rmi/packets.hpp#L641-L666
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