From 75de8286c04af256762804ee96b08a68d2aba279 Mon Sep 17 00:00:00 2001 From: Tyson Nottingham Date: Sun, 20 Sep 2020 17:40:51 -0700 Subject: rustc_span: add span_data_to_lines_and_cols to caching source map view Gives a performance increase over calling byte_pos_to_line_and_col twice, partially because it decreases the function calling overhead, potentially because it doesn't populate the line cache with lines that turn out to belong to invalid spans, and likely because of some other incidental improvements made possible by having more context available. --- compiler/rustc_span/src/lib.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'compiler/rustc_span/src/lib.rs') diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 11a49d1ab88..24b06f27afd 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1848,6 +1848,10 @@ pub trait HashStableContext { &mut self, byte: BytePos, ) -> Option<(Lrc, usize, BytePos)>; + fn span_data_to_lines_and_cols( + &mut self, + span: &SpanData, + ) -> Option<(Lrc, usize, BytePos, usize, BytePos)>; } impl HashStable for Span @@ -1880,22 +1884,8 @@ where // position that belongs to it, as opposed to hashing the first // position past it. let span = self.data(); - let (file_lo, line_lo, col_lo) = match ctx.byte_pos_to_line_and_col(span.lo) { - Some(pos) => pos, - None => { - Hash::hash(&TAG_INVALID_SPAN, hasher); - span.ctxt.hash_stable(ctx, hasher); - return; - } - }; - - if !file_lo.contains(span.hi) { - Hash::hash(&TAG_INVALID_SPAN, hasher); - span.ctxt.hash_stable(ctx, hasher); - return; - } - - let (_, line_hi, col_hi) = match ctx.byte_pos_to_line_and_col(span.hi) { + let (file, line_lo, col_lo, line_hi, col_hi) = match ctx.span_data_to_lines_and_cols(&span) + { Some(pos) => pos, None => { Hash::hash(&TAG_INVALID_SPAN, hasher); @@ -1907,7 +1897,7 @@ where Hash::hash(&TAG_VALID_SPAN, hasher); // We truncate the stable ID hash and line and column numbers. The chances // of causing a collision this way should be minimal. - Hash::hash(&(file_lo.name_hash as u64), hasher); + Hash::hash(&(file.name_hash as u64), hasher); // Hash both the length and the end location (line/column) of a span. If we // hash only the length, for example, then two otherwise equal spans with -- cgit 1.4.1-3-g733a5