From 12868c33be2d16d504869871b607793201ec3257 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 17 Jul 2019 19:50:29 +0200 Subject: Use lazy_static to not recompile regex every time This introduces lazy_static as a new dependency so that the regex does not have to be compiled every time the function is called. Signed-off-by: Matthias Beyer --- Cargo.lock | 1 + Cargo.toml | 1 + src/git.rs | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74352a4..a0d85dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,6 +12,7 @@ dependencies = [ name = "annotate-rust" version = "0.1.0" dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index e229d66..00514e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ regex = "1.1.9" syn = { version = "0.15.39", default-features = false, features = ["full", "parsing", "visit", "extra-traits"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +lazy_static = "1.3" diff --git a/src/git.rs b/src/git.rs index daaceb0..68afd52 100644 --- a/src/git.rs +++ b/src/git.rs @@ -32,13 +32,15 @@ pub fn crawl_git_tree>(path: P) -> io::Result> { } pub fn parse_ls_tree_output<'a>(output: &'a [u8]) -> Result>, Utf8Error> { - let re = Regex::new(r"^[^ ]+ [^ ]+ ([^\t]+)\t(.+)$").unwrap(); + lazy_static::lazy_static! { + static ref RE: Regex = Regex::new(r"^[^ ]+ [^ ]+ ([^\t]+)\t(.+)$").unwrap(); + } output.split(|byte| *byte == 0) .map(str::from_utf8) .filter_map(|r_l| match r_l { Err(e) => Some(Err(e)), - Ok(l) => re.captures(l).map(Ok), + Ok(l) => RE.captures(l).map(Ok), }) .map(|r_captures| r_captures.map(|captures| Blob { object: captures.get(1).unwrap().as_str(), -- cgit v1.2.3