Skip to content

Commit 2a18d12

Browse files
committed
Auto merge of rust-lang#97575 - nnethercote:lazify-SourceFile-lines, r=Mark-Simulacrum
Lazify `SourceFile::lines`. `SourceFile::lines` is a big part of metadata. It's stored in a compressed form (a difference list) to save disk space. Decoding it is a big fraction of compile time for very small crates/programs. This commit introduces a new type `SourceFileLines` which has a `Lines` form and a `Diffs` form. The latter is used when the metadata is first read, and it is only decoded into the `Lines` form when line data is actually needed. This avoids the decoding cost for many files, especially in `std`. It's a performance win of up to 15% for tiny crates/programs where metadata decoding is a high part of compilation costs. A `RefCell` is needed because the methods that access lines data (which can trigger decoding) take `&self` rather than `&mut self`. To allow for this, `SourceFile::lines` now takes a `FnMut` that operates on the lines slice rather than returning the lines slice. r? `@Mark-Simulacrum`
2 parents 86092a7 + 11d22ae commit 2a18d12

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,13 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> bool {
187187
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
188188
&& let Some(src) = unsafe_line.sf.src.as_deref()
189189
{
190-
comment_start_line.line < unsafe_line.line && text_has_safety_comment(
191-
src,
192-
&unsafe_line.sf.lines[comment_start_line.line + 1..=unsafe_line.line],
193-
unsafe_line.sf.start_pos.to_usize(),
194-
)
190+
unsafe_line.sf.lines(|lines| {
191+
comment_start_line.line < unsafe_line.line && text_has_safety_comment(
192+
src,
193+
&lines[comment_start_line.line + 1..=unsafe_line.line],
194+
unsafe_line.sf.start_pos.to_usize(),
195+
)
196+
})
195197
} else {
196198
// Problem getting source text. Pretend a comment was found.
197199
true
@@ -249,11 +251,13 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
249251
&& Lrc::ptr_eq(&unsafe_line.sf, &macro_line.sf)
250252
&& let Some(src) = unsafe_line.sf.src.as_deref()
251253
{
252-
macro_line.line < unsafe_line.line && text_has_safety_comment(
253-
src,
254-
&unsafe_line.sf.lines[macro_line.line + 1..=unsafe_line.line],
255-
unsafe_line.sf.start_pos.to_usize(),
256-
)
254+
unsafe_line.sf.lines(|lines| {
255+
macro_line.line < unsafe_line.line && text_has_safety_comment(
256+
src,
257+
&lines[macro_line.line + 1..=unsafe_line.line],
258+
unsafe_line.sf.start_pos.to_usize(),
259+
)
260+
})
257261
} else {
258262
// Problem getting source text. Pretend a comment was found.
259263
true
@@ -276,11 +280,13 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
276280
// Get the text from the start of function body to the unsafe block.
277281
// fn foo() { some_stuff; unsafe { stuff }; other_stuff; }
278282
// ^-------------^
279-
body_line.line < unsafe_line.line && text_has_safety_comment(
280-
src,
281-
&unsafe_line.sf.lines[body_line.line + 1..=unsafe_line.line],
282-
unsafe_line.sf.start_pos.to_usize(),
283-
)
283+
unsafe_line.sf.lines(|lines| {
284+
body_line.line < unsafe_line.line && text_has_safety_comment(
285+
src,
286+
&lines[body_line.line + 1..=unsafe_line.line],
287+
unsafe_line.sf.start_pos.to_usize(),
288+
)
289+
})
284290
} else {
285291
// Problem getting source text. Pretend a comment was found.
286292
true

clippy_utils/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ pub fn span_lint_and_sugg_for_edges(
283283
{
284284
let split_idx = MAX_SUGGESTION_HIGHLIGHT_LINES / 2;
285285
let span_upper = sm.span_until_char(
286-
sp.with_hi(line_upper.sf.lines[line_upper.line + split_idx]),
286+
sp.with_hi(line_upper.sf.lines(|lines| lines[line_upper.line + split_idx])),
287287
'\n',
288288
);
289-
let span_bottom = sp.with_lo(line_bottom.sf.lines[line_bottom.line - split_idx]);
289+
let span_bottom = sp.with_lo(line_bottom.sf.lines(|lines| lines[line_bottom.line - split_idx]));
290290

291291
let sugg_lines_vec = sugg.lines().collect::<Vec<&str>>();
292292
let sugg_upper = sugg_lines_vec[..split_idx].join("\n");

clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ fn line_span<T: LintContext>(cx: &T, span: Span) -> Span {
11491149
let span = original_sp(span, DUMMY_SP);
11501150
let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
11511151
let line_no = source_map_and_line.line;
1152-
let line_start = source_map_and_line.sf.lines[line_no];
1152+
let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]);
11531153
span.with_lo(line_start)
11541154
}
11551155

0 commit comments

Comments
 (0)