Skip to content

Commit b53a0ed

Browse files
committed
Make regex static
We were compiling this in each loop. On a large codebase, this contributed to ~10% of the time taken to build the graph.
1 parent 395c656 commit b53a0ed

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

rust/src/filesystem.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ use std::ffi::OsStr;
66
use std::fs;
77
use std::path::{Path, PathBuf};
88
use unindent::unindent;
9+
use lazy_static::lazy_static;
10+
11+
12+
lazy_static! {
13+
static ref ENCODING_RE: Regex = Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap();
14+
}
915

1016
pub trait FileSystem: Send + Sync {
1117
fn sep(&self) -> String;
@@ -81,13 +87,13 @@ impl FileSystem for RealBasicFileSystem {
8187
})?;
8288

8389
let s = String::from_utf8_lossy(&bytes);
84-
let encoding_re = Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)").unwrap();
90+
8591

8692
let mut detected_encoding: Option<String> = None;
8793

8894
// Coding specification needs to be in the first two lines, or it's ignored.
8995
for line in s.lines().take(2) {
90-
if let Some(captures) = encoding_re.captures(line)
96+
if let Some(captures) = ENCODING_RE.captures(line)
9197
&& let Some(encoding_name) = captures.get(1) {
9298
detected_encoding = Some(encoding_name.as_str().to_string());
9399
break;

0 commit comments

Comments
 (0)