perf: avoid retrieving possible_values unless they're used #5267
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.
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 whenhide_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.