Don't compile regex at every function call.

Use `SyncOnceCell` to only compile it once.
I believe this still adds some kind of locking mechanism?
This commit is contained in:
Hanif Bin Ariffin 2020-09-17 10:56:08 +08:00
parent 285fc7d704
commit f4a7149f49
2 changed files with 10 additions and 1 deletions

View File

@ -1,6 +1,7 @@
//! A helpful diagram for debugging dataflow problems.
use std::borrow::Cow;
use std::lazy::SyncOnceCell;
use std::{io, ops, str};
use regex::Regex;
@ -570,6 +571,13 @@ fn visit_terminator_after_primary_effect(
}
}
macro_rules! regex {
($re:literal $(,)?) => {{
static RE: SyncOnceCell<regex::Regex> = SyncOnceCell::new();
RE.get_or_init(|| Regex::new($re).unwrap())
}};
}
fn diff_pretty<T, C>(new: T, old: T, ctxt: &C) -> String
where
T: DebugWithContext<C>,
@ -578,7 +586,7 @@ fn diff_pretty<T, C>(new: T, old: T, ctxt: &C) -> String
return String::new();
}
let re = Regex::new("\t?\u{001f}([+-])").unwrap();
let re = regex!("\t?\u{001f}([+-])");
let raw_diff = format!("{:#?}", DebugDiffWithAdapter { new, old, ctxt });

View File

@ -27,6 +27,7 @@
#![feature(trait_alias)]
#![feature(option_expect_none)]
#![feature(or_patterns)]
#![feature(once_cell)]
#![recursion_limit = "256"]
#[macro_use]