Skip to content

Commit 3268e33

Browse files
authored
Rollup merge of #76818 - hbina:dont_compile_regex_all_the_time, r=ecstatic-morse
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? Related issue: #76817
2 parents c12feb3 + f4a7149 commit 3268e33

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

compiler/rustc_mir/src/dataflow/framework/graphviz.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A helpful diagram for debugging dataflow problems.
22
33
use std::borrow::Cow;
4+
use std::lazy::SyncOnceCell;
45
use std::{io, ops, str};
56

67
use regex::Regex;
@@ -570,6 +571,13 @@ where
570571
}
571572
}
572573

574+
macro_rules! regex {
575+
($re:literal $(,)?) => {{
576+
static RE: SyncOnceCell<regex::Regex> = SyncOnceCell::new();
577+
RE.get_or_init(|| Regex::new($re).unwrap())
578+
}};
579+
}
580+
573581
fn diff_pretty<T, C>(new: T, old: T, ctxt: &C) -> String
574582
where
575583
T: DebugWithContext<C>,
@@ -578,7 +586,7 @@ where
578586
return String::new();
579587
}
580588

581-
let re = Regex::new("\t?\u{001f}([+-])").unwrap();
589+
let re = regex!("\t?\u{001f}([+-])");
582590

583591
let raw_diff = format!("{:#?}", DebugDiffWithAdapter { new, old, ctxt });
584592

compiler/rustc_mir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Rust MIR: a lowered representation of Rust.
2828
#![feature(trait_alias)]
2929
#![feature(option_expect_none)]
3030
#![feature(or_patterns)]
31+
#![feature(once_cell)]
3132
#![recursion_limit = "256"]
3233

3334
#[macro_use]

0 commit comments

Comments
 (0)