libafl: with_capacity method for NewHashFeedback (#1034)
Most of the time, fuzzing campaigns are reasonably long. Accordingly, when using `NewHashFeedback`, you might have good reason to believe that you'll find thousands (or more) different observations with different hashes. When the `HashSet` outgrows its capacity, it can cause reallocation, which is slow. See the following link for more details: https://doc.rust-lang.org/std/vec/struct.Vec.html#capacity-and-reallocation
This commit is contained in:
parent
5d76707ede
commit
e75f65080e
@ -45,6 +45,14 @@ impl NewHashFeedbackMetadata {
|
|||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new [`NewHashFeedbackMetadata`] with the given initial capacity
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
hash_set: HashSet::with_capacity(capacity),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reset the internal state
|
/// Reset the internal state
|
||||||
pub fn reset(&mut self) -> Result<(), Error> {
|
pub fn reset(&mut self) -> Result<(), Error> {
|
||||||
self.hash_set.clear();
|
self.hash_set.clear();
|
||||||
@ -71,6 +79,8 @@ impl HashSetState<u64> for NewHashFeedbackMetadata {
|
|||||||
pub struct NewHashFeedback<O, S> {
|
pub struct NewHashFeedback<O, S> {
|
||||||
name: String,
|
name: String,
|
||||||
observer_name: String,
|
observer_name: String,
|
||||||
|
/// Initial capacity of hash set
|
||||||
|
capacity: usize,
|
||||||
o_type: PhantomData<(O, S)>,
|
o_type: PhantomData<(O, S)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +90,10 @@ where
|
|||||||
S: UsesInput + Debug + HasNamedMetadata + HasClientPerfMonitor,
|
S: UsesInput + Debug + HasNamedMetadata + HasClientPerfMonitor,
|
||||||
{
|
{
|
||||||
fn init_state(&mut self, state: &mut S) -> Result<(), Error> {
|
fn init_state(&mut self, state: &mut S) -> Result<(), Error> {
|
||||||
state.add_named_metadata(NewHashFeedbackMetadata::default(), &self.name);
|
state.add_named_metadata(
|
||||||
|
NewHashFeedbackMetadata::with_capacity(self.capacity),
|
||||||
|
&self.name,
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,6 +159,7 @@ where
|
|||||||
Self {
|
Self {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
observer_name: observer_name.to_string(),
|
observer_name: observer_name.to_string(),
|
||||||
|
capacity: 0,
|
||||||
o_type: PhantomData,
|
o_type: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,9 +167,17 @@ where
|
|||||||
/// Returns a new [`NewHashFeedback`].
|
/// Returns a new [`NewHashFeedback`].
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new(observer: &O) -> Self {
|
pub fn new(observer: &O) -> Self {
|
||||||
|
Self::with_capacity(observer, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a new [`NewHashFeedback`] that will create a hash set with the
|
||||||
|
/// given initial capacity.
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_capacity(observer: &O, capacity: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: NEWHASHFEEDBACK_PREFIX.to_string() + observer.name(),
|
name: NEWHASHFEEDBACK_PREFIX.to_string() + observer.name(),
|
||||||
observer_name: observer.name().to_string(),
|
observer_name: observer.name().to_string(),
|
||||||
|
capacity,
|
||||||
o_type: PhantomData,
|
o_type: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user