Skip to content

Commit eb4fc45

Browse files
committed
Use CachingSourceMapView::byte_pos_to_line_and_col instead of SourceMap::lookup_char_pos
The former calculates byte offsets instead of char offsets. It is faster to calculate byte offsets than char offsets. Also most DWARF producers and consumers use byte offsets instead of char offsets.
1 parent 33e7309 commit eb4fc45

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/debuginfo/line_info.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Component, Path};
33

44
use crate::prelude::*;
55

6-
use rustc_span::FileName;
6+
use rustc_span::{CachingSourceMapView, FileName};
77

88
use cranelift_codegen::binemit::CodeOffset;
99

@@ -74,12 +74,12 @@ fn line_program_add_file(
7474

7575
impl<'tcx> DebugContext<'tcx> {
7676
pub(super) fn emit_location(&mut self, entry_id: UnitEntryId, span: Span) {
77-
let loc = self.tcx.sess.source_map().lookup_char_pos(span.lo());
77+
let (file, line, col) = self.source_map.byte_pos_to_line_and_col(span.lo()).unwrap();
7878

7979
let file_id = line_program_add_file(
8080
&mut self.dwarf.unit.line_program,
8181
&mut self.dwarf.line_strings,
82-
&loc.file.name,
82+
&file.name,
8383
);
8484

8585
let entry = self.dwarf.unit.get_mut(entry_id);
@@ -90,12 +90,12 @@ impl<'tcx> DebugContext<'tcx> {
9090
);
9191
entry.set(
9292
gimli::DW_AT_decl_line,
93-
AttributeValue::Udata(loc.line as u64),
93+
AttributeValue::Udata(line as u64),
9494
);
9595
// FIXME: probably omit this
9696
entry.set(
9797
gimli::DW_AT_decl_column,
98-
AttributeValue::Udata(loc.col.to_usize() as u64),
98+
AttributeValue::Udata(col.to_usize() as u64),
9999
);
100100
}
101101
}
@@ -108,6 +108,7 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
108108
source_info_set: &indexmap::IndexSet<SourceInfo>,
109109
) -> CodeOffset {
110110
let tcx = self.debug_context.tcx;
111+
let mut source_map = CachingSourceMapView::new(tcx.sess.source_map());
111112

112113
let line_program = &mut self.debug_context.dwarf.unit.line_program;
113114

@@ -124,25 +125,25 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
124125
let line_strings = &mut self.debug_context.dwarf.line_strings;
125126
let mut last_file = None;
126127
let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| {
127-
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
128+
let (file, line, col) = source_map.byte_pos_to_line_and_col(span.lo()).unwrap();
128129

129130
// line_program_add_file is very slow.
130131
// Optimize for the common case of the current file not being changed.
131132
let current_file_changed = if let Some(last_file) = &mut last_file {
132133
// If the allocations are not equal, then the files may still be equal, but that
133134
// is not a problem, as this is just an optimization.
134-
!Lrc::ptr_eq(last_file, &loc.file)
135+
!Lrc::ptr_eq(last_file, &file)
135136
} else {
136137
true
137138
};
138139
if current_file_changed {
139-
let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
140+
let file_id = line_program_add_file(line_program, line_strings, &file.name);
140141
line_program.row().file = file_id;
141-
last_file = Some(loc.file.clone());
142+
last_file = Some(file.clone());
142143
}
143144

144-
line_program.row().line = loc.line as u64;
145-
line_program.row().column = loc.col.to_u32() as u64 + 1;
145+
line_program.row().line = line as u64;
146+
line_program.row().column = col.to_u32() as u64 + 1;
146147
line_program.generate_row();
147148
};
148149

src/debuginfo/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mod line_info;
33

44
use crate::prelude::*;
55

6+
use rustc_span::CachingSourceMapView;
7+
68
use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
79
use cranelift_codegen::isa::RegUnit;
810
use cranelift_codegen::ValueLocRange;
@@ -34,6 +36,8 @@ pub struct DebugContext<'tcx> {
3436
unit_range_list: RangeList,
3537

3638
types: HashMap<Ty<'tcx>, UnitEntryId>,
39+
40+
source_map: CachingSourceMapView<'tcx>,
3741
}
3842

3943
impl<'tcx> DebugContext<'tcx> {
@@ -98,6 +102,8 @@ impl<'tcx> DebugContext<'tcx> {
98102
unit_range_list: RangeList(Vec::new()),
99103

100104
types: HashMap::new(),
105+
106+
source_map: CachingSourceMapView::new(tcx.sess.source_map()),
101107
}
102108
}
103109

0 commit comments

Comments
 (0)