Skip to content

Commit 61f5c20

Browse files
committed
[HEAVY PERF] Optimize documentation lints 2/2
So, after rust-lang#14693 was merged, this is the continuation. It performs some optimizations on `Fragments::span` , makes it so we don't call it so much, and makes a 85.75% decrease (7.51% -> 10.07%) in execution samples of `source_span_for_markdown_range` and a 6.39% -> 0.88% for `core::StrSearcher::new`. Overall a 13.11% icount decrase on docs-heavy crates. Benchmarked mainly on `regex-1.10.5`. This means that currently our heaviest function is `rustc_middle::Interners::intern_ty`, even for documentation-heavy crates
1 parent 3da4c10 commit 61f5c20

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

clippy_lints/src/doc/lazy_continuation.rs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use itertools::Itertools;
33
use rustc_errors::Applicability;
44
use rustc_lint::LateContext;
5-
use rustc_span::{BytePos, Span};
5+
use rustc_span::BytePos;
66
use std::cmp::Ordering;
77
use std::ops::Range;
88

9-
use super::{DOC_LAZY_CONTINUATION, DOC_OVERINDENTED_LIST_ITEMS};
9+
use super::{DOC_LAZY_CONTINUATION, DOC_OVERINDENTED_LIST_ITEMS, Fragments};
1010

1111
fn map_container_to_text(c: &super::Container) -> &'static str {
1212
match c {
@@ -19,29 +19,27 @@ fn map_container_to_text(c: &super::Container) -> &'static str {
1919
pub(super) fn check(
2020
cx: &LateContext<'_>,
2121
doc: &str,
22-
range: Range<usize>,
23-
mut span: Span,
22+
cooked_range: Range<usize>,
23+
fragments: &Fragments<'_>,
2424
containers: &[super::Container],
2525
) {
26-
if doc[range.clone()].contains('\t') {
27-
// We don't do tab stops correctly.
28-
return;
29-
}
30-
31-
// Blockquote
32-
let ccount = doc[range.clone()].chars().filter(|c| *c == '>').count();
26+
// Get blockquotes
27+
let ccount = doc[cooked_range.clone()].chars().filter(|c| *c == '>').count();
3328
let blockquote_level = containers
3429
.iter()
3530
.filter(|c| matches!(c, super::Container::Blockquote))
3631
.count();
37-
if ccount < blockquote_level {
32+
33+
if ccount < blockquote_level
34+
&& let Some(mut span) = fragments.span(cx, cooked_range.clone())
35+
{
3836
span_lint_and_then(
3937
cx,
4038
DOC_LAZY_CONTINUATION,
4139
span,
4240
"doc quote line without `>` marker",
4341
|diag| {
44-
let mut doc_start_range = &doc[range];
42+
let mut doc_start_range = &doc[cooked_range];
4543
let mut suggested = String::new();
4644
for c in containers {
4745
let text = map_container_to_text(c);
@@ -78,7 +76,7 @@ pub(super) fn check(
7876
}
7977

8078
// List
81-
let leading_spaces = doc[range].chars().filter(|c| *c == ' ').count();
79+
let leading_spaces = doc[cooked_range.clone()].chars().filter(|c| *c == ' ').count();
8280
let list_indentation = containers
8381
.iter()
8482
.map(|c| {
@@ -90,25 +88,34 @@ pub(super) fn check(
9088
})
9189
.sum();
9290
match leading_spaces.cmp(&list_indentation) {
93-
Ordering::Less => span_lint_and_then(
94-
cx,
95-
DOC_LAZY_CONTINUATION,
96-
span,
97-
"doc list item without indentation",
98-
|diag| {
99-
// simpler suggestion style for indentation
100-
let indent = list_indentation - leading_spaces;
101-
diag.span_suggestion_verbose(
102-
span.shrink_to_hi(),
103-
"indent this line",
104-
std::iter::repeat_n(" ", indent).join(""),
105-
Applicability::MaybeIncorrect,
106-
);
107-
diag.help("if this is supposed to be its own paragraph, add a blank line");
108-
},
109-
),
91+
Ordering::Less => {
92+
let Some(span) = fragments.span(cx, cooked_range.clone()) else {
93+
return;
94+
};
95+
span_lint_and_then(
96+
cx,
97+
DOC_LAZY_CONTINUATION,
98+
span,
99+
"doc list item without indentation",
100+
|diag| {
101+
// simpler suggestion style for indentation
102+
let indent = list_indentation - leading_spaces;
103+
diag.span_suggestion_verbose(
104+
span.shrink_to_hi(),
105+
"indent this line",
106+
std::iter::repeat_n(" ", indent).join(""),
107+
Applicability::MaybeIncorrect,
108+
);
109+
diag.help("if this is supposed to be its own paragraph, add a blank line");
110+
},
111+
);
112+
},
110113
Ordering::Greater => {
111114
let sugg = std::iter::repeat_n(" ", list_indentation).join("");
115+
116+
let Some(span) = fragments.span(cx, cooked_range.clone()) else {
117+
return;
118+
};
112119
span_lint_and_sugg(
113120
cx,
114121
DOC_OVERINDENTED_LIST_ITEMS,

clippy_lints/src/doc/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use clippy_config::Conf;
44
use clippy_utils::attrs::is_doc_hidden;
55
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_then};
6-
use clippy_utils::source::snippet_opt;
76
use clippy_utils::{is_entrypoint_fn, is_trait_impl_item};
87
use pulldown_cmark::Event::{
98
Code, DisplayMath, End, FootnoteReference, HardBreak, Html, InlineHtml, InlineMath, Rule, SoftBreak, Start,
@@ -1081,26 +1080,27 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
10811080
| TaskListMarker(_) | Code(_) | Rule | InlineMath(..) | DisplayMath(..) => (),
10821081
SoftBreak | HardBreak => {
10831082
if !containers.is_empty()
1084-
&& let Some((next_event, next_range)) = events.peek()
1085-
&& let Some(next_span) = fragments.span(cx, next_range.clone())
1086-
&& let Some(span) = fragments.span(cx, range.clone())
10871083
&& !in_footnote_definition
1084+
// Tabs aren't handled correctly vvvv
1085+
&& !doc[range.clone()].contains('\t')
1086+
&& let Some((next_event, next_range)) = events.peek()
10881087
&& !matches!(next_event, End(_))
10891088
{
10901089
lazy_continuation::check(
10911090
cx,
10921091
doc,
10931092
range.end..next_range.start,
1094-
Span::new(span.hi(), next_span.lo(), span.ctxt(), span.parent()),
1093+
&fragments,
10951094
&containers[..],
10961095
);
10971096
}
10981097

1099-
if let Some(span) = fragments.span(cx, range.clone())
1098+
1099+
if event == HardBreak
1100+
&& !doc[range.clone()].trim().starts_with('\\')
1101+
&& let Some(span) = fragments.span(cx, range.clone())
11001102
&& !span.from_expansion()
1101-
&& let Some(snippet) = snippet_opt(cx, span)
1102-
&& !snippet.trim().starts_with('\\')
1103-
&& event == HardBreak {
1103+
{
11041104
collected_breaks.push(span);
11051105
}
11061106
},

0 commit comments

Comments
 (0)