perf: Reduce allocations on 'args'

This seems like it'd have close to the same benefits as the suggestion
in the TODO, resolving it.
This commit is contained in:
Ed Page 2021-10-14 13:04:28 -05:00
parent 294cabf7c6
commit e2865c91be
2 changed files with 9 additions and 2 deletions

View file

@ -1159,8 +1159,10 @@ impl<'help> App<'help> {
I: IntoIterator<Item = T>,
T: Into<Arg<'help>>,
{
// @TODO @perf @p4 @v3-beta: maybe extend_from_slice would be possible and perform better?
// But that may also not let us do `&["-a 'some'", "-b 'other']` because of not Into<Arg>
let args = args.into_iter();
let (lower, _) = args.size_hint();
self.args.reserve(lower);
for arg in args.into_iter() {
self = self.arg(arg);
}

View file

@ -78,6 +78,11 @@ impl<'help> MKeyMap<'help> {
self.keys.iter().any(|x| x.key == key)
}
/// Reserves capacity for at least additional more elements to be inserted
pub(crate) fn reserve(&mut self, additional: usize) {
self.args.reserve(additional);
}
/// Push an argument in the map.
pub(crate) fn push(&mut self, new_arg: Arg<'help>) {
self.args.push(new_arg);