Skip to content

Commit 2b91c40

Browse files
committed
Avoid adjusting file positions twice.
`imported_source_files` adjusts lots of file positions, and then calls `new_imported_source_file`, which then adjust them all again. This commit combines the two adjustments into one, for a small perf win.
1 parent 3e810c6 commit 2b91c40

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -1639,10 +1639,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16391639
src_hash,
16401640
start_pos,
16411641
end_pos,
1642-
mut lines,
1643-
mut multibyte_chars,
1644-
mut non_narrow_chars,
1645-
mut normalized_pos,
1642+
lines,
1643+
multibyte_chars,
1644+
non_narrow_chars,
1645+
normalized_pos,
16461646
name_hash,
16471647
..
16481648
} = source_file_to_import;
@@ -1679,24 +1679,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16791679

16801680
let source_length = (end_pos - start_pos).to_usize();
16811681

1682-
// Translate line-start positions and multibyte character
1683-
// position into frame of reference local to file.
1684-
// `SourceMap::new_imported_source_file()` will then translate those
1685-
// coordinates to their new global frame of reference when the
1686-
// offset of the SourceFile is known.
1687-
for pos in &mut lines {
1688-
*pos = *pos - start_pos;
1689-
}
1690-
for mbc in &mut multibyte_chars {
1691-
mbc.pos = mbc.pos - start_pos;
1692-
}
1693-
for swc in &mut non_narrow_chars {
1694-
*swc = *swc - start_pos;
1695-
}
1696-
for np in &mut normalized_pos {
1697-
np.pos = np.pos - start_pos;
1698-
}
1699-
17001682
let local_version = sess.source_map().new_imported_source_file(
17011683
name,
17021684
src_hash,

compiler/rustc_span/src/source_map.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,27 @@ impl SourceMap {
345345
let end_pos = Pos::from_usize(start_pos + source_len);
346346
let start_pos = Pos::from_usize(start_pos);
347347

348+
// Translate these positions into the new global frame of reference,
349+
// now that the offset of the SourceFile is known.
350+
//
351+
// These are all unsigned values. `original_start_pos` may be larger or
352+
// smaller than `start_pos`, but `pos` is always larger than both.
353+
// Therefore, `(pos - original_start_pos) + start_pos` won't overflow
354+
// but `start_pos - original_start_pos` might. So we use the former
355+
// form rather than pre-computing the offset into a local variable. The
356+
// compiler backend can optimize away the repeated computations in a
357+
// way that won't trigger overflow checks.
348358
for pos in &mut file_local_lines {
349-
*pos = *pos + start_pos;
359+
*pos = (*pos - original_start_pos) + start_pos;
350360
}
351-
352361
for mbc in &mut file_local_multibyte_chars {
353-
mbc.pos = mbc.pos + start_pos;
362+
mbc.pos = (mbc.pos - original_start_pos) + start_pos;
354363
}
355-
356364
for swc in &mut file_local_non_narrow_chars {
357-
*swc = *swc + start_pos;
365+
*swc = (*swc - original_start_pos) + start_pos;
358366
}
359-
360367
for nc in &mut file_local_normalized_pos {
361-
nc.pos = nc.pos + start_pos;
368+
nc.pos = (nc.pos - original_start_pos) + start_pos;
362369
}
363370

364371
let source_file = Lrc::new(SourceFile {

0 commit comments

Comments
 (0)