From e34c0ccf4ce3f39939826f357f8084bcd7cdf6da Mon Sep 17 00:00:00 2001 From: ProofOfConcept Date: Tue, 3 Mar 2026 12:44:02 -0500 Subject: [PATCH] capnp_store: cache compiled regexes with OnceLock parse_units and parse_marker_attrs were recompiling 4 regexes on every call. Since they're called per-file during init, this was measurable overhead. Use std::sync::OnceLock to compile once. --- src/capnp_store.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/capnp_store.rs b/src/capnp_store.rs index 443862b..af1dc61 100644 --- a/src/capnp_store.rs +++ b/src/capnp_store.rs @@ -1606,11 +1606,18 @@ pub struct MemoryUnit { } pub fn parse_units(filename: &str, content: &str) -> Vec { - let marker_re = Regex::new( - r"" - ).unwrap(); - let source_re = Regex::new(r"").unwrap(); - let md_link_re = Regex::new(r"\[[^\]]*\]\(([^)]*\.md(?:#[^)]*)?)\)").unwrap(); + use std::sync::OnceLock; + + static MARKER_RE: OnceLock = OnceLock::new(); + static SOURCE_RE: OnceLock = OnceLock::new(); + static MD_LINK_RE: OnceLock = OnceLock::new(); + + let marker_re = MARKER_RE.get_or_init(|| + Regex::new(r"").unwrap()); + let source_re = SOURCE_RE.get_or_init(|| + Regex::new(r"").unwrap()); + let md_link_re = MD_LINK_RE.get_or_init(|| + Regex::new(r"\[[^\]]*\]\(([^)]*\.md(?:#[^)]*)?)\)").unwrap()); let markers: Vec<_> = marker_re.captures_iter(content) .map(|cap| { @@ -1699,7 +1706,9 @@ pub fn parse_units(filename: &str, content: &str) -> Vec { } fn parse_marker_attrs(attrs_str: &str) -> HashMap { - let attr_re = Regex::new(r"(\w+)\s*=\s*(\S+)").unwrap(); + use std::sync::OnceLock; + static ATTR_RE: OnceLock = OnceLock::new(); + let attr_re = ATTR_RE.get_or_init(|| Regex::new(r"(\w+)\s*=\s*(\S+)").unwrap()); let mut attrs = HashMap::new(); for cap in attr_re.captures_iter(attrs_str) { attrs.insert(cap[1].to_string(), cap[2].to_string());