Very slow type checking

Does anyone have any tips on how I can speed up type checking of something like the following:

    func updatedProfile(
        bio: String? = nil,
        area: String? = nil,
        email: String? = nil,
        phone: String? = nil,
        example1: [String]? = nil,
        example2: [String]? = nil
    ) -> Profile.Update {
        return Profile.Update(
            personal: .init(
                name: model.name,
                bio: bio ?? model.bio,
                dateOfBirth: model.dateOfBirth,
                area: area ?? model.area,
                email: email ?? model.email,
                phone: phone ?? model.phone
            ),
            example1: example1 ?? [],
            example2: example2 ?? []
        )
    }

Using -Xfrontend -warn-long-function-bodies=300 -Xfrontend -warn-long-expression-type-checking=200 I'm seeing that this function alone is taking almost 2 full seconds just to type check. I like the utility of an API like this as it's very flexible - maybe there's just a small change I can make somewhere?

1 Like

If you get rid of that function, does it speed up your overall build by two seconds? If not it seems likely that the function is getting charged for the type checking that needs to happen anyway, so you probably don't need to worry about it.

As suggested by @Jon_Shier, the warnings are imprecise. They don't always mean what they think you mean. However, if it is causing trouble, I would be immediately inclined to try putting the array declarations on lines of their own with explicit type annotations, like

let example1: [Example] = example1 ?? []
let example2: [Example] = example2 ?? []

And making making the .init an explicit Profile.Personal(name:... or whatever.

That's just the glaring cases where I've been burned with slow type-checking in the past.

2 Likes