[clang] 80e8efd - Use a fast path when initializing LineOffsetMapping

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 1 01:23:45 PST 2021


Author: serge-sans-paille
Date: 2021-03-01T10:18:36+01:00
New Revision: 80e8efd563fda4d7b125b834d3243b3ef9a05270

URL: https://github.com/llvm/llvm-project/commit/80e8efd563fda4d7b125b834d3243b3ef9a05270
DIFF: https://github.com/llvm/llvm-project/commit/80e8efd563fda4d7b125b834d3243b3ef9a05270.diff

LOG: 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

Added: 
    

Modified: 
    clang/lib/Basic/SourceManager.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index c0b22837693b..cc275d4e8dc4 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;
   }


        


More information about the cfe-commits mailing list