Skip to content

perf(cli): lower_bound prefix skipping in cli::Reader, O(F) usage positionals, and O(F) option/usage symbols - #238

Open
clockblocker wants to merge 1 commit into
cli-usage-help-textfrom
queue/perf-cli-reader-and-usage
Open

clockblocker wants to merge 1 commit into
cli-usage-help-textfrom
queue/perf-cli-reader-and-usage

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Addresses munich-data-engineering/reflect-cpp#692

Stacked on: #23 (cli-usage-help-text)

Problem

rfl::cli::Reader rescanned argument maps linearly across nested prefixes and allocated a std::set per nested object.

Meanwhile, option and usage collection instantiated per-field helpers carrying the full NamedTuple type, causing $\Theta(F^2)$ symbol bloat and slow builds on wide CLI structs.

// Before: linear prefix scans + per-field templates coupled to the full NamedTuple
std::set<std::string> visited; // rescans every descendant key at each depth

// After: lower_bound subtree jumps + constexpr descriptor tables decoupled from NamedTuple
sub_prefix.back() = after_separator; // '.' + 1 == '/'
it = _obj.args->lower_bound(sub_prefix);

Summary

  • Replaces linear prefix scans with lower_bound subtree jumps, drops <set> from cli/Reader.hpp, and switches usage positional lookups to $O(1)$ hashing.
  • Decouples option and usage helpers from the full NamedTuple using constexpr descriptor tables while preserving visit order and all static_assert diagnostics.
  • Measured impact (64-option CLI, Clang 23 -O3; refl-cpp-workbench#33):
    • Compile: -26.5% wall time, -27.4% peak RSS, -85.6% cli::internal mangled symbol bytes.
    • Runtime: rfl::cli::usage 1.60x faster and rfl::cli::read 1.38x faster.
For Agents

…itionals, and O(F) option/usage symbols

Fixes three algorithmic and compile-time scaling bottlenecks in `rfl::cli`:

1. `include/rfl/cli/Reader.hpp` (`Reader::read_object`, `Reader::is_empty`):
   - Previously `read_object` stepped linearly through every entry matching
     `_obj.prefix` at every nesting level, allocating a `std::set<std::string>`
     and substring copies per entry (`O(N * D * (log K + D))`).
   - Now uses `starts_with` (zero substring allocation), skips nested subtrees
     in one jump via `lower_bound(sub_prefix + after_separator)` (where
     `after_separator = static_cast<char>(path_separator + 1) == '/'`), and
     deduplicates a child with an exact key via `_obj.args->contains(sub_prefix)`
     rather than a per-call `std::set<std::string>`. Preserves exact visit
     order, empty-prefix and empty-segment handling, and the ASCII boundary
     `'-' (45) < '.' (46) < '/' (47)`.

2. `include/rfl/cli/options.hpp`:
   - Previously instantiated `add_short_and_positional<NamedTupleType, _i>`
     and `field_info<NamedTupleType, _i>` once per field, embedding the full
     `O(F)`-length `NamedTuple` mangling `F` times (`Theta(F^2)` symbol bytes)
     alongside per-field lambdas and `add_option_for_field` instantiations.
   - Now parameterizes `field_info<FieldType>` and constexpr
     `make_option_field_descriptor<FieldType, ProcessorsType>()` solely on the
     individual field type and processors (never the enclosing `NamedTuple` or
     struct), materializes one `static constexpr` descriptor table inside
     `collect_options<T, ProcessorsType>`, and populates `Options` in a single
     non-template `populate_options` loop. Preserves `static_assert` checks for
     conflicting `Short`/`Positional`, duplicate short-name detection, and
     numeric short-option detection.

3. `include/rfl/cli/usage.hpp`:
   - Replaces the `Theta(F * P)` `std::find` positional scan in
     `UsageCollector::add_object` with an `O(1)`
     `std::unordered_set<std::string_view>` (and `std::unordered_map` with
     `std::string_view` keys for `short_names_` and `meta_`).
   - Replaces `add_usage_field_meta<U, NamedTupleType, ProcessorsType, _i>`
     (`Theta(F^2)` symbol bytes) with a constexpr
     `make_usage_field_descriptor<FieldType, ProcessorsType, bool, bool>()`
     table and a single non-template `populate_usage_field_meta` helper.

Tests & empirical verification:
- Added `tests/cli/test_nested_prefixes_and_scale.cpp` covering dash/dot/slash
  prefix collisions, optional prefix `is_empty` boundaries, exact-key +
  descendant deduplication, `ExtraFields` / `NoExtraFields`, a 36-option wide
  nested config with exact `cli::usage` verification, and 16+ positionals.
- Standalone header checks (`-std=c++20 -Wall -Wextra -Wextra-semi -Werror`,
  plus `-DREFLECTCPP_C_ARRAYS_OR_INHERITANCE`) pass on Clang 23.1.6 and GCC 13.4.0.
- Full CTest suite (778 tests: 777 passed, 1 skipped locale test, 0 failed)
  with `-DREFLECTCPP_STRICT_WARNINGS=ON` passes with 0 warnings on both
  Clang 23.1.6 and GCC 13.4.0.
- Empirical before/after:
  - 32-field CLI struct (`read` + `usage`, Clang `-O0`): targeted option/usage
    symbols dropped 125 -> 6 (257,586 B -> 914 B mangled bytes, -99.65%);
    all `cli::internal` mangled bytes dropped 302,278 B -> 43,493 B (-85.6%)
    on Clang `-O0` and 318,754 B -> 60,592 B (-81.0%) on GCC `-O0`.
  - Nested 200-option `cli::Reader::read_object` (Clang `-O2`):
    depth-5x200 82.21 us -> 20.26 us (4.06x faster),
    wide-10x20 54.26 us -> 20.70 us (2.62x faster),
    4-level tree (256 leaves) 104.75 us -> 46.32 us (2.26x faster).
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