Skip to content

Commit

Permalink
Auto merge of #86778 - tmiasko:fast-multiline, r=davidtwco
Browse files Browse the repository at this point in the history
Avoid byte to char position conversions in `is_multiline`

Converting a byte position into a char position is currently linear in
the number of multibyte characters in the source code. Avoid it when
checking if a range spans across lines.

This makes it feasible to compile source files with a large number of
multibyte characters.
  • Loading branch information
bors committed Jul 3, 2021
2 parents 8649737 + 7a41076 commit 96859db
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions compiler/rustc_span/src/source_map.rs
Expand Up @@ -461,9 +461,13 @@ impl SourceMap {
}

pub fn is_multiline(&self, sp: Span) -> bool {
let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi());
lo.line != hi.line
let lo = self.lookup_source_file_idx(sp.lo());
let hi = self.lookup_source_file_idx(sp.hi());
if lo != hi {
return true;
}
let f = (*self.files.borrow().source_files)[lo].clone();
f.lookup_line(sp.lo()) != f.lookup_line(sp.hi())
}

pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
Expand Down

0 comments on commit 96859db

Please sign in to comment.