Skip to content

Commit b2c9872

Browse files
committed
Auto merge of #97386 - nnethercote:optimize-pos-adjustments, r=bjorn3
Optimize position adjustments A small improvement. r? `@bjorn3`
2 parents 490324f + 2b91c40 commit b2c9872

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -1565,10 +1565,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15651565
src_hash,
15661566
start_pos,
15671567
end_pos,
1568-
mut lines,
1569-
mut multibyte_chars,
1570-
mut non_narrow_chars,
1571-
mut normalized_pos,
1568+
lines,
1569+
multibyte_chars,
1570+
non_narrow_chars,
1571+
normalized_pos,
15721572
name_hash,
15731573
..
15741574
} = source_file_to_import;
@@ -1605,24 +1605,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16051605

16061606
let source_length = (end_pos - start_pos).to_usize();
16071607

1608-
// Translate line-start positions and multibyte character
1609-
// position into frame of reference local to file.
1610-
// `SourceMap::new_imported_source_file()` will then translate those
1611-
// coordinates to their new global frame of reference when the
1612-
// offset of the SourceFile is known.
1613-
for pos in &mut lines {
1614-
*pos = *pos - start_pos;
1615-
}
1616-
for mbc in &mut multibyte_chars {
1617-
mbc.pos = mbc.pos - start_pos;
1618-
}
1619-
for swc in &mut non_narrow_chars {
1620-
*swc = *swc - start_pos;
1621-
}
1622-
for np in &mut normalized_pos {
1623-
np.pos = np.pos - start_pos;
1624-
}
1625-
16261608
let local_version = sess.source_map().new_imported_source_file(
16271609
name,
16281610
src_hash,

compiler/rustc_span/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,9 @@ impl<S: Encoder> Encodable<S> for SourceFile {
12701270
// the lines list is sorted and individual lines are
12711271
// probably not that long. Because of that we can store lines
12721272
// as a difference list, using as little space as possible
1273-
// for the differences.
1273+
// for the differences. But note that the first line is
1274+
// always encoded as a `BytePos` because its position is
1275+
// often much larger than any of the differences.
12741276
let max_line_length = if lines.len() == 1 {
12751277
0
12761278
} else {

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)