diff options
author | serge-sans-paille <sguelton@redhat.com> | 2021-02-23 20:46:35 +0100 |
---|---|---|
committer | serge-sans-paille <sguelton@redhat.com> | 2021-03-01 10:18:36 +0100 |
commit | 80e8efd563fda4d7b125b834d3243b3ef9a05270 (patch) | |
tree | dbf251ea2631ce3173a82ddbf832d86e3925b57d /clang/lib/Basic/SourceManager.cpp | |
parent | 588db1ccff713332c1f9358f423e682f18c06e8e (diff) | |
download | llvm-80e8efd563fda4d7b125b834d3243b3ef9a05270.zip llvm-80e8efd563fda4d7b125b834d3243b3ef9a05270.tar.gz llvm-80e8efd563fda4d7b125b834d3243b3ef9a05270.tar.bz2 |
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
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index c0b2283..cc275d4 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -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; } |