diff options
author | Haojian Wu <hokein.wu@gmail.com> | 2022-10-02 11:54:05 +0200 |
---|---|---|
committer | Haojian Wu <hokein.wu@gmail.com> | 2022-10-07 09:37:04 +0200 |
commit | a6a0d9ecd5d744316e699fa78a053376bb659dd1 (patch) | |
tree | 17b95cdcd4913d36d2584f77073d36a3de37852f /clang/lib/Basic/SourceManager.cpp | |
parent | 640946e1927999c14d6c42bf1fa384f28bb7a5e9 (diff) | |
download | llvm-a6a0d9ecd5d744316e699fa78a053376bb659dd1.zip llvm-a6a0d9ecd5d744316e699fa78a053376bb659dd1.tar.gz llvm-a6a0d9ecd5d744316e699fa78a053376bb659dd1.tar.bz2 |
[SourceManager] Improve getFileIDLocal.
Prune the search space -- If we know offset(LastFileIDLookup) < SearchOffset, we
can prune the initial binary-search range from [0, end) to [LastFileIDlookup, end).
It reduces the binary search scan by ~30%.
SemaExpr.cpp: 1393437 -> 1035426
FindTarget.cpp: 1275930 -> 920087
Linux kernel:
getFileIDLocal: 2.45% -> 2.15%
Differential Revision: https://reviews.llvm.org/D135132
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 32234b5..f82df59 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -793,24 +793,28 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const { // See if this is near the file point - worst case we start scanning from the // most newly created FileID. - const SrcMgr::SLocEntry *I; - if (LastFileIDLookup.ID < 0 || - LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { - // Neither loc prunes our search. - I = LocalSLocEntryTable.end(); - } else { - // Perhaps it is near the file point. - I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID; + // LessIndex - This is the lower bound of the range that we're searching. + // We know that the offset corresponding to the FileID is is less than + // SLocOffset. + unsigned LessIndex = 0; + // upper bound of the search range. + unsigned GreaterIndex = LocalSLocEntryTable.size(); + if (LastFileIDLookup.ID >= 0) { + // Use the LastFileIDLookup to prune the search space. + if (LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) + LessIndex = LastFileIDLookup.ID; + else + GreaterIndex = LastFileIDLookup.ID; } - // Find the FileID that contains this. "I" is an iterator that points to a - // FileID whose offset is known to be larger than SLocOffset. + // Find the FileID that contains this. unsigned NumProbes = 0; while (true) { - --I; - if (I->getOffset() <= SLocOffset) { - FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin())); + --GreaterIndex; + assert(GreaterIndex < LocalSLocEntryTable.size()); + if (LocalSLocEntryTable[GreaterIndex].getOffset() <= SLocOffset) { + FileID Res = FileID::get(int(GreaterIndex)); // Remember it. We have good locality across FileID lookups. LastFileIDLookup = Res; NumLinearScans += NumProbes+1; @@ -820,13 +824,6 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const { break; } - // Convert "I" back into an index. We know that it is an entry whose index is - // larger than the offset we are looking for. - unsigned GreaterIndex = I - LocalSLocEntryTable.begin(); - // LessIndex - This is the lower bound of the range that we're searching. - // We know that the offset corresponding to the FileID is is less than - // SLocOffset. - unsigned LessIndex = 0; NumProbes = 0; while (true) { unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |