@@ -335,6 +335,7 @@ void SourceManager::clearIDTables() {
335
335
LastLineNoFileIDQuery = FileID ();
336
336
LastLineNoContentCache = nullptr ;
337
337
LastFileIDLookup = FileID ();
338
+ LastLookupStartOffset = LastLookupEndOffset = 0 ;
338
339
339
340
IncludedLocMap.clear ();
340
341
if (LineTable)
@@ -630,9 +631,11 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
630
631
LocalSLocEntryTable.push_back (
631
632
SLocEntry::get (NextLocalOffset,
632
633
FileInfo::get (IncludePos, File, FileCharacter, Filename)));
634
+ LastLookupStartOffset = NextLocalOffset;
633
635
// We do a +1 here because we want a SourceLocation that means "the end of the
634
636
// file", e.g. for the "no newline at the end of the file" diagnostic.
635
637
NextLocalOffset += FileSize + 1 ;
638
+ LastLookupEndOffset = NextLocalOffset;
636
639
updateSlocUsageStats ();
637
640
638
641
// Set LastFileIDLookup to the newly created file. The next getFileID call is
@@ -802,8 +805,9 @@ FileID SourceManager::getFileIDSlow(SourceLocation::UIntTy SLocOffset) const {
802
805
// / loaded one.
803
806
FileID SourceManager::getFileIDLocal (SourceLocation::UIntTy SLocOffset) const {
804
807
assert (SLocOffset < NextLocalOffset && " Bad function choice" );
805
- assert (SLocOffset >= LocalSLocEntryTable[0 ].getOffset () &&
808
+ assert (SLocOffset >= LocalSLocEntryTable[0 ].getOffset () && SLocOffset > 0 &&
806
809
" Invalid SLocOffset" );
810
+ assert (LastFileIDLookup.ID >= 0 && " Only cache local file sloc entry" );
807
811
808
812
// After the first and second level caches, I see two common sorts of
809
813
// behavior: 1) a lot of searched FileID's are "near" the cached file
@@ -823,13 +827,11 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
823
827
unsigned LessIndex = 0 ;
824
828
// upper bound of the search range.
825
829
unsigned GreaterIndex = LocalSLocEntryTable.size ();
826
- if (LastFileIDLookup.ID >= 0 ) {
827
- // Use the LastFileIDLookup to prune the search space.
828
- if (LocalSLocEntryTable[LastFileIDLookup.ID ].getOffset () < SLocOffset)
829
- LessIndex = LastFileIDLookup.ID ;
830
- else
831
- GreaterIndex = LastFileIDLookup.ID ;
832
- }
830
+ // Use the LastFileIDLookup to prune the search space.
831
+ if (LastLookupStartOffset < SLocOffset)
832
+ LessIndex = LastFileIDLookup.ID ;
833
+ else
834
+ GreaterIndex = LastFileIDLookup.ID ;
833
835
834
836
// Find the FileID that contains this.
835
837
unsigned NumProbes = 0 ;
@@ -840,7 +842,12 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
840
842
FileID Res = FileID::get (int (GreaterIndex));
841
843
// Remember it. We have good locality across FileID lookups.
842
844
LastFileIDLookup = Res;
843
- NumLinearScans += NumProbes+1 ;
845
+ LastLookupStartOffset = LocalSLocEntryTable[GreaterIndex].getOffset ();
846
+ LastLookupEndOffset =
847
+ GreaterIndex + 1 >= LocalSLocEntryTable.size ()
848
+ ? NextLocalOffset
849
+ : LocalSLocEntryTable[GreaterIndex + 1 ].getOffset ();
850
+ NumLinearScans += NumProbes + 1 ;
844
851
return Res;
845
852
}
846
853
if (++NumProbes == 8 )
@@ -864,6 +871,8 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
864
871
// At this point, LessIndex is the index of the *first element greater than*
865
872
// SLocOffset. The element we are actually looking for is the one immediately
866
873
// before it.
874
+ LastLookupStartOffset = LocalSLocEntryTable[LessIndex - 1 ].getOffset ();
875
+ LastLookupEndOffset = LocalSLocEntryTable[LessIndex].getOffset ();
867
876
return LastFileIDLookup = FileID::get (LessIndex - 1 );
868
877
}
869
878
0 commit comments