Skip to content

Commit 959a2eb

Browse files
committed
Auto merge of #127455 - Nilstrieb:blazing-tidy, r=jieyouxu
Make tidy problematic const checking fast again fixes pathological tidy performance described in #127453 by reverting #127428 i think anyone can approve this ASAP, it makes working on this repo significantly worse.
2 parents 382148d + 1cfc89a commit 959a2eb

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/tools/tidy/src/style.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
// ignore-tidy-dbg
1919

2020
use crate::walk::{filter_dirs, walk};
21+
use regex::RegexSet;
2122
use rustc_hash::FxHashMap;
22-
use std::{ffi::OsStr, path::Path, sync::LazyLock};
23+
use std::{ffi::OsStr, path::Path};
2324

2425
#[cfg(test)]
2526
mod tests;
@@ -109,32 +110,16 @@ const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
109110
173390526, 721077,
110111
];
111112

112-
#[cfg(not(test))]
113-
const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3')];
114-
115-
#[cfg(test)]
116-
const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3'), ('0', 'F')]; // use "futile" F intentionally
117-
113+
// Returns all permutations of problematic consts, over 2000 elements.
118114
fn generate_problematic_strings(
119115
consts: &[u32],
120116
letter_digit: &FxHashMap<char, char>,
121117
) -> Vec<String> {
122118
generate_problems(consts, letter_digit)
123-
.flat_map(|v| vec![v.to_string(), format!("{:X}", v)])
119+
.flat_map(|v| vec![v.to_string(), format!("{:x}", v), format!("{:X}", v)])
124120
.collect()
125121
}
126122

127-
static PROBLEMATIC_CONSTS_STRINGS: LazyLock<Vec<String>> = LazyLock::new(|| {
128-
generate_problematic_strings(
129-
ROOT_PROBLEMATIC_CONSTS,
130-
&FxHashMap::from_iter(LETTER_DIGIT.iter().copied()),
131-
)
132-
});
133-
134-
fn contains_problematic_const(trimmed: &str) -> bool {
135-
PROBLEMATIC_CONSTS_STRINGS.iter().any(|s| trimmed.to_uppercase().contains(s))
136-
}
137-
138123
const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
139124

140125
/// Parser states for `line_is_url`.
@@ -331,6 +316,13 @@ pub fn check(path: &Path, bad: &mut bool) {
331316
// We only check CSS files in rustdoc.
332317
path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
333318
}
319+
let problematic_consts_strings = generate_problematic_strings(
320+
ROOT_PROBLEMATIC_CONSTS,
321+
&[('A', '4'), ('B', '8'), ('E', '3')].iter().cloned().collect(),
322+
);
323+
// This creates a RegexSet as regex contains performance optimizations to be able to deal with these over
324+
// 2000 needles efficiently. This runs over the entire source code, so performance matters.
325+
let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
334326

335327
walk(path, skip, &mut |entry, contents| {
336328
let file = entry.path();
@@ -400,6 +392,7 @@ pub fn check(path: &Path, bad: &mut bool) {
400392
let is_test = file.components().any(|c| c.as_os_str() == "tests");
401393
// scanning the whole file for multiple needles at once is more efficient than
402394
// executing lines times needles separate searches.
395+
let any_problematic_line = problematic_regex.is_match(contents);
403396
for (i, line) in contents.split('\n').enumerate() {
404397
if line.is_empty() {
405398
if i == 0 {
@@ -469,8 +462,12 @@ pub fn check(path: &Path, bad: &mut bool) {
469462
if trimmed.contains("//") && trimmed.contains(" XXX") {
470463
err("Instead of XXX use FIXME")
471464
}
472-
if contains_problematic_const(trimmed) {
473-
err("Don't use magic numbers that spell things (consider 0x12345678)");
465+
if any_problematic_line {
466+
for s in problematic_consts_strings.iter() {
467+
if trimmed.contains(s) {
468+
err("Don't use magic numbers that spell things (consider 0x12345678)");
469+
}
470+
}
474471
}
475472
}
476473
// for now we just check libcore

src/tools/tidy/src/style/tests.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
use super::*;
22

33
#[test]
4-
fn test_contains_problematic_const() {
5-
assert!(contains_problematic_const("786357")); // check with no "decimal" hex digits - converted to integer
6-
assert!(contains_problematic_const("589701")); // check with "decimal" replacements - converted to integer
7-
assert!(contains_problematic_const("8FF85")); // check for hex display
8-
assert!(contains_problematic_const("8fF85")); // check for case-alternating hex display
9-
assert!(!contains_problematic_const("1193046")); // check for non-matching value
4+
fn test_generate_problematic_strings() {
5+
let problematic_regex = RegexSet::new(
6+
generate_problematic_strings(
7+
ROOT_PROBLEMATIC_CONSTS,
8+
&[('A', '4'), ('B', '8'), ('E', '3'), ('0', 'F')].iter().cloned().collect(), // use "futile" F intentionally
9+
)
10+
.as_slice(),
11+
)
12+
.unwrap();
13+
assert!(problematic_regex.is_match("786357")); // check with no "decimal" hex digits - converted to integer
14+
assert!(problematic_regex.is_match("589701")); // check with "decimal" replacements - converted to integer
15+
assert!(problematic_regex.is_match("8FF85")); // check for hex display
16+
assert!(!problematic_regex.is_match("1193046")); // check for non-matching value
1017
}

0 commit comments

Comments
 (0)