[clang] [clang][Lex] Optimize the FileCheckPoints search in Preprocessor (PR #206356)

Hardik Kumar via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 28 11:20:06 PDT 2026


https://github.com/hardikxk created https://github.com/llvm/llvm-project/pull/206356

Resolves a FIXME and improved linear search by replacing with a binary search on the FileCheckPoints SmallVector in Lex/Preprocessor.

- used std::upper_bound to find the upper bound of the Start position.
- handle special case if the Start is less than all elements in FileCheckPoints so returns a nullptr;
- else return the dereferenced value of the CheckPoint just before Start point.

>From 25f04854d70116e6cecce9104daa89f8f58189bc Mon Sep 17 00:00:00 2001
From: hardikxk <hardikxk at gmail.com>
Date: Sun, 28 Jun 2026 23:47:12 +0530
Subject: [PATCH] [clang][Lex] Optimize the FileCheckPoints search in
 Preprocessor Resolves a FIXME and improved linear search by replacing with a
 binary search on the FileCheckPoints SmallVector in Lex/PreProcessor.

- used std::upper_bound to find the upper bound of the Start position.
- handle special case if the Start is less than all elements in FileCheckPoints
so returns a nullptr;
- else return the dereferenced value of the CheckPoint just before Start point.
---
 clang/lib/Lex/Preprocessor.cpp | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index c69d084d6514f..3f83546ea7956 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -1746,16 +1746,13 @@ void Preprocessor::removePPCallbacks() {
 const char *Preprocessor::getCheckPoint(FileID FID, const char *Start) const {
   if (auto It = CheckPoints.find(FID); It != CheckPoints.end()) {
     const SmallVector<const char *> &FileCheckPoints = It->second;
-    const char *Last = nullptr;
-    // FIXME: Do better than a linear search.
-    for (const char *P : FileCheckPoints) {
-      if (P > Start)
-        break;
-      Last = P;
+    auto P =
+        std::upper_bound(FileCheckPoints.begin(), FileCheckPoints.end(), Start);
+    if (P == FileCheckPoints.begin()) {
+      return nullptr;
     }
-    return Last;
+    return *std::prev(P);
   }
-
   return nullptr;
 }
 



More information about the cfe-commits mailing list