Skip to content

Commit

Permalink
Rollup merge of rust-lang#76818 - hbina:dont_compile_regex_all_the_ti…
Browse files Browse the repository at this point in the history
…me, 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: rust-lang#76817
  • Loading branch information
RalfJung authored Sep 20, 2020
2 parents e6de86d + f4a7149 commit eda93f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion compiler/rustc_mir/src/dataflow/framework/graphviz.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -570,6 +571,13 @@ where
}
}

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>,
Expand All @@ -578,7 +586,7 @@ where
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 });

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Rust MIR: a lowered representation of Rust.
#![feature(trait_alias)]
#![feature(option_expect_none)]
#![feature(or_patterns)]
#![feature(once_cell)]
#![recursion_limit = "256"]

#[macro_use]
Expand Down

0 comments on commit eda93f9

Please sign in to comment.