From 2b14529a7cc106d5d808529cd14d71aa158c7789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gammels=C3=A6ter?= Date: Tue, 12 Apr 2022 16:31:03 +0200 Subject: [PATCH 1/2] Optimize line offset parsing in ::decode By inverting parsing loop, avoiding continually re-checking bytes_per_diff. --- compiler/rustc_span/src/lib.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 5232c8d7006bd..68401d5ca7cba 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1318,17 +1318,26 @@ impl Decodable for SourceFile { let mut line_start: BytePos = Decodable::decode(d); lines.push(line_start); - for _ in 1..num_lines { - let diff = match bytes_per_diff { - 1 => d.read_u8() as u32, - 2 => d.read_u16() as u32, - 4 => d.read_u32(), - _ => unreachable!(), - }; - - line_start = line_start + BytePos(diff); - - lines.push(line_start); + match bytes_per_diff { + 1 => { + for _ in 1..num_lines { + line_start = line_start + BytePos(d.read_u8() as u32); + lines.push(line_start); + } + } + 2 => { + for _ in 1..num_lines { + line_start = line_start + BytePos(d.read_u16() as u32); + lines.push(line_start); + } + } + 4 => { + for _ in 1..num_lines { + line_start = line_start + BytePos(d.read_u32()); + lines.push(line_start); + } + } + _ => unreachable!(), } } From 5f2c6b92d1cb2749872432448d42cd9cb05b0565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gammels=C3=A6ter?= Date: Wed, 13 Apr 2022 08:44:20 +0200 Subject: [PATCH 2/2] Use .extend(..) instead of push()-ing in loop A bit less readable but more compact, and maybe faster? We'll see. --- compiler/rustc_span/src/lib.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 68401d5ca7cba..45b0e0c2dd1f8 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1319,24 +1319,18 @@ impl Decodable for SourceFile { lines.push(line_start); match bytes_per_diff { - 1 => { - for _ in 1..num_lines { - line_start = line_start + BytePos(d.read_u8() as u32); - lines.push(line_start); - } - } - 2 => { - for _ in 1..num_lines { - line_start = line_start + BytePos(d.read_u16() as u32); - lines.push(line_start); - } - } - 4 => { - for _ in 1..num_lines { - line_start = line_start + BytePos(d.read_u32()); - lines.push(line_start); - } - } + 1 => lines.extend((1..num_lines).map(|_| { + line_start = line_start + BytePos(d.read_u8() as u32); + line_start + })), + 2 => lines.extend((1..num_lines).map(|_| { + line_start = line_start + BytePos(d.read_u16() as u32); + line_start + })), + 4 => lines.extend((1..num_lines).map(|_| { + line_start = line_start + BytePos(d.read_u32()); + line_start + })), _ => unreachable!(), } }