Skip to content

Commit 72de7c4

Browse files
committed
Address review comments.
1 parent 0b81d7c commit 72de7c4

File tree

3 files changed

+73
-54
lines changed

3 files changed

+73
-54
lines changed

compiler/rustc_query_system/src/ich/impls_syntax.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ich::StableHashingContext;
55

66
use rustc_ast as ast;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8-
use rustc_span::{BytePos, NormalizedPos, SourceFile, SourceFileLines};
8+
use rustc_span::{BytePos, NormalizedPos, SourceFile};
99
use std::assert_matches::assert_matches;
1010

1111
use smallvec::SmallVec;
@@ -60,7 +60,7 @@ impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
6060
impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
6161
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
6262
let SourceFile {
63-
ref name, // We hash the smaller name_hash instead of this
63+
name: _, // We hash the smaller name_hash instead of this
6464
name_hash,
6565
cnum,
6666
// Do not hash the source as it is not encoded
@@ -69,7 +69,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
6969
external_src: _,
7070
start_pos,
7171
end_pos: _,
72-
ref lines,
72+
lines: _,
7373
ref multibyte_chars,
7474
ref non_narrow_chars,
7575
ref normalized_pos,
@@ -79,18 +79,15 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
7979

8080
src_hash.hash_stable(hcx, hasher);
8181

82-
// We only hash the relative position within this source_file
83-
match &*lines.borrow() {
84-
SourceFileLines::Lines { lines } => {
85-
lines.len().hash_stable(hcx, hasher);
86-
for &line in lines.iter() {
87-
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
88-
}
89-
}
90-
SourceFileLines::Diffs { .. } => {
91-
panic!("called hash_stable on SourceFileLines::Diffs for {:?}", name);
82+
// We are always in `Lines` form by the time we reach here.
83+
assert!(self.lines.borrow().is_lines());
84+
self.lines(|lines| {
85+
// We only hash the relative position within this source_file
86+
lines.len().hash_stable(hcx, hasher);
87+
for &line in lines.iter() {
88+
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
9289
}
93-
}
90+
});
9491

9592
// We only hash the relative position within this source_file
9693
multibyte_chars.len().hash_stable(hcx, hasher);

compiler/rustc_span/src/lib.rs

+60-38
Original file line numberDiff line numberDiff line change
@@ -1225,37 +1225,47 @@ impl DebuggerVisualizerFile {
12251225
#[derive(Clone)]
12261226
pub enum SourceFileLines {
12271227
/// The source file lines, in decoded (random-access) form.
1228-
Lines { lines: Vec<BytePos> },
1228+
Lines(Vec<BytePos>),
12291229

1230-
/// The source file lines in difference list form. This matches the form
1231-
/// used within metadata, which saves space by exploiting the fact that the
1232-
/// lines list is sorted and individual lines are usually not that long.
1233-
///
1234-
/// We read it directly from metadata and only decode it into `Lines` form
1235-
/// when necessary. This is a significant performance win, especially for
1236-
/// small crates where very little of `std`'s metadata is used.
1237-
Diffs {
1238-
/// Position of the first line. Note that this is always encoded as a
1239-
/// `BytePos` because it is often much larger than any of the
1240-
/// differences.
1241-
line_start: BytePos,
1242-
1243-
/// Always 1, 2, or 4. Always as small as possible, while being big
1244-
/// enough to hold the length of the longest line in the source file.
1245-
/// The 1 case is by far the most common.
1246-
bytes_per_diff: usize,
1247-
1248-
/// The number of diffs encoded in `raw_diffs`. Always one less than
1249-
/// the number of lines in the source file.
1250-
num_diffs: usize,
1251-
1252-
/// The diffs in "raw" form. Each segment of `bytes_per_diff` length
1253-
/// encodes one little-endian diff. Note that they aren't LEB128
1254-
/// encoded. This makes for much faster decoding. Besides, the
1255-
/// bytes_per_diff==1 case is by far the most common, and LEB128
1256-
/// encoding has no effect on that case.
1257-
raw_diffs: Vec<u8>,
1258-
},
1230+
/// The source file lines, in undecoded difference list form.
1231+
Diffs(SourceFileDiffs),
1232+
}
1233+
1234+
impl SourceFileLines {
1235+
pub fn is_lines(&self) -> bool {
1236+
matches!(self, SourceFileLines::Lines(_))
1237+
}
1238+
}
1239+
1240+
/// The source file lines in difference list form. This matches the form
1241+
/// used within metadata, which saves space by exploiting the fact that the
1242+
/// lines list is sorted and individual lines are usually not that long.
1243+
///
1244+
/// We read it directly from metadata and only decode it into `Lines` form
1245+
/// when necessary. This is a significant performance win, especially for
1246+
/// small crates where very little of `std`'s metadata is used.
1247+
#[derive(Clone)]
1248+
pub struct SourceFileDiffs {
1249+
/// Position of the first line. Note that this is always encoded as a
1250+
/// `BytePos` because it is often much larger than any of the
1251+
/// differences.
1252+
line_start: BytePos,
1253+
1254+
/// Always 1, 2, or 4. Always as small as possible, while being big
1255+
/// enough to hold the length of the longest line in the source file.
1256+
/// The 1 case is by far the most common.
1257+
bytes_per_diff: usize,
1258+
1259+
/// The number of diffs encoded in `raw_diffs`. Always one less than
1260+
/// the number of lines in the source file.
1261+
num_diffs: usize,
1262+
1263+
/// The diffs in "raw" form. Each segment of `bytes_per_diff` length
1264+
/// encodes one little-endian diff. Note that they aren't LEB128
1265+
/// encoded. This makes for much faster decoding. Besides, the
1266+
/// bytes_per_diff==1 case is by far the most common, and LEB128
1267+
/// encoding has no effect on that case.
1268+
raw_diffs: Vec<u8>,
12591269
}
12601270

12611271
/// A single source in the [`SourceMap`].
@@ -1298,6 +1308,8 @@ impl<S: Encoder> Encodable<S> for SourceFile {
12981308
s.emit_struct_field("start_pos", false, |s| self.start_pos.encode(s))?;
12991309
s.emit_struct_field("end_pos", false, |s| self.end_pos.encode(s))?;
13001310
s.emit_struct_field("lines", false, |s| {
1311+
// We are always in `Lines` form by the time we reach here.
1312+
assert!(self.lines.borrow().is_lines());
13011313
self.lines(|lines| {
13021314
// Store the length.
13031315
s.emit_u32(lines.len() as u32)?;
@@ -1384,9 +1396,14 @@ impl<D: Decoder> Decodable<D> for SourceFile {
13841396
// Read the difference list.
13851397
let num_diffs = num_lines as usize - 1;
13861398
let raw_diffs = d.read_raw_bytes(bytes_per_diff * num_diffs).to_vec();
1387-
SourceFileLines::Diffs { line_start, bytes_per_diff, num_diffs, raw_diffs }
1399+
SourceFileLines::Diffs(SourceFileDiffs {
1400+
line_start,
1401+
bytes_per_diff,
1402+
num_diffs,
1403+
raw_diffs,
1404+
})
13881405
} else {
1389-
SourceFileLines::Lines { lines: vec![] }
1406+
SourceFileLines::Lines(vec![])
13901407
}
13911408
};
13921409
let multibyte_chars: Vec<MultiByteChar> = Decodable::decode(d);
@@ -1448,7 +1465,7 @@ impl SourceFile {
14481465
external_src: Lock::new(ExternalSource::Unneeded),
14491466
start_pos,
14501467
end_pos: Pos::from_usize(end_pos),
1451-
lines: Lock::new(SourceFileLines::Lines { lines }),
1468+
lines: Lock::new(SourceFileLines::Lines(lines)),
14521469
multibyte_chars,
14531470
non_narrow_chars,
14541471
normalized_pos,
@@ -1457,14 +1474,19 @@ impl SourceFile {
14571474
}
14581475
}
14591476

1460-
pub fn lines<F, R>(&self, mut f: F) -> R
1477+
pub fn lines<F, R>(&self, f: F) -> R
14611478
where
1462-
F: FnMut(&[BytePos]) -> R,
1479+
F: FnOnce(&[BytePos]) -> R,
14631480
{
14641481
let mut guard = self.lines.borrow_mut();
14651482
match &*guard {
1466-
SourceFileLines::Lines { lines } => f(lines),
1467-
SourceFileLines::Diffs { mut line_start, bytes_per_diff, num_diffs, raw_diffs } => {
1483+
SourceFileLines::Lines(lines) => f(lines),
1484+
SourceFileLines::Diffs(SourceFileDiffs {
1485+
mut line_start,
1486+
bytes_per_diff,
1487+
num_diffs,
1488+
raw_diffs,
1489+
}) => {
14681490
// Convert from "diffs" form to "lines" form.
14691491
let num_lines = num_diffs + 1;
14701492
let mut lines = Vec::with_capacity(num_lines);
@@ -1504,7 +1526,7 @@ impl SourceFile {
15041526
_ => unreachable!(),
15051527
}
15061528
let res = f(&lines);
1507-
*guard = SourceFileLines::Lines { lines };
1529+
*guard = SourceFileLines::Lines(lines);
15081530
res
15091531
}
15101532
}

compiler/rustc_span/src/source_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,12 @@ impl SourceMap {
356356
// compiler backend can optimize away the repeated computations in a
357357
// way that won't trigger overflow checks.
358358
match &mut *file_local_lines.borrow_mut() {
359-
SourceFileLines::Lines { lines } => {
359+
SourceFileLines::Lines(lines) => {
360360
for pos in lines {
361361
*pos = (*pos - original_start_pos) + start_pos;
362362
}
363363
}
364-
SourceFileLines::Diffs { line_start, .. } => {
364+
SourceFileLines::Diffs(SourceFileDiffs { line_start, .. }) => {
365365
*line_start = (*line_start - original_start_pos) + start_pos;
366366
}
367367
}

0 commit comments

Comments
 (0)