Cache results of visible decl lookup at top-level in a TranslationUnit.

This is a tradeoff, assuming that lookup in a particular module is less
likely to be repeated than lookup within the current source file. By
storing the cached results with the TU's own local lookup cache, the
results get cleared out when we parse additional decls.

Since this is only used by code completion, there's no performance benefit
for the compiler, or even for swift-ide-test...it's only in repeated lookups
that this will be useful.

Swift SVN r7169
This commit is contained in:
Jordan Rose
2013-08-12 18:54:59 +00:00
parent c4990cfc25
commit ae67a7719a
3 changed files with 29 additions and 3 deletions

View File

@@ -299,6 +299,9 @@ public:
void clearLookupCache();
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;
const SmallVectorImpl<ValueDecl *> &getCachedVisibleDecls() const;
void dump() const;
/// \brief Pretty-print the entire contents of this translation unit.

View File

@@ -489,16 +489,22 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
DC = DC->getParent();
}
if (Loc.isValid()) {
if (auto TU = dyn_cast<TranslationUnit>(&M)) {
if (auto TU = dyn_cast<TranslationUnit>(&M)) {
if (Loc.isValid()) {
// Look for local variables in top-level code; normally, the parser
// resolves these for us, but it can't do the right thing for
// local types.
FindLocalVal(SM, Loc, Consumer).checkTranslationUnit(TU);
}
auto &cached = TU->getCachedVisibleDecls();
if (!cached.empty()) {
for (auto result : cached)
Consumer.foundDecl(result);
return;
}
}
// FIXME: Cache TU-level visible decls, since those shouldn't change.
using namespace namelookup;
SmallVector<ValueDecl *, 0> moduleResults;
auto &mutableM = const_cast<Module&>(M);
@@ -507,6 +513,10 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
ResolutionKind::Overloadable);
for (auto result : moduleResults)
Consumer.foundDecl(result);
if (auto TU = dyn_cast<TranslationUnit>(&M)) {
TU->cacheVisibleDecls(std::move(moduleResults));
}
}
void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer, Type BaseTy,

View File

@@ -104,6 +104,8 @@ namespace {
VisibleDeclConsumer &Consumer,
NLKind LookupKind,
const TranslationUnit &TU);
SmallVector<ValueDecl *, 0> AllVisibleValues;
};
} // end anonymous namespace.
@@ -416,6 +418,17 @@ void TranslationUnit::clearLookupCache() {
freeTUCachePimpl(LookupCachePimpl);
}
void
TranslationUnit::cacheVisibleDecls(SmallVectorImpl<ValueDecl*> &&globals) const{
auto &cached = getTUCachePimpl(LookupCachePimpl, *this).AllVisibleValues;
static_cast<SmallVectorImpl<ValueDecl*>&>(cached) = std::move(globals);
}
const SmallVectorImpl<ValueDecl *> &
TranslationUnit::getCachedVisibleDecls() const {
return getTUCachePimpl(LookupCachePimpl, *this).AllVisibleValues;
}
//===----------------------------------------------------------------------===//
// LoadedModule Implementation
//===----------------------------------------------------------------------===//