Skip to content

Commit

Permalink
refactor(parser): Share a parser across calls
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 28, 2019
1 parent 36fefc1 commit 3e678cc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
18 changes: 12 additions & 6 deletions benches/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -284,7 +288,7 @@ fn run() -> Result<i32, failure::Error> {
&dictionary,
check_filenames,
check_files,
ignore_hex,
&parser,
binary,
options.format.report(),
)? {
Expand Down

0 comments on commit 3e678cc

Please sign in to comment.