Skip to content

Commit

Permalink
use once_cell to compile regex only once
Browse files Browse the repository at this point in the history
Signed-off-by: keisku <[email protected]>
  • Loading branch information
keisku committed Jul 3, 2024
1 parent 04ee13d commit e01daa1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ getset = "0.1.1"
strum = "0.26.2"
strum_macros = "0.26.2"
regex = "1.10.5"
once_cell = "1.19.0"

[dev-dependencies]
tempfile = "3.2.0"
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use regex::Regex;
use serde::{de, Deserialize, Deserializer, Serialize};
use std::path::PathBuf;
use strum_macros::{Display as StrumDisplay, EnumString};
use once_cell::sync::Lazy;

#[derive(
Builder,
Expand Down Expand Up @@ -622,10 +623,12 @@ where
Ok(value)
}

fn validate_cpu_affinity(s: &str) -> Result<(), String> {
let re = Regex::new(r"^(\d+(-\d+)?)(,\d+(-\d+)?)*$").map_err(|e| e.to_string())?;
static CPU_AFFINITY_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(\d+(-\d+)?)(,\d+(-\d+)?)*$").expect("Failed to create regex for execCPUAffinity")
});

if !re.is_match(s) {
fn validate_cpu_affinity(s: &str) -> Result<(), String> {
if !CPU_AFFINITY_REGEX.is_match(s) {
return Err(format!("Invalid CPU affinity format: {}", s));
}

Expand Down

0 comments on commit e01daa1

Please sign in to comment.