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.
This commit is contained in:
Tyson Nottingham
2020-09-20 17:40:51 -07:00
parent 0987b84198
commit 75de8286c0
3 changed files with 203 additions and 40 deletions

View File

@@ -1848,6 +1848,10 @@ pub trait HashStableContext {
&mut self,
byte: BytePos,
) -> Option<(Lrc<SourceFile>, usize, BytePos)>;
fn span_data_to_lines_and_cols(
&mut self,
span: &SpanData,
) -> Option<(Lrc<SourceFile>, usize, BytePos, usize, BytePos)>;
}
impl<CTX> HashStable<CTX> 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