[clang] [clang][Lex] Optimize the FileCheckPoints search in Preprocessor (PR #206356)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 28 11:20:42 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Hardik Kumar (hardikxk)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/206356.diff
1 Files Affected:
- (modified) clang/lib/Lex/Preprocessor.cpp (+5-8)
``````````diff
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;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/206356
More information about the cfe-commits
mailing list