From 7a5796d3f1c3c39c9c3dbed9dfc7d258b2bb6c06 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Wed, 5 Jan 2022 06:40:46 +0100 Subject: [PATCH] use lazy_static to compile `Regex::new` instances once instead on each test --- src/tools/compiletest/src/runtest.rs | 63 ++++++++++++++-------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index c8c2fa2e7d9ad..d06e48df11b52 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -3486,10 +3486,12 @@ impl<'test> TestCx<'test> { // with placeholders as we do not want tests needing updated when compiler source code // changes. // eg. $SRC_DIR/libcore/mem.rs:323:14 becomes $SRC_DIR/libcore/mem.rs:LL:COL - normalized = Regex::new("SRC_DIR(.+):\\d+:\\d+(: \\d+:\\d+)?") - .unwrap() - .replace_all(&normalized, "SRC_DIR$1:LL:COL") - .into_owned(); + lazy_static! { + static ref SRC_DIR_RE: Regex = + Regex::new("SRC_DIR(.+):\\d+:\\d+(: \\d+:\\d+)?").unwrap(); + } + + normalized = SRC_DIR_RE.replace_all(&normalized, "SRC_DIR$1:LL:COL").into_owned(); normalized = Self::normalize_platform_differences(&normalized); normalized = normalized.replace("\t", "\\t"); // makes tabs visible @@ -3498,38 +3500,37 @@ impl<'test> TestCx<'test> { // since they duplicate actual errors and make the output hard to read. // This mirrors the regex in src/tools/tidy/src/style.rs, please update // both if either are changed. - normalized = - Regex::new("\\s*//(\\[.*\\])?~.*").unwrap().replace_all(&normalized, "").into_owned(); + lazy_static! { + static ref ANNOTATION_RE: Regex = Regex::new("\\s*//(\\[.*\\])?~.*").unwrap(); + } + + normalized = ANNOTATION_RE.replace_all(&normalized, "").into_owned(); + + // This code normalizes various hashes in v0 symbol mangling that is + // emitted in the ui and mir-opt tests. + lazy_static! { + static ref V0_CRATE_HASH_PREFIX_RE: Regex = + Regex::new(r"_R.*?Cs[0-9a-zA-Z]+_").unwrap(); + static ref V0_CRATE_HASH_RE: Regex = Regex::new(r"Cs[0-9a-zA-Z]+_").unwrap(); + } - // This code normalizes various hashes in both - // v0 and legacy symbol names that are emitted in - // the ui and mir-opt tests. - // - // Some tests still require normalization with headers. - const V0_CRATE_HASH_PREFIX_REGEX: &str = r"_R.*?Cs[0-9a-zA-Z]+_"; - const V0_CRATE_HASH_REGEX: &str = r"Cs[0-9a-zA-Z]+_"; const V0_CRATE_HASH_PLACEHOLDER: &str = r"CsCRATE_HASH_"; - const V0_BACK_REF_PREFIX_REGEX: &str = r"\(_R.*?B[0-9a-zA-Z]_"; - const V0_BACK_REF_REGEX: &str = r"B[0-9a-zA-Z]_"; - const V0_BACK_REF_PLACEHOLDER: &str = r"B_"; - // Normalize v0 crate hashes (see RFC 2603) - let symbol_mangle_prefix_re = Regex::new(V0_CRATE_HASH_PREFIX_REGEX).unwrap(); - if symbol_mangle_prefix_re.is_match(&normalized) { + if V0_CRATE_HASH_PREFIX_RE.is_match(&normalized) { // Normalize crate hash - normalized = Regex::new(V0_CRATE_HASH_REGEX) - .unwrap() - .replace_all(&normalized, V0_CRATE_HASH_PLACEHOLDER) - .into_owned(); + normalized = + V0_CRATE_HASH_RE.replace_all(&normalized, V0_CRATE_HASH_PLACEHOLDER).into_owned(); } - let back_ref_prefix_re = Regex::new(V0_BACK_REF_PREFIX_REGEX).unwrap(); - if back_ref_prefix_re.is_match(&normalized) { + + lazy_static! { + static ref V0_BACK_REF_PREFIX_RE: Regex = Regex::new(r"\(_R.*?B[0-9a-zA-Z]_").unwrap(); + static ref V0_BACK_REF_RE: Regex = Regex::new(r"B[0-9a-zA-Z]_").unwrap(); + } + + const V0_BACK_REF_PLACEHOLDER: &str = r"B_"; + if V0_BACK_REF_PREFIX_RE.is_match(&normalized) { // Normalize back references (see RFC 2603) - let back_ref_regex = format!("{}", V0_BACK_REF_REGEX); - let back_ref_placeholder = format!("{}", V0_BACK_REF_PLACEHOLDER); - normalized = Regex::new(&back_ref_regex) - .unwrap() - .replace_all(&normalized, back_ref_placeholder) - .into_owned(); + normalized = + V0_BACK_REF_RE.replace_all(&normalized, V0_BACK_REF_PLACEHOLDER).into_owned(); } // Custom normalization rules