Tiny optimization for into_vec (#1931)

* Tiny optimization for into_vec

* fix bug
This commit is contained in:
Dominik Maier 2024-03-13 16:52:15 +01:00 committed by GitHub
parent 44aec56f7e
commit 4f3d9d2e50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 8 deletions

View File

@ -175,6 +175,9 @@ pub trait MutatorsTuple<I, S>: HasLen {
corpus_idx: Option<CorpusId>, corpus_idx: Option<CorpusId>,
) -> Result<(), Error>; ) -> Result<(), Error>;
/// Gets all names of the wrapped [`Mutator`]`s`, reversed.
fn names_reversed(&self) -> Vec<&str>;
/// Gets all names of the wrapped [`Mutator`]`s`. /// Gets all names of the wrapped [`Mutator`]`s`.
fn names(&self) -> Vec<&str>; fn names(&self) -> Vec<&str>;
} }
@ -222,6 +225,11 @@ impl<I, S> MutatorsTuple<I, S> for () {
Ok(()) Ok(())
} }
#[inline]
fn names_reversed(&self) -> Vec<&str> {
Vec::new()
}
#[inline] #[inline]
fn names(&self) -> Vec<&str> { fn names(&self) -> Vec<&str> {
Vec::new() Vec::new()
@ -287,9 +295,15 @@ where
} }
} }
fn names_reversed(&self) -> Vec<&str> {
let mut ret = self.1.names_reversed();
ret.push(self.0.name());
ret
}
fn names(&self) -> Vec<&str> { fn names(&self) -> Vec<&str> {
let mut ret = self.1.names(); let mut ret = self.names_reversed();
ret.insert(0, self.0.name()); ret.reverse();
ret ret
} }
} }
@ -299,10 +313,16 @@ where
Head: Mutator<I, S> + 'static, Head: Mutator<I, S> + 'static,
Tail: IntoVec<Box<dyn Mutator<I, S>>>, Tail: IntoVec<Box<dyn Mutator<I, S>>>,
{ {
fn into_vec(self) -> Vec<Box<dyn Mutator<I, S>>> { fn into_vec_reversed(self) -> Vec<Box<dyn Mutator<I, S>>> {
let (head, tail) = self.uncons(); let (head, tail) = self.uncons();
let mut ret = tail.into_vec(); let mut ret = tail.into_vec_reversed();
ret.insert(0, Box::new(head)); ret.push(Box::new(head));
ret
}
fn into_vec(self) -> Vec<Box<dyn Mutator<I, S>>> {
let mut ret = self.into_vec_reversed();
ret.reverse();
ret ret
} }
} }
@ -353,6 +373,10 @@ where
fn names(&self) -> Vec<&str> { fn names(&self) -> Vec<&str> {
self.0.names() self.0.names()
} }
fn names_reversed(&self) -> Vec<&str> {
self.0.names_reversed()
}
} }
impl<Tail, I, S> IntoVec<Box<dyn Mutator<I, S>>> for (Tail,) impl<Tail, I, S> IntoVec<Box<dyn Mutator<I, S>>> for (Tail,)
@ -419,6 +443,10 @@ impl<I, S> MutatorsTuple<I, S> for Vec<Box<dyn Mutator<I, S>>> {
mutator.post_exec(state, stage_idx, new_corpus_idx) mutator.post_exec(state, stage_idx, new_corpus_idx)
} }
fn names_reversed(&self) -> Vec<&str> {
self.iter().rev().map(|x| x.name()).collect()
}
fn names(&self) -> Vec<&str> { fn names(&self) -> Vec<&str> {
self.iter().map(|x| x.name()).collect() self.iter().map(|x| x.name()).collect()
} }

View File

@ -222,10 +222,18 @@ where
Z: UsesState<State = Head::State>, Z: UsesState<State = Head::State>,
Head::State: HasCurrentStage, Head::State: HasCurrentStage,
{ {
fn into_vec(self) -> Vec<Box<dyn Stage<E, EM, Z, State = Head::State, Input = Head::Input>>> { fn into_vec_reversed(
self,
) -> Vec<Box<dyn Stage<E, EM, Z, State = Head::State, Input = Head::Input>>> {
let (head, tail) = self.uncons(); let (head, tail) = self.uncons();
let mut ret = tail.0.into_vec(); let mut ret = tail.0.into_vec_reversed();
ret.insert(0, Box::new(head)); ret.push(Box::new(head));
ret
}
fn into_vec(self) -> Vec<Box<dyn Stage<E, EM, Z, State = Head::State, Input = Head::Input>>> {
let mut ret = self.into_vec_reversed();
ret.reverse();
ret ret
} }
} }

View File

@ -94,6 +94,17 @@ where
/// (We need this trait since we cannot implement `Into` for foreign types) /// (We need this trait since we cannot implement `Into` for foreign types)
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub trait IntoVec<T> { pub trait IntoVec<T> {
/// Convert this into a [`Vec`], reversed.
/// (Having this method around makes some implementations more performant)
fn into_vec_reversed(self) -> Vec<T>
where
Self: Sized,
{
let mut ret = self.into_vec();
ret.reverse();
ret
}
/// Convert this into a [`Vec`]. /// Convert this into a [`Vec`].
fn into_vec(self) -> Vec<T>; fn into_vec(self) -> Vec<T>;
} }