-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apollo-parser: optimises performance of peek_n and peek_data_n (#113)
* query parsing benchmark * avoid allocations in peek_n and peek_data_n
- Loading branch information
Showing
2 changed files
with
45 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![feature(test)] | ||
extern crate test; | ||
use apollo_parser::ast; | ||
use test::{black_box, Bencher}; | ||
|
||
#[bench] | ||
fn bench_peek_n(b: &mut Bencher) { | ||
let query = "query ExampleQuery($topProductsFirst: Int) {\n me { \n id\n }\n topProducts(first: $topProductsFirst) {\n name\n price\n inStock\n weight\n test test test test test test test test test test test test }\n}"; | ||
|
||
b.iter(|| { | ||
let parser = apollo_parser::Parser::new(query); | ||
let tree = parser.parse(); | ||
|
||
if !tree.errors().is_empty() { | ||
panic!("error parsing query: {:?}", tree.errors()); | ||
} | ||
let document = tree.document(); | ||
|
||
for definition in document.definitions() { | ||
if let ast::Definition::OperationDefinition(operation) = definition { | ||
let selection_set = operation | ||
.selection_set() | ||
.expect("the node SelectionSet is not optional in the spec; qed"); | ||
for selection in selection_set.selections() { | ||
match selection { | ||
ast::Selection::Field(field) => { | ||
let _selection_set = field.selection_set(); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters