From 2650c5ed1338157cfcba414b44b740428c7d4fbe Mon Sep 17 00:00:00 2001 From: Tpt Date: Wed, 24 May 2023 11:14:15 +0200 Subject: [PATCH] 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 --- lib/sparql-smith/src/lib.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/sparql-smith/src/lib.rs b/lib/sparql-smith/src/lib.rs index 6b554ec7..6005d827 100644 --- a/lib/sparql-smith/src/lib.rs +++ b/lib/sparql-smith/src/lib.rs @@ -1,6 +1,5 @@ use arbitrary::{Arbitrary, Result, Unstructured}; use std::fmt; -use std::fmt::Debug; use std::iter::once; use std::ops::ControlFlow; @@ -30,8 +29,12 @@ const LITERALS: [&str; 11] = [ "1e0", ]; -#[derive(Arbitrary)] pub struct Query { + inner: QueryContent, +} + +#[derive(Arbitrary)] +struct QueryContent { // [1] QueryUnit ::= Query // [2] Query ::= Prologue ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery ) ValuesClause variant: QueryVariant, @@ -44,16 +47,34 @@ enum QueryVariant { //TODO: Other variants! } +impl<'a> Arbitrary<'a> for Query { + fn arbitrary(u: &mut Unstructured<'a>) -> Result { + Ok(Self { + inner: QueryContent::arbitrary(u)?, + }) + } + + fn arbitrary_take_rest(u: Unstructured<'a>) -> Result { + Ok(Self { + inner: QueryContent::arbitrary_take_rest(u)?, + }) + } + + fn size_hint(_depth: usize) -> (usize, Option) { + (20, None) + } +} + impl fmt::Display for Query { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match &self.variant { + match &self.inner.variant { 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 { fmt::Display::fmt(self, f) }