Skip to content

Commit 2f95fc4

Browse files
committed
[AST] NFC: Do not reinterpret_cast 'Type'
Found while doing a performance experiment with the 'Type' type wall.
1 parent 7c096c9 commit 2f95fc4

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

include/swift/AST/ExistentialLayout.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_EXISTENTIAL_LAYOUT_H
1818
#define SWIFT_EXISTENTIAL_LAYOUT_H
1919

20+
#include "swift/Basic/ArrayRefView.h"
2021
#include "swift/AST/ASTContext.h"
2122
#include "swift/AST/Type.h"
2223
#include "llvm/ADT/SmallVector.h"
@@ -66,21 +67,24 @@ struct ExistentialLayout {
6667
// constraints?
6768
bool isErrorExistential() const;
6869

69-
ArrayRef<ProtocolType *> getProtocols() const {
70-
if (singleProtocol)
71-
return ArrayRef<ProtocolType *>{&singleProtocol, 1};
72-
return multipleProtocols;
70+
static inline ProtocolType *getProtocolType(const Type &Ty) {
71+
return cast<ProtocolType>(Ty.getPointer());
72+
}
73+
typedef ArrayRefView<Type,ProtocolType*,getProtocolType> ProtocolTypeArrayRef;
74+
75+
ProtocolTypeArrayRef getProtocols() const {
76+
return protocols;
7377
}
7478

7579
LayoutConstraint getLayoutConstraint() const;
7680

7781
private:
7882
// Inline storage for 'protocols' member above when computing
7983
// layout of a single ProtocolType
80-
ProtocolType *singleProtocol;
84+
Type singleProtocol;
8185

8286
/// Zero or more protocol constraints.
83-
ArrayRef<ProtocolType *> multipleProtocols;
87+
ArrayRef<Type> protocols;
8488
};
8589

8690
}

lib/AST/Type.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ ExistentialLayout::ExistentialLayout(ProtocolType *type) {
249249
containsNonObjCProtocol = !protoDecl->isObjC();
250250

251251
singleProtocol = type;
252+
protocols = { &singleProtocol, 1 };
252253
}
253254

254255
ExistentialLayout::ExistentialLayout(ProtocolCompositionType *type) {
@@ -270,10 +271,7 @@ ExistentialLayout::ExistentialLayout(ProtocolCompositionType *type) {
270271
}
271272

272273
singleProtocol = nullptr;
273-
multipleProtocols = {
274-
reinterpret_cast<ProtocolType * const *>(members.data()),
275-
members.size()
276-
};
274+
protocols = { members.data(), members.size() };
277275
}
278276

279277

lib/ParseSIL/ParseSIL.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ bool SILParser::parseSubstitutions(SmallVectorImpl<ParsedSubstitution> &parsed,
15291529
/// Collect conformances by looking up the conformance from replacement
15301530
/// type and protocol decl.
15311531
static bool getConformancesForSubstitution(Parser &P,
1532-
ArrayRef<ProtocolType *> protocols,
1532+
ExistentialLayout::ProtocolTypeArrayRef protocols,
15331533
Type subReplacement,
15341534
SourceLoc loc,
15351535
SmallVectorImpl<ProtocolConformanceRef> &conformances) {
@@ -1578,11 +1578,12 @@ bool getApplySubstitutionsFromParsed(
15781578
parses = parses.slice(1);
15791579

15801580
SmallVector<ProtocolConformanceRef, 2> conformances;
1581-
SmallVector<ProtocolType *, 2> protocols;
1581+
SmallVector<Type, 2> protocols;
15821582
for (auto reqt : reqts)
1583-
protocols.push_back(reqt.getSecondType()->castTo<ProtocolType>());
1583+
protocols.push_back(reqt.getSecondType());
15841584

1585-
if (getConformancesForSubstitution(SP.P, protocols,
1585+
if (getConformancesForSubstitution(SP.P,
1586+
ExistentialLayout::ProtocolTypeArrayRef(protocols),
15861587
parsed.replacement,
15871588
parsed.loc, conformances))
15881589
return true;

0 commit comments

Comments
 (0)