
Reland https://github.com/llvm/llvm-project/pull/83237 --- (Original comments) Currently all the specializations of a template (including instantiation, specialization and partial specializations) will be loaded at once if we want to instantiate another instance for the template, or find instantiation for the template, or just want to complete the redecl chain. This means basically we need to load every specializations for the template once the template declaration got loaded. This is bad since when we load a specialization, we need to load all of its template arguments. Then we have to deserialize a lot of unnecessary declarations. For example, ``` // M.cppm export module M; export template <class T> class A {}; export class ShouldNotBeLoaded {}; export class Temp { A<ShouldNotBeLoaded> AS; }; // use.cpp import M; A<int> a; ``` We have a specialization ` A<ShouldNotBeLoaded>` in `M.cppm` and we instantiate the template `A` in `use.cpp`. Then we will deserialize `ShouldNotBeLoaded` surprisingly when compiling `use.cpp`. And this patch tries to avoid that. Given that the templates are heavily used in C++, this is a pain point for the performance. This patch adds MultiOnDiskHashTable for specializations in the ASTReader. Then we will only deserialize the specializations with the same template arguments. We made that by using ODRHash for the template arguments as the key of the hash table. To review this patch, I think `ASTReaderDecl::AddLazySpecializations` may be a good entry point.
41 lines
1.0 KiB
C++
41 lines
1.0 KiB
C++
// RUN: rm -rf %t
|
|
// RUN: mkdir -p %t
|
|
// RUN: split-file %s %t
|
|
//
|
|
// RUN: %clang_cc1 -std=c++20 %t/type_traits.cppm -emit-module-interface -o %t/type_traits.pcm
|
|
// RUN: %clang_cc1 -std=c++20 %t/test.cpp -fprebuilt-module-path=%t -verify
|
|
|
|
//--- type_traits.cppm
|
|
export module type_traits;
|
|
|
|
export template <typename T>
|
|
constexpr bool is_pod_v = __is_pod(T);
|
|
|
|
//--- test.cpp
|
|
// expected-no-diagnostics
|
|
import type_traits;
|
|
// Base is either void or wrapper<T>.
|
|
template <class Base> struct wrapper : Base {};
|
|
template <> struct wrapper<void> {};
|
|
|
|
// wrap<0>::type<T> is wrapper<T>, wrap<1>::type<T> is wrapper<wrapper<T>>,
|
|
// and so on.
|
|
template <int N>
|
|
struct wrap {
|
|
template <class Base>
|
|
using type = wrapper<typename wrap<N-1>::template type<Base>>;
|
|
};
|
|
|
|
template <>
|
|
struct wrap<0> {
|
|
template <class Base>
|
|
using type = wrapper<Base>;
|
|
};
|
|
|
|
inline constexpr int kMaxRank = 40;
|
|
template <int N, class Base = void>
|
|
using rank = typename wrap<N>::template type<Base>;
|
|
using rank_selector_t = rank<kMaxRank>;
|
|
|
|
static_assert(is_pod_v<rank_selector_t>, "Must be POD");
|