Speeds up sparql_eval fuzzer by hardcoding size_hint

For some reasons size_hint() computation takes a lot of time.

Hardcoding allows to increase the iter speed from 0.2iter/s to 1250iter/s
pull/510/head
Tpt 2 years ago committed by Thomas Tanon
parent 7b9e9f9694
commit 2650c5ed13
  1. 31
      lib/sparql-smith/src/lib.rs

@ -1,6 +1,5 @@
use arbitrary::{Arbitrary, Result, Unstructured}; use arbitrary::{Arbitrary, Result, Unstructured};
use std::fmt; use std::fmt;
use std::fmt::Debug;
use std::iter::once; use std::iter::once;
use std::ops::ControlFlow; use std::ops::ControlFlow;
@ -30,8 +29,12 @@ const LITERALS: [&str; 11] = [
"1e0", "1e0",
]; ];
#[derive(Arbitrary)]
pub struct Query { pub struct Query {
inner: QueryContent,
}
#[derive(Arbitrary)]
struct QueryContent {
// [1] QueryUnit ::= Query // [1] QueryUnit ::= Query
// [2] Query ::= Prologue ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery ) ValuesClause // [2] Query ::= Prologue ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery ) ValuesClause
variant: QueryVariant, variant: QueryVariant,
@ -44,16 +47,34 @@ enum QueryVariant {
//TODO: Other variants! //TODO: Other variants!
} }
impl<'a> Arbitrary<'a> for Query {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Ok(Self {
inner: QueryContent::arbitrary(u)?,
})
}
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
Ok(Self {
inner: QueryContent::arbitrary_take_rest(u)?,
})
}
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(20, None)
}
}
impl fmt::Display for Query { impl fmt::Display for Query {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.variant { match &self.inner.variant {
QueryVariant::Select(s) => write!(f, "{s}"), QueryVariant::Select(s) => write!(f, "{s}"),
}?; }?;
write!(f, "{}", self.values_clause) write!(f, "{}", self.inner.values_clause)
} }
} }
impl Debug for Query { impl fmt::Debug for Query {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f) fmt::Display::fmt(self, f)
} }

Loading…
Cancel
Save