Use a fast path when initializing LineOffsetMapping

Use the fact that the number of line break is lower than printable characters to
guide the optimization process. Also use a fuzzy test that catches both \n and
\r in a single check to speedup the computation.

Differential Revision: https://reviews.llvm.org/D97320
This commit is contained in:
serge-sans-paille 2021-02-23 20:46:35 +01:00
parent 588db1ccff
commit 80e8efd563
1 changed files with 10 additions and 7 deletions

View File

@ -1270,13 +1270,16 @@ LineOffsetMapping LineOffsetMapping::get(llvm::MemoryBufferRef Buffer,
const std::size_t BufLen = End - Buf;
unsigned I = 0;
while (I < BufLen) {
if (Buf[I] == '\n') {
LineOffsets.push_back(I + 1);
} else if (Buf[I] == '\r') {
// If this is \r\n, skip both characters.
if (I + 1 < BufLen && Buf[I + 1] == '\n')
++I;
LineOffsets.push_back(I + 1);
// Use a fast check to catch both newlines
if (LLVM_UNLIKELY(Buf[I] <= std::max('\n', '\r'))) {
if (Buf[I] == '\n') {
LineOffsets.push_back(I + 1);
} else if (Buf[I] == '\r') {
// If this is \r\n, skip both characters.
if (I + 1 < BufLen && Buf[I + 1] == '\n')
++I;
LineOffsets.push_back(I + 1);
}
}
++I;
}