From 45ed622306035249d2e6bc9be5ca9cc5310cb590 Mon Sep 17 00:00:00 2001 From: Bowei Zhang Date: Sun, 21 Sep 2025 22:46:23 -0700 Subject: [PATCH] perf: `lazy_static!` for regexes --- src/matcher.rs | 56 ++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/matcher.rs b/src/matcher.rs index 611b931..4cb275a 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -12,6 +12,7 @@ pub struct MatchResult { pub line_number: Option, } +#[derive(Clone)] struct RegexConfig { regex: Regex, line_number_idx: usize, @@ -52,33 +53,38 @@ impl Matcher for RegexMatcher { } } +lazy_static! { + // TODO(bz): files/paths with spaces + static ref REGEX_CONFIGS: Vec = vec![ + // Homedir paths. ~/a/b/c.ext:123 + RegexConfig { + regex: Regex::new( + r"(~/([a-zA-Z0-9._-]+/)*[a-zA-Z0-9._-]+(\.[a-zA-Z0-9]{1,42})?)[:-]?(\d+)?", + ) + .unwrap(), + line_number_idx: 4, + }, + // Standard paths, w/ or w/o extension. a/b/c.ext:123 + RegexConfig { + regex: Regex::new( + r"(/?([a-zA-Z0-9._-]+/)+[a-zA-Z0-9._-]+(\.[a-zA-Z0-9]{1,42})?)[:-]?(\d+)?", + ) + .unwrap(), + line_number_idx: 4, + }, + // Single file with extension + RegexConfig { + regex: Regex::new(r"(/?[a-zA-Z0-9._-]+\.[a-zA-Z0-9]{1,42})[:-]?(\d+)?").unwrap(), + line_number_idx: 2, + }, + ]; +} + impl RegexMatcher { pub fn new() -> Self { - // TODO(bz): files/paths with spaces - let regex_configs = vec![ - // Homedir paths. ~/a/b/c.ext:123 - RegexConfig { - regex: Regex::new( - r"(~/([a-zA-Z0-9._-]+/)*[a-zA-Z0-9._-]+(\.[a-zA-Z0-9]{1,42})?)[:-]?(\d+)?", - ) - .unwrap(), - line_number_idx: 4, - }, - // Standard paths, w/ or w/o extension. a/b/c.ext:123 - RegexConfig { - regex: Regex::new( - r"(/?([a-zA-Z0-9._-]+/)+[a-zA-Z0-9._-]+(\.[a-zA-Z0-9]{1,42})?)[:-]?(\d+)?", - ) - .unwrap(), - line_number_idx: 4, - }, - // Single file with extension - RegexConfig { - regex: Regex::new(r"(/?[a-zA-Z0-9._-]+\.[a-zA-Z0-9]{1,42})[:-]?(\d+)?").unwrap(), - line_number_idx: 2, - }, - ]; - Self { regex_configs } + Self { + regex_configs: REGEX_CONFIGS.clone(), + } } fn post_processing<'a>(&self, line: &'a str) -> &'a str {