Skip to content

Commit 2b14529

Browse files
committed
Optimize line offset parsing in <SourceFile as Decodable>::decode
By inverting parsing loop, avoiding continually re-checking bytes_per_diff.
1 parent 4e1927d commit 2b14529

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

compiler/rustc_span/src/lib.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -1318,17 +1318,26 @@ impl<D: Decoder> Decodable<D> for SourceFile {
13181318
let mut line_start: BytePos = Decodable::decode(d);
13191319
lines.push(line_start);
13201320

1321-
for _ in 1..num_lines {
1322-
let diff = match bytes_per_diff {
1323-
1 => d.read_u8() as u32,
1324-
2 => d.read_u16() as u32,
1325-
4 => d.read_u32(),
1326-
_ => unreachable!(),
1327-
};
1328-
1329-
line_start = line_start + BytePos(diff);
1330-
1331-
lines.push(line_start);
1321+
match bytes_per_diff {
1322+
1 => {
1323+
for _ in 1..num_lines {
1324+
line_start = line_start + BytePos(d.read_u8() as u32);
1325+
lines.push(line_start);
1326+
}
1327+
}
1328+
2 => {
1329+
for _ in 1..num_lines {
1330+
line_start = line_start + BytePos(d.read_u16() as u32);
1331+
lines.push(line_start);
1332+
}
1333+
}
1334+
4 => {
1335+
for _ in 1..num_lines {
1336+
line_start = line_start + BytePos(d.read_u32());
1337+
lines.push(line_start);
1338+
}
1339+
}
1340+
_ => unreachable!(),
13321341
}
13331342
}
13341343

0 commit comments

Comments
 (0)