@@ -122,19 +122,25 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
122
122
ebbs. sort_by_key ( |ebb| func. offsets [ * ebb] ) ; // Ensure inst offsets always increase
123
123
124
124
let line_strings = & mut self . debug_context . dwarf . line_strings ;
125
+ let mut last_file = None ;
125
126
let mut create_row_for_span = |line_program : & mut LineProgram , span : Span | {
126
127
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 ) ;
128
128
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
+ }
136
143
137
- line_program. row ( ) . file = file_id;
138
144
line_program. row ( ) . line = loc. line as u64 ;
139
145
line_program. row ( ) . column = loc. col . to_u32 ( ) as u64 + 1 ;
140
146
line_program. generate_row ( ) ;
0 commit comments