1
Fork 0

Auto merge of #97386 - nnethercote:optimize-pos-adjustments, r=bjorn3

Optimize position adjustments

A small improvement.

r? `@bjorn3`
This commit is contained in:
bors 2022-05-26 22:01:19 +00:00
commit b2c9872c6c
3 changed files with 21 additions and 30 deletions

View file

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

View file

@ -345,20 +345,27 @@ impl SourceMap {
let end_pos = Pos::from_usize(start_pos + source_len);
let start_pos = Pos::from_usize(start_pos);
// Translate these positions into the new global frame of reference,
// now that the offset of the SourceFile is known.
//
// These are all unsigned values. `original_start_pos` may be larger or
// smaller than `start_pos`, but `pos` is always larger than both.
// Therefore, `(pos - original_start_pos) + start_pos` won't overflow
// but `start_pos - original_start_pos` might. So we use the former
// form rather than pre-computing the offset into a local variable. The
// compiler backend can optimize away the repeated computations in a
// way that won't trigger overflow checks.
for pos in &mut file_local_lines {
*pos = *pos + start_pos;
*pos = (*pos - original_start_pos) + start_pos;
}
for mbc in &mut file_local_multibyte_chars {
mbc.pos = mbc.pos + start_pos;
mbc.pos = (mbc.pos - original_start_pos) + start_pos;
}
for swc in &mut file_local_non_narrow_chars {
*swc = *swc + start_pos;
*swc = (*swc - original_start_pos) + start_pos;
}
for nc in &mut file_local_normalized_pos {
nc.pos = nc.pos + start_pos;
nc.pos = (nc.pos - original_start_pos) + start_pos;
}
let source_file = Lrc::new(SourceFile {