Xcode 在 equatable 协议上的编译时间很慢

问题描述

Xcode 用 equatable 协议编译类的时间太长,这有点奇怪,因为在这种特殊情况下它只比较原始类型,这应该很容易。

用了 608 毫秒进行类型检查:

    public static func == (lhs: TheModel,rhs: TheModel) -> Bool {
    return lhs.firstName == rhs.firstName &&
        lhs.lastName == rhs.lastName &&
        lhs.maritalStatus == rhs.maritalStatus &&
        lhs.sex == rhs.sex &&
        lhs.birthDate == rhs.birthDate &&
        lhs.nationality == rhs.nationality &&
        lhs.hasAnotherNationality == rhs.hasAnotherNationality &&
        lhs.anotherNationality == rhs.anotherNationality &&
        lhs.nif == rhs.nif &&
        lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress &&
        lhs.identificationCard == rhs.identificationCard &&
        lhs.identificationCardNumber == rhs.identificationCardNumber &&
        lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate &&
        lhs.education == rhs.education &&
        lhs.jobTitle == rhs.jobTitle &&
        lhs.patronalEntity == rhs.patronalEntity &&
        lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes &&
        lhs.permanentAddressContainer == rhs.permanentAddressContainer &&
        lhs.fiscalAddressContainer == rhs.fiscalAddressContainer &&
        lhs.placeOfBirth == rhs.placeOfBirth &&
        lhs.honorificTitle == rhs.honorificTitle &&
        lhs.userType == rhs.userType &&
        lhs.userTypeDescription == rhs.userTypeDescription &&
        lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode &&
        lhs.lastUpdate == rhs.lastUpdate
}

PS:我知道这个比较有点矫枉过正,但这只是一个例子。 顺便说一句,这是在 MacbookPro 中使用的核心 i9、64Gb 内存和 SSD。

更新 - 我进行了一些更改并运行了更多测试:

测试 1

向 let 添加显式类型并返回 let:花费 ~450 毫秒

public static func == (lhs: TheModel,rhs: TheModel) -> Bool {
    let extractedExpr: Bool = lhs.firstName == rhs.firstName &&
        lhs.lastName == rhs.lastName &&
        lhs.maritalStatus == rhs.maritalStatus &&
        lhs.sex == rhs.sex &&
        lhs.birthDate == rhs.birthDate &&
        lhs.nationality == rhs.nationality &&
        lhs.hasAnotherNationality == rhs.hasAnotherNationality &&
        lhs.anotherNationality == rhs.anotherNationality &&
        lhs.nif == rhs.nif &&
        lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress &&
        lhs.identificationCard == rhs.identificationCard &&
        lhs.identificationCardNumber == rhs.identificationCardNumber &&
        lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate &&
        lhs.education == rhs.education &&
        lhs.jobTitle == rhs.jobTitle &&
        lhs.patronalEntity == rhs.patronalEntity &&
        lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes &&
        lhs.permanentAddressContainer == rhs.permanentAddressContainer &&
        lhs.fiscalAddressContainer == rhs.fiscalAddressContainer &&
        lhs.placeOfBirth == rhs.placeOfBirth &&
        lhs.honorificTitle == rhs.honorificTitle &&
        lhs.userType == rhs.userType &&
        lhs.userTypeDescription == rhs.userTypeDescription &&
        lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode &&
        lhs.lastUpdate == rhs.lastUpdate
    
    return extractedExpr
}

测试 2

为每个案例添加括号:结果与测试 1 相同!

 let extractedExpr: Bool = (lhs.firstName == rhs.firstName) &&
        (lhs.lastName == rhs.lastName) &&
        (lhs.maritalStatus == rhs.maritalStatus) &&
        (lhs.sex == rhs.sex) &&
        (lhs.birthDate == rhs.birthDate) &&
        (lhs.nationality == rhs.nationality) &&
        (lhs.hasAnotherNationality == rhs.hasAnotherNationality) &&
        (lhs.anotherNationality == rhs.anotherNationality) &&
        (lhs.nif == rhs.nif) &&
        (lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress) &&
        (lhs.identificationCard == rhs.identificationCard) &&
        (lhs.identificationCardNumber == rhs.identificationCardNumber) &&
        (lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate) &&
        (lhs.education == rhs.education) &&
        (lhs.jobTitle == rhs.jobTitle) &&
        (lhs.patronalEntity == rhs.patronalEntity) &&
        (lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes) &&
        (lhs.permanentAddressContainer == rhs.permanentAddressContainer) &&
        (lhs.fiscalAddressContainer == rhs.fiscalAddressContainer) &&
        (lhs.placeOfBirth == rhs.placeOfBirth) &&
        (lhs.honorificTitle == rhs.honorificTitle) &&
        (lhs.userType == rhs.userType) &&
        (lhs.userTypeDescription == rhs.userTypeDescription) &&
        (lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode) &&
        (lhs.lastUpdate == rhs.lastUpdate)

    return extractedExpr

测试 3

添加一个 bollean 数组作为具有显式类型的 var 并附加每个条件:花费 171 毫秒 :)

public static func == (lhs: TheModel,rhs: TheModel) -> Bool {
    var result: [Bool] = [Bool]()

    result.append(lhs.firstName == rhs.firstName)
    result.append(lhs.lastName == rhs.lastName)
    result.append(lhs.maritalStatus == rhs.maritalStatus)
    result.append(lhs.sex == rhs.sex)
    result.append(lhs.birthDate == rhs.birthDate)
    result.append(lhs.nationality == rhs.nationality)
    result.append(lhs.hasAnotherNationality == rhs.hasAnotherNationality)
    result.append(lhs.anotherNationality == rhs.anotherNationality)
    result.append(lhs.nif == rhs.nif)
    result.append(lhs.hasAnotherFiscalAddress == rhs.hasAnotherFiscalAddress)
    result.append(lhs.identificationCard == rhs.identificationCard)
    result.append(lhs.identificationCardNumber == rhs.identificationCardNumber)
    result.append(lhs.identificationCardExpirationDate == rhs.identificationCardExpirationDate)
    result.append(lhs.education == rhs.education)
    result.append(lhs.jobTitle == rhs.jobTitle)
    result.append(lhs.patronalEntity == rhs.patronalEntity)
    result.append(lhs.hasLivesOfIncomes == rhs.hasLivesOfIncomes)
    result.append(lhs.permanentAddressContainer == rhs.permanentAddressContainer)
    result.append(lhs.fiscalAddressContainer == rhs.fiscalAddressContainer)
    result.append(lhs.placeOfBirth == rhs.placeOfBirth)
    result.append(lhs.honorificTitle == rhs.honorificTitle)
    result.append(lhs.userType == rhs.userType)
    result.append(lhs.userTypeDescription == rhs.userTypeDescription)
    result.append(lhs.lastVisitedCountryCode == rhs.lastVisitedCountryCode)
    result.append(lhs.lastUpdate == rhs.lastUpdate)

    return !result.contains(false)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)