Skip to content

Commit 50c6fcb

Browse files
committed
Cache the Offset for LastFileIDLookup.
1 parent 0588e81 commit 50c6fcb

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
767767
/// LastFileIDLookup records the last FileID looked up or created, because it
768768
/// is very common to look up many tokens from the same file.
769769
mutable FileID LastFileIDLookup;
770+
mutable SourceLocation::UIntTy LastLookupStartOffset;
771+
mutable SourceLocation::UIntTy LastLookupEndOffset; // exclude
770772

771773
/// Holds information for \#line directives.
772774
///
@@ -1901,9 +1903,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
19011903

19021904
FileID getFileID(SourceLocation::UIntTy SLocOffset) const {
19031905
// If our one-entry cache covers this offset, just return it.
1904-
if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1906+
if (SLocOffset >= LastLookupStartOffset && SLocOffset < LastLookupEndOffset)
19051907
return LastFileIDLookup;
1906-
19071908
return getFileIDSlow(SLocOffset);
19081909
}
19091910

clang/lib/Basic/SourceManager.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ void SourceManager::clearIDTables() {
334334
LastLineNoFileIDQuery = FileID();
335335
LastLineNoContentCache = nullptr;
336336
LastFileIDLookup = FileID();
337+
LastLookupStartOffset = LastLookupEndOffset = 0;
337338

338339
IncludedLocMap.clear();
339340
if (LineTable)
@@ -639,9 +640,11 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
639640
LocalSLocEntryTable.push_back(
640641
SLocEntry::get(NextLocalOffset,
641642
FileInfo::get(IncludePos, File, FileCharacter, Filename)));
643+
LastLookupStartOffset = NextLocalOffset;
642644
// We do a +1 here because we want a SourceLocation that means "the end of the
643645
// file", e.g. for the "no newline at the end of the file" diagnostic.
644646
NextLocalOffset += FileSize + 1;
647+
LastLookupEndOffset = NextLocalOffset;
645648
updateSlocUsageStats();
646649

647650
// Set LastFileIDLookup to the newly created file. The next getFileID call is
@@ -832,13 +835,11 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
832835
unsigned LessIndex = 0;
833836
// upper bound of the search range.
834837
unsigned GreaterIndex = LocalSLocEntryTable.size();
835-
if (LastFileIDLookup.ID >= 0) {
836-
// Use the LastFileIDLookup to prune the search space.
837-
if (LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset)
838-
LessIndex = LastFileIDLookup.ID;
839-
else
840-
GreaterIndex = LastFileIDLookup.ID;
841-
}
838+
// Use the LastFileIDLookup to prune the search space.
839+
if (LastLookupStartOffset < SLocOffset)
840+
LessIndex = LastFileIDLookup.ID;
841+
else
842+
GreaterIndex = LastFileIDLookup.ID;
842843

843844
// Find the FileID that contains this.
844845
unsigned NumProbes = 0;
@@ -849,7 +850,12 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
849850
FileID Res = FileID::get(int(GreaterIndex));
850851
// Remember it. We have good locality across FileID lookups.
851852
LastFileIDLookup = Res;
852-
NumLinearScans += NumProbes+1;
853+
LastLookupStartOffset = LocalSLocEntryTable[GreaterIndex].getOffset();
854+
LastLookupEndOffset =
855+
GreaterIndex + 1 >= LocalSLocEntryTable.size()
856+
? NextLocalOffset
857+
: LocalSLocEntryTable[GreaterIndex + 1].getOffset();
858+
NumLinearScans += NumProbes + 1;
853859
return Res;
854860
}
855861
if (++NumProbes == 8)
@@ -873,6 +879,8 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
873879
// At this point, LessIndex is the index of the *first element greater than*
874880
// SLocOffset. The element we are actually looking for is the one immediately
875881
// before it.
882+
LastLookupStartOffset = LocalSLocEntryTable[LessIndex - 1].getOffset();
883+
LastLookupEndOffset = LocalSLocEntryTable[LessIndex].getOffset();
876884
return LastFileIDLookup = FileID::get(LessIndex - 1);
877885
}
878886

0 commit comments

Comments
 (0)