Skip to content

Commit 29fafb4

Browse files
committed
Optimize .debug_line generation
This reduces the amount of time spent in .debug_line generation by about 50% Fixes #807
1 parent 4fbb45c commit 29fafb4

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/debuginfo/line_info.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,25 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
122122
ebbs.sort_by_key(|ebb| func.offsets[*ebb]); // Ensure inst offsets always increase
123123

124124
let line_strings = &mut self.debug_context.dwarf.line_strings;
125+
let mut last_file = None;
125126
let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| {
126127
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
127-
let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
128128

129-
/*println!(
130-
"srcloc {:>04X} {}:{}:{}",
131-
line_program.row().address_offset,
132-
file.display(),
133-
loc.line,
134-
loc.col.to_u32()
135-
);*/
129+
// line_program_add_file is very slow.
130+
// Optimize for the common case of the current file not being changed.
131+
let current_file_changed = if let Some(last_file) = &mut last_file {
132+
// If the allocations are not equal, then the files may still be equal, but that
133+
// is not a problem, as this is just an optimization.
134+
!Lrc::ptr_eq(last_file, &loc.file)
135+
} else {
136+
true
137+
};
138+
if current_file_changed {
139+
let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
140+
line_program.row().file = file_id;
141+
last_file = Some(loc.file.clone());
142+
}
136143

137-
line_program.row().file = file_id;
138144
line_program.row().line = loc.line as u64;
139145
line_program.row().column = loc.col.to_u32() as u64 + 1;
140146
line_program.generate_row();

0 commit comments

Comments
 (0)