mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 04:04:06 +00:00
Auto merge of #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`
This commit is contained in:
commit
2a18d124aa
@ -187,11 +187,13 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> bool {
|
|||||||
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
|
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
|
||||||
&& let Some(src) = unsafe_line.sf.src.as_deref()
|
&& let Some(src) = unsafe_line.sf.src.as_deref()
|
||||||
{
|
{
|
||||||
comment_start_line.line < unsafe_line.line && text_has_safety_comment(
|
unsafe_line.sf.lines(|lines| {
|
||||||
src,
|
comment_start_line.line < unsafe_line.line && text_has_safety_comment(
|
||||||
&unsafe_line.sf.lines[comment_start_line.line + 1..=unsafe_line.line],
|
src,
|
||||||
unsafe_line.sf.start_pos.to_usize(),
|
&lines[comment_start_line.line + 1..=unsafe_line.line],
|
||||||
)
|
unsafe_line.sf.start_pos.to_usize(),
|
||||||
|
)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
// Problem getting source text. Pretend a comment was found.
|
// Problem getting source text. Pretend a comment was found.
|
||||||
true
|
true
|
||||||
@ -249,11 +251,13 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
|
|||||||
&& Lrc::ptr_eq(&unsafe_line.sf, ¯o_line.sf)
|
&& Lrc::ptr_eq(&unsafe_line.sf, ¯o_line.sf)
|
||||||
&& let Some(src) = unsafe_line.sf.src.as_deref()
|
&& let Some(src) = unsafe_line.sf.src.as_deref()
|
||||||
{
|
{
|
||||||
macro_line.line < unsafe_line.line && text_has_safety_comment(
|
unsafe_line.sf.lines(|lines| {
|
||||||
src,
|
macro_line.line < unsafe_line.line && text_has_safety_comment(
|
||||||
&unsafe_line.sf.lines[macro_line.line + 1..=unsafe_line.line],
|
src,
|
||||||
unsafe_line.sf.start_pos.to_usize(),
|
&lines[macro_line.line + 1..=unsafe_line.line],
|
||||||
)
|
unsafe_line.sf.start_pos.to_usize(),
|
||||||
|
)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
// Problem getting source text. Pretend a comment was found.
|
// Problem getting source text. Pretend a comment was found.
|
||||||
true
|
true
|
||||||
@ -276,11 +280,13 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
|
|||||||
// Get the text from the start of function body to the unsafe block.
|
// Get the text from the start of function body to the unsafe block.
|
||||||
// fn foo() { some_stuff; unsafe { stuff }; other_stuff; }
|
// fn foo() { some_stuff; unsafe { stuff }; other_stuff; }
|
||||||
// ^-------------^
|
// ^-------------^
|
||||||
body_line.line < unsafe_line.line && text_has_safety_comment(
|
unsafe_line.sf.lines(|lines| {
|
||||||
src,
|
body_line.line < unsafe_line.line && text_has_safety_comment(
|
||||||
&unsafe_line.sf.lines[body_line.line + 1..=unsafe_line.line],
|
src,
|
||||||
unsafe_line.sf.start_pos.to_usize(),
|
&lines[body_line.line + 1..=unsafe_line.line],
|
||||||
)
|
unsafe_line.sf.start_pos.to_usize(),
|
||||||
|
)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
// Problem getting source text. Pretend a comment was found.
|
// Problem getting source text. Pretend a comment was found.
|
||||||
true
|
true
|
||||||
|
@ -283,10 +283,10 @@ pub fn span_lint_and_sugg_for_edges(
|
|||||||
{
|
{
|
||||||
let split_idx = MAX_SUGGESTION_HIGHLIGHT_LINES / 2;
|
let split_idx = MAX_SUGGESTION_HIGHLIGHT_LINES / 2;
|
||||||
let span_upper = sm.span_until_char(
|
let span_upper = sm.span_until_char(
|
||||||
sp.with_hi(line_upper.sf.lines[line_upper.line + split_idx]),
|
sp.with_hi(line_upper.sf.lines(|lines| lines[line_upper.line + split_idx])),
|
||||||
'\n',
|
'\n',
|
||||||
);
|
);
|
||||||
let span_bottom = sp.with_lo(line_bottom.sf.lines[line_bottom.line - split_idx]);
|
let span_bottom = sp.with_lo(line_bottom.sf.lines(|lines| lines[line_bottom.line - split_idx]));
|
||||||
|
|
||||||
let sugg_lines_vec = sugg.lines().collect::<Vec<&str>>();
|
let sugg_lines_vec = sugg.lines().collect::<Vec<&str>>();
|
||||||
let sugg_upper = sugg_lines_vec[..split_idx].join("\n");
|
let sugg_upper = sugg_lines_vec[..split_idx].join("\n");
|
||||||
|
@ -1149,7 +1149,7 @@ fn line_span<T: LintContext>(cx: &T, span: Span) -> Span {
|
|||||||
let span = original_sp(span, DUMMY_SP);
|
let span = original_sp(span, DUMMY_SP);
|
||||||
let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
|
let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
|
||||||
let line_no = source_map_and_line.line;
|
let line_no = source_map_and_line.line;
|
||||||
let line_start = source_map_and_line.sf.lines[line_no];
|
let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]);
|
||||||
span.with_lo(line_start)
|
span.with_lo(line_start)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user