From df4bc13018b893213e611e46fbad4a62039f3a99 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 20 Feb 2016 21:06:47 +0100 Subject: [PATCH 1/2] use lazy_static so we do not compile regex multiple times --- libimagstore/Cargo.toml | 1 + libimagstore/src/lib.rs | 1 + libimagstore/src/store.rs | 16 +++++++++------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/libimagstore/Cargo.toml b/libimagstore/Cargo.toml index 624e129e..b649cae6 100644 --- a/libimagstore/Cargo.toml +++ b/libimagstore/Cargo.toml @@ -6,6 +6,7 @@ authors = ["Matthias Beyer "] [dependencies] fs2 = "0.2.2" glob = "0.2.10" +lazy_static = "0.1.15" log = "0.3.5" regex = "0.1.47" semver = "0.2" diff --git a/libimagstore/src/lib.rs b/libimagstore/src/lib.rs index 97b7a52c..d889f286 100644 --- a/libimagstore/src/lib.rs +++ b/libimagstore/src/lib.rs @@ -2,6 +2,7 @@ #[macro_use] extern crate version; extern crate fs2; extern crate glob; +#[macro_use] extern crate lazy_static; extern crate regex; extern crate toml; #[cfg(test)] extern crate tempdir; diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 39c3ccb3..16121b62 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -870,14 +870,16 @@ impl Entry { pub fn from_str(loc: StoreId, s: &str) -> Result { debug!("Building entry from string"); - let re = Regex::new(r"(?smx) - ^---$ - (?P
.*) # Header - ^---$\n - (?P.*) # Content - ").unwrap(); + lazy_static! { + static ref RE: Regex = Regex::new(r"(?smx) + ^---$ + (?P
.*) # Header + ^---$\n + (?P.*) # Content + ").unwrap(); + } - let matches = re.captures(s); + let matches = RE.captures(s); if matches.is_none() { return Err(StoreError::new(StoreErrorKind::MalformedEntry, None)); From 1b8838bf1f37179387b966cb10d891a4b2abc5cb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 20 Feb 2016 21:10:22 +0100 Subject: [PATCH 2/2] use lazy_static so we do not compile regex multiple times --- libimagutil/Cargo.toml | 1 + libimagutil/src/key_value_split.rs | 15 +++++++++------ libimagutil/src/lib.rs | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index 6e442b18..3e7d82f8 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] +lazy_static = "0.1.15" log = "0.3.5" regex = "0.1.47" diff --git a/libimagutil/src/key_value_split.rs b/libimagutil/src/key_value_split.rs index 796f0c82..b5e2790e 100644 --- a/libimagutil/src/key_value_split.rs +++ b/libimagutil/src/key_value_split.rs @@ -43,16 +43,19 @@ impl IntoKeyValue for String { fn into_kv(self) -> Option> { let key = { - let r = "^(?P([^=]*))=(.*)$"; - let r = Regex::new(r).unwrap(); - r.captures(&self[..]) + lazy_static! { + static ref R: Regex = Regex::new("^(?P([^=]*))=(.*)$").unwrap(); + } + R.captures(&self[..]) .and_then(|caps| caps.name("KEY")) }; let value = { - let r = "(.*)=(\"(?P([^\"]*))\"|(?P(.*)))$"; - let r = Regex::new(r).unwrap(); - r.captures(&self[..]) + lazy_static! { + static ref R: Regex = Regex::new("(.*)=(\"(?P([^\"]*))\"|(?P(.*)))$") + .unwrap(); + } + R.captures(&self[..]) .map(|caps| { caps.name("VALUE") .or(caps.name("QVALUE")) diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index a7586143..0b74fc2d 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -1,3 +1,4 @@ +#[macro_use] extern crate lazy_static; #[macro_use] extern crate log; extern crate regex;