refactor(parser): Share a parser across calls

This commit is contained in:
Ed Page 2019-07-25 07:45:16 -06:00
parent 36fefc166e
commit 3e678cca1e
3 changed files with 18 additions and 9 deletions

View file

@ -13,13 +13,14 @@ fn process_empty(b: &mut test::Bencher) {
sample_path.write_str(data::EMPTY).unwrap();
let corrections = typos::Dictionary::new();
let parser = typos::tokens::Parser::new();
b.iter(|| {
typos::process_file(
sample_path.path(),
&corrections,
true,
true,
true,
&parser,
false,
typos::report::print_silent,
)
@ -35,13 +36,14 @@ fn process_no_tokens(b: &mut test::Bencher) {
sample_path.write_str(data::NO_TOKENS).unwrap();
let corrections = typos::Dictionary::new();
let parser = typos::tokens::Parser::new();
b.iter(|| {
typos::process_file(
sample_path.path(),
&corrections,
true,
true,
true,
&parser,
false,
typos::report::print_silent,
)
@ -57,13 +59,14 @@ fn process_single_token(b: &mut test::Bencher) {
sample_path.write_str(data::SINGLE_TOKEN).unwrap();
let corrections = typos::Dictionary::new();
let parser = typos::tokens::Parser::new();
b.iter(|| {
typos::process_file(
sample_path.path(),
&corrections,
true,
true,
true,
&parser,
false,
typos::report::print_silent,
)
@ -79,13 +82,14 @@ fn process_sherlock(b: &mut test::Bencher) {
sample_path.write_str(data::SHERLOCK).unwrap();
let corrections = typos::Dictionary::new();
let parser = typos::tokens::Parser::new();
b.iter(|| {
typos::process_file(
sample_path.path(),
&corrections,
true,
true,
true,
&parser,
false,
typos::report::print_silent,
)
@ -101,13 +105,14 @@ fn process_code(b: &mut test::Bencher) {
sample_path.write_str(data::CODE).unwrap();
let corrections = typos::Dictionary::new();
let parser = typos::tokens::Parser::new();
b.iter(|| {
typos::process_file(
sample_path.path(),
&corrections,
true,
true,
true,
&parser,
false,
typos::report::print_silent,
)
@ -123,13 +128,14 @@ fn process_corpus(b: &mut test::Bencher) {
sample_path.write_str(data::CORPUS).unwrap();
let corrections = typos::Dictionary::new();
let parser = typos::tokens::Parser::new();
b.iter(|| {
typos::process_file(
sample_path.path(),
&corrections,
true,
true,
true,
&parser,
false,
typos::report::print_silent,
)

View file

@ -19,11 +19,10 @@ pub fn process_file(
dictionary: &Dictionary,
check_filenames: bool,
check_files: bool,
ignore_hex: bool,
parser: &tokens::Parser,
binary: bool,
report: report::Report,
) -> Result<bool, failure::Error> {
let parser = tokens::ParserBuilder::new().ignore_hex(ignore_hex).build();
let mut typos_found = false;
if check_filenames {

View file

@ -261,6 +261,10 @@ fn run() -> Result<i32, failure::Error> {
let ignore_hex = options.ignore_hex().unwrap_or(true);
let binary = options.binary().unwrap_or(false);
let parser = typos::tokens::ParserBuilder::new()
.ignore_hex(ignore_hex)
.build();
let first_path = &options
.path
.get(0)
@ -284,7 +288,7 @@ fn run() -> Result<i32, failure::Error> {
&dictionary,
check_filenames,
check_files,
ignore_hex,
&parser,
binary,
options.format.report(),
)? {