Skip to content

Commit 7f0d756

Browse files
authored
Merge pull request swiftlang#23323 from davezarzycki/shrink_ast_densemaps
Shrink AST DenseMaps
2 parents 00874e9 + 2fef830 commit 7f0d756

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,9 @@ enum class MetatypeRepresentation : char {
23322332
/// which permit dynamic behavior.
23332333
Thick,
23342334
/// An Objective-C metatype refers to an Objective-C class object.
2335-
ObjC
2335+
ObjC,
2336+
2337+
Last_MetatypeRepresentation = ObjC
23362338
};
23372339

23382340
/// AnyMetatypeType - A common parent class of MetatypeType and

lib/AST/ASTContext.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,22 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
301301
/// Structure that captures data that is segregated into different
302302
/// arenas.
303303
struct Arena {
304+
static_assert(alignof(TypeBase) >= 8, "TypeBase not 8-byte aligned?");
305+
static_assert(alignof(TypeBase) > static_cast<unsigned>(
306+
MetatypeRepresentation::Last_MetatypeRepresentation) + 1,
307+
"Use std::pair for MetatypeTypes and ExistentialMetatypeTypes.");
308+
304309
llvm::DenseMap<Type, ErrorType *> ErrorTypesWithOriginal;
305310
llvm::FoldingSet<TypeAliasType> TypeAliasTypes;
306311
llvm::FoldingSet<TupleType> TupleTypes;
307-
llvm::DenseMap<std::pair<Type,char>, MetatypeType*> MetatypeTypes;
308-
llvm::DenseMap<std::pair<Type,char>,
312+
llvm::DenseMap<llvm::PointerIntPair<TypeBase*, 3, unsigned>,
313+
MetatypeType*> MetatypeTypes;
314+
llvm::DenseMap<llvm::PointerIntPair<TypeBase*, 3, unsigned>,
309315
ExistentialMetatypeType*> ExistentialMetatypeTypes;
310316
llvm::DenseMap<Type, ArraySliceType*> ArraySliceTypes;
311317
llvm::DenseMap<std::pair<Type, Type>, DictionaryType *> DictionaryTypes;
312318
llvm::DenseMap<Type, OptionalType*> OptionalTypes;
319+
llvm::DenseMap<Type, ParenType*> SimpleParenTypes; // Most are simple
313320
llvm::DenseMap<std::pair<Type, unsigned>, ParenType*> ParenTypes;
314321
llvm::DenseMap<uintptr_t, ReferenceStorageType*> ReferenceStorageTypes;
315322
llvm::DenseMap<Type, LValueType*> LValueTypes;
@@ -2088,6 +2095,7 @@ size_t ASTContext::Implementation::Arena::getTotalMemory() const {
20882095
llvm::capacity_in_bytes(ArraySliceTypes) +
20892096
llvm::capacity_in_bytes(DictionaryTypes) +
20902097
llvm::capacity_in_bytes(OptionalTypes) +
2098+
llvm::capacity_in_bytes(SimpleParenTypes) +
20912099
llvm::capacity_in_bytes(ParenTypes) +
20922100
llvm::capacity_in_bytes(ReferenceStorageTypes) +
20932101
llvm::capacity_in_bytes(LValueTypes) +
@@ -3045,8 +3053,10 @@ ParenType *ParenType::get(const ASTContext &C, Type underlying,
30453053

30463054
auto properties = underlying->getRecursiveProperties();
30473055
auto arena = getArena(properties);
3048-
ParenType *&Result =
3049-
C.getImpl().getArena(arena).ParenTypes[{underlying, fl.toRaw()}];
3056+
auto flags = fl.toRaw();
3057+
ParenType *&Result = flags == 0
3058+
? C.getImpl().getArena(arena).SimpleParenTypes[underlying]
3059+
: C.getImpl().getArena(arena).ParenTypes[{underlying, flags}];
30503060
if (Result == nullptr) {
30513061
Result = new (C, arena) ParenType(underlying,
30523062
properties, fl);
@@ -3492,13 +3502,16 @@ MetatypeType *MetatypeType::get(Type T, Optional<MetatypeRepresentation> Repr,
34923502
auto properties = T->getRecursiveProperties();
34933503
auto arena = getArena(properties);
34943504

3495-
char reprKey;
3505+
unsigned reprKey;
34963506
if (Repr.hasValue())
3497-
reprKey = static_cast<char>(*Repr) + 1;
3507+
reprKey = static_cast<unsigned>(*Repr) + 1;
34983508
else
34993509
reprKey = 0;
35003510

3501-
MetatypeType *&Entry = Ctx.getImpl().getArena(arena).MetatypeTypes[{T, reprKey}];
3511+
auto pair = llvm::PointerIntPair<TypeBase*, 3, unsigned>(T.getPointer(),
3512+
reprKey);
3513+
3514+
MetatypeType *&Entry = Ctx.getImpl().getArena(arena).MetatypeTypes[pair];
35023515
if (Entry) return Entry;
35033516

35043517
return Entry = new (Ctx, arena) MetatypeType(
@@ -3517,13 +3530,16 @@ ExistentialMetatypeType::get(Type T, Optional<MetatypeRepresentation> repr,
35173530
auto properties = T->getRecursiveProperties();
35183531
auto arena = getArena(properties);
35193532

3520-
char reprKey;
3533+
unsigned reprKey;
35213534
if (repr.hasValue())
3522-
reprKey = static_cast<char>(*repr) + 1;
3535+
reprKey = static_cast<unsigned>(*repr) + 1;
35233536
else
35243537
reprKey = 0;
35253538

3526-
auto &entry = ctx.getImpl().getArena(arena).ExistentialMetatypeTypes[{T, reprKey}];
3539+
auto pair = llvm::PointerIntPair<TypeBase*, 3, unsigned>(T.getPointer(),
3540+
reprKey);
3541+
3542+
auto &entry = ctx.getImpl().getArena(arena).ExistentialMetatypeTypes[pair];
35273543
if (entry) return entry;
35283544

35293545
return entry = new (ctx, arena) ExistentialMetatypeType(

0 commit comments

Comments
 (0)