@@ -811,6 +811,8 @@ FileID SourceManager::getFileIDSlow(SourceLocation::UIntTy SLocOffset) const {
811
811
// / loaded one.
812
812
FileID SourceManager::getFileIDLocal (SourceLocation::UIntTy SLocOffset) const {
813
813
assert (SLocOffset < NextLocalOffset && " Bad function choice" );
814
+ assert (SLocOffset >= LocalSLocEntryTable[0 ].getOffset () &&
815
+ " Invalid SLocOffset" );
814
816
815
817
// After the first and second level caches, I see two common sorts of
816
818
// behavior: 1) a lot of searched FileID's are "near" the cached file
@@ -854,35 +856,24 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
854
856
break ;
855
857
}
856
858
857
- NumProbes = 0 ;
858
- while (true ) {
859
- unsigned MiddleIndex = (GreaterIndex-LessIndex)/2 +LessIndex;
860
- SourceLocation::UIntTy MidOffset =
861
- getLocalSLocEntry (MiddleIndex).getOffset ();
862
-
863
- ++NumProbes;
864
-
865
- // If the offset of the midpoint is too large, chop the high side of the
866
- // range to the midpoint.
867
- if (MidOffset > SLocOffset) {
868
- GreaterIndex = MiddleIndex;
869
- continue ;
870
- }
859
+ while (LessIndex < GreaterIndex) {
860
+ ++NumBinaryProbes;
871
861
872
- // If the middle index contains the value, succeed and return.
873
- if (MiddleIndex + 1 == LocalSLocEntryTable.size () ||
874
- SLocOffset < getLocalSLocEntry (MiddleIndex + 1 ).getOffset ()) {
875
- FileID Res = FileID::get (MiddleIndex);
862
+ unsigned MiddleIndex = LessIndex + (GreaterIndex - LessIndex) / 2 ;
876
863
877
- // Remember it. We have good locality across FileID lookups.
878
- LastFileIDLookup = Res;
879
- NumBinaryProbes += NumProbes;
880
- return Res;
881
- }
864
+ SourceLocation::UIntTy MidOffset =
865
+ LocalSLocEntryTable[MiddleIndex].getOffset ();
882
866
883
- // Otherwise, move the low-side up to the middle index.
884
- LessIndex = MiddleIndex;
867
+ if (MidOffset <= SLocOffset)
868
+ LessIndex = MiddleIndex + 1 ;
869
+ else
870
+ GreaterIndex = MiddleIndex;
885
871
}
872
+
873
+ // At this point, LessIndex is the index of the *first element greater than*
874
+ // SLocOffset. The element we are actually looking for is the one immediately
875
+ // before it.
876
+ return LastFileIDLookup = FileID::get (LessIndex - 1 );
886
877
}
887
878
888
879
// / Return the FileID for a SourceLocation with a high offset.
0 commit comments