Unverified Commit 2c8fcc5e authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

perf: Cache Rust `Regex`es (#69585)

### What?

Use `Lazy<Regex>` instead of `Regex` if the pattern is known.

### Why?

It's way more efficient.

### How?
parent 9435b7b9
......@@ -226,9 +226,8 @@ fn get_named_parametrized_route(
} else {
None
};
let param_matches = Regex::new(r"\[((?:\[.*\])|.+)\]")
.unwrap()
.captures(segment);
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\[((?:\[.*\])|.+)\]").unwrap());
let param_matches = RE.captures(segment);
if let Some(matches) = param_matches {
return get_safe_key_from_segment(
get_safe_route_key,
......
......@@ -567,7 +567,8 @@ impl ReactServerComponentValidator {
}
fn is_from_node_modules(&self, filepath: &str) -> bool {
Regex::new(r"node_modules[\\/]").unwrap().is_match(filepath)
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"node_modules[\\/]").unwrap());
RE.is_match(filepath)
}
// Asserts the server lib apis
......@@ -681,9 +682,9 @@ impl ReactServerComponentValidator {
if self.is_from_node_modules(&self.filepath) {
return;
}
let is_layout_or_page = Regex::new(r"[\\/](page|layout)\.(ts|js)x?$")
.unwrap()
.is_match(&self.filepath);
static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[\\/](page|layout)\.(ts|js)x?$").unwrap());
let is_layout_or_page = RE.is_match(&self.filepath);
if is_layout_or_page {
let mut span = DUMMY_SP;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment