From: Jakob Cornell Date: Mon, 22 Jan 2024 01:12:52 +0000 (-0600) Subject: Don't repeatedly recompile regular expression in sharedmodel number parsing X-Git-Url: https://jcornell.net/gitweb/gitweb.cgi?a=commitdiff_plain;p=counting.git Don't repeatedly recompile regular expression in sharedmodel number parsing --- diff --git a/sharedmodel/Cargo.toml b/sharedmodel/Cargo.toml index fb0b7fd..0222675 100644 --- a/sharedmodel/Cargo.toml +++ b/sharedmodel/Cargo.toml @@ -19,6 +19,7 @@ uuid = "0.8" html5ever = "0.25" string_cache = "0.8" anyhow = "^1.0.31" +lazy_static = "*" # no need for Unicode support #fancy-regex = { version = "^0.7.0", default-features = false, features = ["perf"] } diff --git a/sharedmodel/src/lib.rs b/sharedmodel/src/lib.rs index a1dc2f4..1568ffa 100644 --- a/sharedmodel/src/lib.rs +++ b/sharedmodel/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate lazy_static; + mod live_event_buffer; mod html; mod html_html5ever; diff --git a/sharedmodel/src/update_parse.rs b/sharedmodel/src/update_parse.rs index 974e75f..6993bca 100644 --- a/sharedmodel/src/update_parse.rs +++ b/sharedmodel/src/update_parse.rs @@ -140,6 +140,12 @@ pub fn parse_update( Ok(parse_from_lines(&stripped_lines, curr_count, bot_user)) } +lazy_static! { + static ref LINE_PATTERN: fancy_regex::Regex = fancy_regex::Regex::new( + "^(?Pv)?(?P-)?(?P[0-9]+((?P[,. \u{2009}]|, )[0-9]+((?P=sep)[0-9]+)*)?)" + ).unwrap(); +} + fn parse_from_lines(lines: &[impl Borrow], curr_count: Option, bot_user: &str) -> ParsedUpdate { let command = lines.iter().find_map(|l| parse_command(l.borrow(), bot_user)); @@ -154,11 +160,8 @@ fn parse_from_lines(lines: &[impl Borrow], curr_count: Option, bot_u } else { // look for groups of ASCII digits (as many as possible) separated by a uniform separator // from the valid set - let pattern = fancy_regex::Regex::new( - "^(?Pv)?(?P-)?(?P[0-9]+((?P[,. \u{2009}]|, )[0-9]+((?P=sep)[0-9]+)*)?)" - ).unwrap(); - let match_result = pattern - .captures(lines[0].borrow()) + let match_result = + LINE_PATTERN.captures(lines[0].borrow()) .with_context(|| format!("parsing line {:?}", lines[0].borrow())) .unwrap(); if let Some(ref match_) = match_result {