Speed up compilation of ASTImporter

Avoid recursively instantiating importSeq. Use initializer list
expansion to stamp out a single instantiation of std::tuple of the
deduced sequence of types, and thread the error around that tuple type.
Avoids needlessly instantiating std::tuple N-1 times.

new time to compile: 0m25.985s
old time to compile: 0m35.563s

new obj size: 10,000kb
old obj size: 12,332kb

I found the slow TU by looking at ClangBuildAnalyzer results, and looked
at -ftime-trace for the file in chrome://tracing to find this.

Tested with: clang-cl, MSVC, and GCC.

Reviewed By: martong

Differential Revision: https://reviews.llvm.org/D73667
This commit is contained in:
Reid Kleckner 2020-01-29 14:29:34 -08:00
parent b0d25fff9b
commit af3e884956
1 changed files with 19 additions and 18 deletions

View File

@ -186,32 +186,33 @@ namespace clang {
return import(*From);
}
template <class T>
Expected<std::tuple<T>>
importSeq(const T &From) {
Expected<T> ToOrErr = import(From);
if (!ToOrErr)
return ToOrErr.takeError();
return std::make_tuple<T>(std::move(*ToOrErr));
// Helper for error management in importSeq.
template <typename T> T checkImport(Error &Err, const T &From) {
// Don't attempt to import nodes if we hit an error earlier.
if (Err)
return T{};
Expected<T> MaybeVal = import(From);
if (!MaybeVal) {
Err = MaybeVal.takeError();
return T{};
}
return *MaybeVal;
}
// Import multiple objects with a single function call.
// This should work for every type for which a variant of `import` exists.
// The arguments are processed from left to right and import is stopped on
// first error.
template <class THead, class... TTail>
Expected<std::tuple<THead, TTail...>>
importSeq(const THead &FromHead, const TTail &...FromTail) {
Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
if (!ToHeadOrErr)
return ToHeadOrErr.takeError();
Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
if (!ToTailOrErr)
return ToTailOrErr.takeError();
return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
template <class... Args>
Expected<std::tuple<Args...>> importSeq(const Args &... args) {
Error E = Error::success();
std::tuple<Args...> Res{checkImport(E, args)...};
if (E)
return std::move(E);
return std::move(Res);
}
// Wrapper for an overload set.
// Wrapper for an overload set.
template <typename ToDeclT> struct CallOverloadedCreateFun {
template <typename... Args>
auto operator()(Args &&... args)