Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
Don't re-use parsing state, pre-warm regex cache
Browse files Browse the repository at this point in the history
  • Loading branch information
trishume committed Jun 29, 2016
1 parent 5b4a550 commit 11ac7b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
19 changes: 12 additions & 7 deletions benches/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ extern crate test;
extern crate syntect;
use test::Bencher;

use syntect::parsing::SyntaxSet;
use syntect::highlighting::ThemeSet;
use syntect::parsing::{SyntaxSet, SyntaxDefinition};
use syntect::highlighting::{ThemeSet, Theme};
use syntect::easy::HighlightLines;
use std::fs::File;
use std::io::Read;

fn do_highlight(s: &str, syntax: &SyntaxDefinition, theme: &Theme) {
let mut h = HighlightLines::new(syntax, theme);
for line in s.lines() {
let regions = h.highlight(line);
test::black_box(&regions);
}
}

fn highlight_file(b: &mut Bencher, path_s: &str) {
// don't load from dump so we don't count lazy regex compilation time
let ps = SyntaxSet::load_defaults_nonewlines();
Expand All @@ -20,12 +28,9 @@ fn highlight_file(b: &mut Bencher, path_s: &str) {
let mut s = String::new();
f.read_to_string(&mut s).unwrap();

let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
do_highlight(&s, syntax, &ts.themes["base16-ocean.dark"]);
b.iter(|| {
for line in s.lines() {
let regions = h.highlight(line);
test::black_box(&regions);
}
do_highlight(&s, syntax, &ts.themes["base16-ocean.dark"]);
});
}

Expand Down
17 changes: 11 additions & 6 deletions benches/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ extern crate test;
extern crate syntect;
use test::Bencher;

use syntect::parsing::{SyntaxSet, ParseState};
use syntect::parsing::{SyntaxSet, ParseState, SyntaxDefinition};
use std::fs::File;
use std::io::Read;

fn do_parse(s: &str, syntax: &SyntaxDefinition) {
let mut state = ParseState::new(syntax);
for line in s.lines() {
let ops = state.parse_line(line);
test::black_box(&ops);
}
}

fn parse_file(b: &mut Bencher, path_s: &str) {
// don't load from dump so we don't count lazy regex compilation time
let ps = SyntaxSet::load_defaults_nonewlines();
Expand All @@ -17,12 +25,9 @@ fn parse_file(b: &mut Bencher, path_s: &str) {
let mut s = String::new();
f.read_to_string(&mut s).unwrap();

let mut state = ParseState::new(syntax);
do_parse(&s, syntax);
b.iter(|| {
for line in s.lines() {
let ops = state.parse_line(line);
test::black_box(&ops);
}
do_parse(&s, syntax);
});
}

Expand Down

0 comments on commit 11ac7b4

Please sign in to comment.