Skip to content

Commit a9d5381

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 a9d5381

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

rust/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ serde = { version = "1.0", features = ["derive"] }
2929
serde_yaml = "0.9"
3030
unindent = "0.2.4"
3131
encoding_rs = "0.8.35"
32+
once_cell = "1.21.3"
3233

3334
[dependencies.pyo3]
3435
version = "0.24.1"

rust/src/filesystem.rs

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

1015
pub trait FileSystem: Send + Sync {
1116
fn sep(&self) -> String;
@@ -81,13 +86,13 @@ impl FileSystem for RealBasicFileSystem {
8186
})?;
8287

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

8691
let mut detected_encoding: Option<String> = None;
8792

8893
// Coding specification needs to be in the first two lines, or it's ignored.
8994
for line in s.lines().take(2) {
90-
if let Some(captures) = encoding_re.captures(line)
95+
if let Some(captures) = ENCODING_RE.captures(line)
9196
&& let Some(encoding_name) = captures.get(1) {
9297
detected_encoding = Some(encoding_name.as_str().to_string());
9398
break;

0 commit comments

Comments
 (0)