Skip to content

Commit f3b207e

Browse files
committed
Serialization: Don't serialize types of various decls
We can recover function, destructor, constructor, enum element and subscript types from their parameter types and result type (if present), by calling the AST's various computeType() methods. These methods don't do any more work than would be done if we had deserialized the type and reconstructed it, so let's just recompute it instead. Note that we still serialize a ton of function types, due to XREFs. With my build configuration, this reduces the size of Swift.swiftmodule by 237KiB (out of 20MiB).
1 parent cd4909b commit f3b207e

File tree

3 files changed

+32
-59
lines changed

3 files changed

+32
-59
lines changed

include/swift/Serialization/ModuleFormat.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t VERSION_MINOR = 433; // Last change: GenericTypeParamDecl doesn't need a DC
58+
const uint16_t VERSION_MINOR = 434; // Last change: don't serialize derivable interface types
5959

6060
using DeclIDField = BCFixed<31>;
6161

@@ -965,7 +965,6 @@ namespace decls_block {
965965
BCFixed<1>, // throws?
966966
CtorInitializerKindField, // initializer kind
967967
GenericEnvironmentIDField, // generic environment
968-
TypeIDField, // interface type
969968
DeclIDField, // overridden decl
970969
AccessLevelField, // access level
971970
BCFixed<1>, // requires a new vtable slot
@@ -1024,7 +1023,7 @@ namespace decls_block {
10241023
BCFixed<1>, // has forced static dispatch?
10251024
BCFixed<1>, // throws?
10261025
GenericEnvironmentIDField, // generic environment
1027-
TypeIDField, // interface type
1026+
TypeIDField, // result interface type
10281027
DeclIDField, // operator decl
10291028
DeclIDField, // overridden function
10301029
BCVBR<5>, // 0 for a simple name, otherwise the number of parameter name
@@ -1053,7 +1052,7 @@ namespace decls_block {
10531052
BCFixed<1>, // has forced static dispatch?
10541053
BCFixed<1>, // throws?
10551054
GenericEnvironmentIDField, // generic environment
1056-
TypeIDField, // interface type
1055+
TypeIDField, // result interface type
10571056
DeclIDField, // overridden function
10581057
DeclIDField, // AccessorStorageDecl
10591058
AccessorKindField, // accessor kind
@@ -1110,7 +1109,6 @@ namespace decls_block {
11101109
using EnumElementLayout = BCRecordLayout<
11111110
ENUM_ELEMENT_DECL,
11121111
DeclContextIDField,// context decl
1113-
TypeIDField, // interface type
11141112
BCFixed<1>, // implicit?
11151113
BCFixed<1>, // has payload?
11161114
EnumElementRawValueKindField, // raw value kind
@@ -1136,7 +1134,7 @@ namespace decls_block {
11361134
ReadWriteImplKindField, // read-write implementation
11371135
AccessorCountField, // number of accessors
11381136
GenericEnvironmentIDField, // generic environment
1139-
TypeIDField, // interface type
1137+
TypeIDField, // element interface type
11401138
DeclIDField, // overridden decl
11411139
AccessLevelField, // access level
11421140
AccessLevelField, // setter access, if applicable
@@ -1167,9 +1165,7 @@ namespace decls_block {
11671165
DeclContextIDField, // context decl
11681166
BCFixed<1>, // implicit?
11691167
BCFixed<1>, // objc?
1170-
GenericEnvironmentIDField, // generic environment
1171-
TypeIDField // interface type
1172-
// Trailed by a pattern for self.
1168+
GenericEnvironmentIDField // generic environment
11731169
>;
11741170

11751171
using ParameterListLayout = BCRecordLayout<
@@ -1257,8 +1253,8 @@ namespace decls_block {
12571253
using GenericRequirementLayout = BCRecordLayout<
12581254
GENERIC_REQUIREMENT,
12591255
GenericRequirementKindField, // requirement kind
1260-
TypeIDField, // types involved (two for conformance,
1261-
TypeIDField // same-type; one for value witness marker)
1256+
TypeIDField, // subject type
1257+
TypeIDField // constraint type
12621258
>;
12631259

12641260
using LayoutRequirementLayout = BCRecordLayout<

lib/Serialization/Deserialization.cpp

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,7 +2724,6 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
27242724
bool isImplicit, isObjC, hasStubImplementation, throws;
27252725
GenericEnvironmentID genericEnvID;
27262726
uint8_t storedInitKind, rawAccessLevel;
2727-
TypeID interfaceID;
27282727
DeclID overriddenID;
27292728
bool needsNewVTableEntry, firstTimeRequired;
27302729
uint8_t rawDefaultArgumentResilienceExpansion;
@@ -2735,7 +2734,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
27352734
rawFailability, isImplicit,
27362735
isObjC, hasStubImplementation,
27372736
throws, storedInitKind,
2738-
genericEnvID, interfaceID,
2737+
genericEnvID,
27392738
overriddenID,
27402739
rawAccessLevel,
27412740
needsNewVTableEntry,
@@ -2819,23 +2818,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
28192818
assert(bodyParams && "missing parameters for constructor");
28202819
ctor->setParameters(selfDecl, bodyParams);
28212820

2822-
auto interfaceType = getType(interfaceID);
2823-
ctor->setInterfaceType(interfaceType);
2824-
2825-
// Set the initializer interface type of the constructor.
2826-
auto allocType = ctor->getInterfaceType();
2827-
auto selfParam = computeSelfParam(ctor, /*isInitializingCtor=*/true);
2828-
if (auto polyFn = allocType->getAs<GenericFunctionType>()) {
2829-
ctor->setInitializerInterfaceType(
2830-
GenericFunctionType::get(polyFn->getGenericSignature(),
2831-
{selfParam}, polyFn->getResult(),
2832-
polyFn->getExtInfo()));
2833-
} else {
2834-
auto fn = allocType->castTo<FunctionType>();
2835-
ctor->setInitializerInterfaceType(FunctionType::get({selfParam},
2836-
fn->getResult(),
2837-
fn->getExtInfo()));
2838-
}
2821+
ctor->computeType();
28392822

28402823
if (auto errorConvention = maybeReadForeignErrorConvention())
28412824
ctor->setForeignErrorConvention(*errorConvention);
@@ -3041,7 +3024,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
30413024
bool isObjC, hasDynamicSelf, hasForcedStaticDispatch, throws;
30423025
unsigned numNameComponentsBiased;
30433026
GenericEnvironmentID genericEnvID;
3044-
TypeID interfaceTypeID;
3027+
TypeID resultInterfaceTypeID;
30453028
DeclID associatedDeclID;
30463029
DeclID overriddenID;
30473030
DeclID accessorStorageDeclID;
@@ -3055,7 +3038,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
30553038
rawMutModifier, hasDynamicSelf,
30563039
hasForcedStaticDispatch, throws,
30573040
genericEnvID,
3058-
interfaceTypeID,
3041+
resultInterfaceTypeID,
30593042
associatedDeclID, overriddenID,
30603043
numNameComponentsBiased,
30613044
rawAccessLevel,
@@ -3068,7 +3051,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
30683051
rawMutModifier, hasDynamicSelf,
30693052
hasForcedStaticDispatch, throws,
30703053
genericEnvID,
3071-
interfaceTypeID,
3054+
resultInterfaceTypeID,
30723055
overriddenID,
30733056
accessorStorageDeclID,
30743057
rawAccessorKind, rawAddressorKind,
@@ -3221,9 +3204,9 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
32213204
}
32223205
}
32233206

3224-
// Set the interface type.
3225-
auto interfaceType = getType(interfaceTypeID);
3226-
fn->setInterfaceType(interfaceType);
3207+
fn->setStatic(isStatic);
3208+
3209+
fn->getBodyResultTypeLoc().setType(getType(resultInterfaceTypeID));
32273210

32283211
ParamDecl *selfDecl = nullptr;
32293212
if (DC->isTypeContext()) {
@@ -3237,6 +3220,9 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
32373220

32383221
fn->setParameters(selfDecl, paramList);
32393222

3223+
// Set the interface type.
3224+
fn->computeType();
3225+
32403226
if (auto errorConvention = maybeReadForeignErrorConvention())
32413227
fn->setForeignErrorConvention(*errorConvention);
32423228

@@ -3245,7 +3231,6 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
32453231
AddAttribute(new (ctx) OverrideAttr(SourceLoc()));
32463232
}
32473233

3248-
fn->setStatic(isStatic);
32493234
if (isImplicit)
32503235
fn->setImplicit();
32513236
fn->setIsObjC(isObjC);
@@ -3644,7 +3629,6 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
36443629

36453630
case decls_block::ENUM_ELEMENT_DECL: {
36463631
DeclContextID contextID;
3647-
TypeID interfaceTypeID;
36483632
bool isImplicit; bool hasPayload; bool isNegative;
36493633
unsigned rawValueKindID;
36503634
IdentifierID blobData;
@@ -3653,7 +3637,6 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
36533637
ArrayRef<uint64_t> argNameAndDependencyIDs;
36543638

36553639
decls_block::EnumElementLayout::readRecord(scratch, contextID,
3656-
interfaceTypeID,
36573640
isImplicit, hasPayload,
36583641
rawValueKindID, isNegative,
36593642
blobData,
@@ -3710,8 +3693,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
37103693
}
37113694
}
37123695

3713-
auto interfaceType = getType(interfaceTypeID);
3714-
elem->setInterfaceType(interfaceType);
3696+
elem->computeType();
37153697

37163698
if (isImplicit)
37173699
elem->setImplicit();
@@ -3732,7 +3714,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
37323714
DeclContextID contextID;
37333715
bool isImplicit, isObjC, isGetterMutating, isSetterMutating;
37343716
GenericEnvironmentID genericEnvID;
3735-
TypeID interfaceTypeID;
3717+
TypeID elemInterfaceTypeID;
37363718
AccessorRecord accessors;
37373719
DeclID overriddenID;
37383720
uint8_t rawAccessLevel, rawSetterAccessLevel;
@@ -3746,7 +3728,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
37463728
readImpl, writeImpl, readWriteImpl,
37473729
numAccessors,
37483730
genericEnvID,
3749-
interfaceTypeID,
3731+
elemInterfaceTypeID,
37503732
overriddenID, rawAccessLevel,
37513733
rawSetterAccessLevel, numArgNames,
37523734
argNameAndDependencyIDs);
@@ -3814,8 +3796,9 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
38143796
}
38153797
}
38163798

3817-
auto interfaceType = getType(interfaceTypeID);
3818-
subscript->setInterfaceType(interfaceType);
3799+
auto elemInterfaceType = getType(elemInterfaceTypeID);
3800+
subscript->getElementTypeLoc().setType(elemInterfaceType);
3801+
subscript->computeType();
38193802

38203803
if (isImplicit)
38213804
subscript->setImplicit();
@@ -3911,12 +3894,10 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
39113894
DeclContextID contextID;
39123895
bool isImplicit, isObjC;
39133896
GenericEnvironmentID genericEnvID;
3914-
TypeID interfaceID;
39153897

39163898
decls_block::DestructorLayout::readRecord(scratch, contextID,
39173899
isImplicit, isObjC,
3918-
genericEnvID,
3919-
interfaceID);
3900+
genericEnvID);
39203901

39213902
DeclContext *DC = getDeclContext(contextID);
39223903
if (declOrOffset.isComplete())
@@ -3935,8 +3916,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
39353916
selfDecl->setImplicit();
39363917
dtor->setParameters(selfDecl, ParameterList::createEmpty(ctx));
39373918

3938-
auto interfaceType = getType(interfaceID);
3939-
dtor->setInterfaceType(interfaceType);
3919+
dtor->computeType();
39403920

39413921
if (isImplicit)
39423922
dtor->setImplicit();

lib/Serialization/Serialization.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,8 +3207,8 @@ void Serializer::writeDecl(const Decl *D) {
32073207
uint8_t rawDefaultArgumentResilienceExpansion =
32083208
getRawStableResilienceExpansion(
32093209
fn->getDefaultArgumentResilienceExpansion());
3210-
Type ty = fn->getInterfaceType();
32113210

3211+
Type ty = fn->getInterfaceType();
32123212
for (auto dependency : collectDependenciesFromType(ty->getCanonicalType()))
32133213
nameComponentsAndDependencies.push_back(addTypeRef(dependency));
32143214

@@ -3226,7 +3226,7 @@ void Serializer::writeDecl(const Decl *D) {
32263226
fn->hasThrows(),
32273227
addGenericEnvironmentRef(
32283228
fn->getGenericEnvironment()),
3229-
addTypeRef(ty),
3229+
addTypeRef(fn->getResultInterfaceType()),
32303230
addDeclRef(fn->getOperatorDecl()),
32313231
addDeclRef(fn->getOverriddenDecl()),
32323232
fn->getFullName().getArgumentNames().size() +
@@ -3263,8 +3263,8 @@ void Serializer::writeDecl(const Decl *D) {
32633263
uint8_t rawDefaultArgumentResilienceExpansion =
32643264
getRawStableResilienceExpansion(
32653265
fn->getDefaultArgumentResilienceExpansion());
3266-
Type ty = fn->getInterfaceType();
32673266

3267+
Type ty = fn->getInterfaceType();
32683268
SmallVector<IdentifierID, 4> dependencies;
32693269
for (auto dependency : collectDependenciesFromType(ty->getCanonicalType()))
32703270
dependencies.push_back(addTypeRef(dependency));
@@ -3283,7 +3283,7 @@ void Serializer::writeDecl(const Decl *D) {
32833283
fn->hasThrows(),
32843284
addGenericEnvironmentRef(
32853285
fn->getGenericEnvironment()),
3286-
addTypeRef(ty),
3286+
addTypeRef(fn->getResultInterfaceType()),
32873287
addDeclRef(fn->getOverriddenDecl()),
32883288
addDeclRef(fn->getStorage()),
32893289
rawAccessorKind,
@@ -3336,7 +3336,6 @@ void Serializer::writeDecl(const Decl *D) {
33363336
unsigned abbrCode = DeclTypeAbbrCodes[EnumElementLayout::Code];
33373337
EnumElementLayout::emitRecord(Out, ScratchRecord, abbrCode,
33383338
contextID,
3339-
addTypeRef(elem->getInterfaceType()),
33403339
elem->isImplicit(),
33413340
elem->hasAssociatedValues(),
33423341
(unsigned)RawValueKind,
@@ -3389,7 +3388,7 @@ void Serializer::writeDecl(const Decl *D) {
33893388
accessors.Decls.size(),
33903389
addGenericEnvironmentRef(
33913390
subscript->getGenericEnvironment()),
3392-
addTypeRef(ty),
3391+
addTypeRef(subscript->getElementInterfaceType()),
33933392
addDeclRef(subscript->getOverriddenDecl()),
33943393
rawAccessLevel,
33953394
rawSetterAccessLevel,
@@ -3440,7 +3439,6 @@ void Serializer::writeDecl(const Decl *D) {
34403439
ctor->getInitKind()),
34413440
addGenericEnvironmentRef(
34423441
ctor->getGenericEnvironment()),
3443-
addTypeRef(ty),
34443442
addDeclRef(ctor->getOverriddenDecl()),
34453443
rawAccessLevel,
34463444
ctor->needsNewVTableEntry(),
@@ -3469,8 +3467,7 @@ void Serializer::writeDecl(const Decl *D) {
34693467
dtor->isImplicit(),
34703468
dtor->isObjC(),
34713469
addGenericEnvironmentRef(
3472-
dtor->getGenericEnvironment()),
3473-
addTypeRef(dtor->getInterfaceType()));
3470+
dtor->getGenericEnvironment()));
34743471
break;
34753472
}
34763473

0 commit comments

Comments
 (0)