Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid retrieving possible_values unless they're used #5267

Merged
merged 1 commit into from Dec 28, 2023

Conversation

vermiculus
Copy link
Contributor

@vermiculus vermiculus commented Dec 24, 2023

In some sophisticated situations, these may be expensive to calculate. One example might be a '--branch' option accepting any single Git branch that exists on the remote -- in such a case, the remote would need to be queried for all possible_values. The cost is ultimately unavoidable at runtime since this validation has to happen eventually, but there's no need to pay it when generating help text if is_hide_possible_values_set.

To keep '-h' fast, avoid collecting possible_values during '-h' unless we're actually going to use the values in display.

This optimization is repeated for the manpage renderer.

This is trivially based on the short-circuiting logic at clap_builder/src/builder/command.rs:long_help_exists_, which at least supports the idea that actually consuming the iterator is not generally-guaranteed behavior when hide_possible_values is set.


This seems like an obvious bug 'to me', but I recognize that there might be some angle to this that warrants discussion. If so, let me know and we can put this PR on ice while discussion happens in a tracking issue.

@vermiculus vermiculus marked this pull request as draft December 24, 2023 15:52
@vermiculus vermiculus force-pushed the sa/avoid-pv-expansion-in-help branch 4 times, most recently from a819532 to eae1ee2 Compare December 24, 2023 17:14
@vermiculus vermiculus marked this pull request as ready for review December 24, 2023 17:17
tests/builder/main.rs Outdated Show resolved Hide resolved
@epage
Copy link
Member

epage commented Dec 27, 2023

This seems like an obvious bug 'to me', but I recognize that there might be some angle to this that warrants discussion. If so, let me know and we can put this PR on ice while discussion happens in a tracking issue.

I will say that the general assumption is Possible Values are fast to look up. I'm ok with this optimization being "best effort" but not willing to guarantee we won't look up possible values if they are hidden. So long as you understand that you'll be playing wack-a-mole with this as new problems crop up, I'm fine moving forward with it.

@vermiculus
Copy link
Contributor Author

Whack-a-mole seems fine; I can certainly own the fixes to any problems in this space (at least by default). As long as the unit test remains in place :-)

In some sophisticated situations, these may be expensive to calculate.
One example might be a '--branch' option accepting any single Git
branch that exists on the remote -- in such a case, the remote would
need to be queried for all possible_values. The cost is ultimately
unavoidable at runtime since this validation has to happen eventually,
but there's no need to pay it when generating help text if
`is_hide_possible_values_set`.

To keep '-h' fast, avoid collecting `possible_values` during '-h'
unless we're actually going to use the values in display.

This optimization is repeated for the manpage renderer.

This is trivially based on the short-circuiting logic at [1], which at
least supports the idea that actually consuming the iterator is not
generally-guaranteed behavior when `hide_possible_values` is set.

Note on the 'expensive' mod: This keeps all the possible_values tests
in one file but allows the entire set of tests to be controlled by the
'strings' feature (which is required to be able to use String rather
than str for each possible value).

[1]: clap_builder/src/builder/command.rs:long_help_exists_
@epage epage merged commit 53f5b82 into clap-rs:master Dec 28, 2023
22 checks passed
@vermiculus vermiculus deleted the sa/avoid-pv-expansion-in-help branch December 28, 2023 19:12
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.

None yet

2 participants