[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 23:07:13 PDT 2026
https://github.com/hardikxk updated https://github.com/llvm/llvm-project/pull/206356
>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 1/2] [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;
}
>From 0754a9ce12975616350742bbc56fded8a06abce1 Mon Sep 17 00:00:00 2001
From: hardikxk <hardikxk at gmail.com>
Date: Mon, 29 Jun 2026 11:36:57 +0530
Subject: [PATCH 2/2] replace binary search with llvm implementation insd of
std
---
clang/lib/Lex/Preprocessor.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 3f83546ea7956..41bc52174afd8 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -1746,11 +1746,9 @@ 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;
- auto P =
- std::upper_bound(FileCheckPoints.begin(), FileCheckPoints.end(), Start);
- if (P == FileCheckPoints.begin()) {
+ auto P = llvm::upper_bound(FileCheckPoints, Start);
+ if (P == FileCheckPoints.begin())
return nullptr;
- }
return *std::prev(P);
}
return nullptr;
More information about the cfe-commits
mailing list