diff options
author | Nick Desaulniers <ndesaulniers@google.com> | 2020-06-25 09:53:11 -0700 |
---|---|---|
committer | Nick Desaulniers <ndesaulniers@google.com> | 2020-06-25 09:59:41 -0700 |
commit | 408efffbe4a52bae05f1677a47eb3ccfd5cdc1d3 (patch) | |
tree | 1ac898b755661860c3686de964a0dae4966a2c42 /clang/lib/Basic/SourceManager.cpp | |
parent | ed8184b7814df4310dbad065a9a1c3bb8f3bfa86 (diff) | |
download | llvm-408efffbe4a52bae05f1677a47eb3ccfd5cdc1d3.zip llvm-408efffbe4a52bae05f1677a47eb3ccfd5cdc1d3.tar.gz llvm-408efffbe4a52bae05f1677a47eb3ccfd5cdc1d3.tar.bz2 |
[Clang][SourceManager] optimize getFileIDLocal()
Summary:
A recent Linux kernel commit exposed a performance cliff in Clang. Calls
to SourceManager::getFileIDLocal() when there's a cache miss against
LastFileIDLookup can be relatively expensive, as getFileIDLocal() tries
a few linear probes, then falls back to binary search. The use of
SourceManager::isOffsetInFileID() is also relatively expensive (both
isOffsetInFileID and getFileIDLocal dominated a trace of the performance
cliff case).
As a FIXME notes (and as @kadircet helpfully noted in review of D80681),
there's a few optimizations we can do here since we've already
identified that an offset is local (as opposed to "loaded").
This patch was forked off of D80681, which additionally did this and
modified some caching behavior, as we expect this change to be less
controversial.
In terms of optimizations, we've already determined that the SLocOffset
parameter to SourceManager::getFileIDLocal() is local in the caller
SourceManager::getFileIDSlow(). Also, there's an early continue in the
binary search loop in getFileIDLocal() that are duplicated in
isOffsetInFileID() as pointed out by @kadircet.
Take advantage of these to optimize the binary search patch, and remove
the FIXME.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: cfe-commits, kadircet, srhines
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82497
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 6e4c8e8..63518bf 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -898,9 +898,8 @@ FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const { } // If the middle index contains the value, succeed and return. - // FIXME: This could be made faster by using a function that's aware of - // being in the local area. - if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { + if (MiddleIndex + 1 == LocalSLocEntryTable.size() || + SLocOffset < getLocalSLocEntry(MiddleIndex + 1).getOffset()) { FileID Res = FileID::get(MiddleIndex); // If this isn't a macro expansion, remember it. We have good locality |