Repository navigation
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
Conversation
…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).
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.
Addresses munich-data-engineering/reflect-cpp#692
Problem
rfl::cli::Readerrescanned argument maps linearly across nested prefixes and allocated astd::setper nested object.Meanwhile, option and usage collection instantiated per-field helpers carrying the full$\Theta(F^2)$ symbol bloat and slow builds on wide CLI structs.
NamedTupletype, causingSummary
lower_boundsubtree jumps, drops<set>fromcli/Reader.hpp, and switches usage positional lookups toNamedTupleusingconstexprdescriptor tables while preserving visit order and allstatic_assertdiagnostics.-O3; refl-cpp-workbench#33):cli::internalmangled symbol bytes.rfl::cli::usage1.60x faster andrfl::cli::read1.38x faster.For Agents
upstream-queue(a332dda1) · Score82/100(S=25,M=26,D=18,R=13) ·+197/-121(3f)queue-tests/cli/test_nested_prefixes_and_scale.cpp