Large Increase in Compilation Time in Swift 5.6 on Enums with Associated Values

FB9963451 - Has been filed due to a bug found in Swift 5.6 when compiling enums with mixed associated types (some with and some without associated types) causing exponentially large compilation time increases.
Plese note: https://twitter.com/kylebshr/status/1504503503942680578?s=21

A shim can be applied as such to address the confusing behaviors removing the drastic build time increases:

The following implementation is a shim that allows us to continue using enums in their current implementation without the incursion of build time increases

public enum UserProfileAction: Equatable {
    case saveUserProfile(profile: Profile)
    case userProfileFailure(error: Error)
    case fetchUserProfile(fromNetwork: Bool)
    case updateUserProfile(updates: [UserProfileField?: String])
    case updatePrivacyPreferences(_ privacyPreferences: [PrivacyPreference])
    case updateUOM(_ unitMeasure: Dimension)
    
    /**
     FB9963451 - Has been filed due to a bug found in Swift 5.6 when compiling enums with mixed associated types (some with and some without associated types) causing exponentially large compilation time increases.
     Plese note: https://twitter.com/kylebshr/status/1504503503942680578?s=21
     The below implementation is a shim that allows us to continue using enums in their current implementation without the incursion of build time increases
     */
    public typealias ARMCOMPILATIONSHIMVALUE = Bool
    fileprivate static var armCompilationShimValue: ARMCOMPILATIONSHIMVALUE { true }
    case updateUserProfileLanguage(_ value: ARMCOMPILATIONSHIMVALUE)
    case clearUserProfile(_ value: ARMCOMPILATIONSHIMVALUE)
    
    public static var updateUserProfileLanguage: UserProfileAction { return .updateUserProfileLanguage(armCompilationShimValue) }
    public static var clearUserProfile: UserProfileAction { return .clearUserProfile(armCompilationShimValue) }
}

Where as the vanilla implementation causes a drastic compilation time increase via Swift 5.6 in an Arm compilation environment (Apple Silicon):

public enum UserProfileAction: Equatable {
    case saveUserProfile(profile: Profile)
    case userProfileFailure(error: Error)
    case fetchUserProfile(fromNetwork: Bool)
    case updateUserProfile(updates: [UserProfileField?: String])
    case updatePrivacyPreferences(_ privacyPreferences: [PrivacyPreference])
    case updateUOM(_ unitMeasure: Dimension)
    
    /**
     FB9963451 - Has been filed due to a bug found in Swift 5.6 when compiling enums with mixed associated types (some with and some without associated types) causing exponentially large compilation time increases.
     Plese note: https://twitter.com/kylebshr/status/1504503503942680578?s=21
     The below implementation causes an increase from 0.1 second to 17.2-20 seconds on an M1 Ultra in a non-Rosetta environment
     */
    case updateUserProfileLanguage
    case clearUserProfile
}

Note the issue:

Description:

When performing a native Apple Silicon complication with a Swift project containing simple enum values with “some” type associated values (note these type associations must have structures with some level of base complexity and cannot be empty), the observed compilation time can exponentially grow to 5x of an equivalent enum “without any” or “with all” enum cases carrying type associated values.
In larger complex projects, this can drive build times that would normally take 5 minutes upwards to 30+ minutes in a process that may look like a compilation hang.
This presents a fairly large amount of cost overhead from development and resource perspective through common base level Swift language features.

Steps:

  1. Open Xcode in “non-Rossetta” on an Apple Silicon machine
  2. Add an enum type with “some” enum cases containing type associated values and “some” enum cases without any associated values
  3. Ensure the associated values are not primitive swift structures and have some level of complexity (they should have relationships and property definitions at least)
  4. Compile the project using a time summary

Expectation:

The project to build relatively similar between an equivalent project having “all” enum cases with “all” cases having type associated values or “all” enum cases having with “all” cases having no associated values.

Result:

The project build time was impacted by >= 5 times the normal compilation time taking a large hit to performance.
Apple Silicon (non-Rosetta) compilation
Containing mixed enums with and without type associated values:
17.2 second
Containing enums that all have type associated values:
0.1 second

Not sure how many people have observed this, but we have been able to confirm impact in a small project, file a report and identified a few other companies observing the same issue.

1 Like

To follow up with the above post, I believe the issue has been identified as an LLVM issue, but still presents a fairly large language problem: https://twitter.com/typesanitizer/status/1505755827814100992?s=21

Also for interest, [SR-16027] Compilation speed slowed 200x on Apple Silicon in Swift 5.6 - Swift has been filed and is open.