# Swift equality operator takes long time to type check

**URL:** <https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226>\
**Category:** Using Swift\
**Created:** [October 16, 2020, 12:00am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226 "2020-10-16T00:00:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanich8355](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@sanich8355](https://forums.swift.org/u/sanich8355)\
**Post date:** [October 16, 2020, 12:00am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/1 "2020-10-16T00:00:30Z")

</div>

I wanted to optimize compile time, so I had `-Xfrontend -warn-long-expression-type-checking=200` flag on to figure out what causes long time to type check.

Turns out `==` equality operators were causing long time to type check. So my question is..

1. Why does equality operator causes long time to type check?
2. Is there an alternative way to compare equality? rather than explicity using `==` operator? And why would this way cause less compile time?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 16, 2020, 12:08am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/2 "2020-10-16T00:08:34Z")

</div>

Could you give the expression that causes the problem (including the type of each variable therein)? It could be other parts of the expression, in tandem with the `==`, that complexify the possible search space.

It can usually be alleviated with one (or two) strategically placed `as`. Breaking the expression into multiple subexpressions may also helps.

> [@sanich8355](#):
>
> Is there an alternative way to compare equality? rather than explicity using `==` operator?

I don't think, not in a way that'd improve the type checking performance (aside from the methods mentioned above).

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 16, 2020, 12:18am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/3 "2020-10-16T00:18:37Z")

</div>

With that said,

> [@sanich8355](#):
>
> so I had `-Xfrontend -warn-long-expression-type-checking=200` flag

I wouldn't recommend relying on this. Many type checking work can be, and are cached. So the _first expression_ of its kind can take a particularly long time without really impacting the type checking performance as a whole.

---

<div class="post-metadata">

**Author:** ![sanich8355](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@sanich8355](https://forums.swift.org/u/sanich8355)\
**Post date:** [October 16, 2020, 12:22am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/4 "2020-10-16T00:22:55Z")

</div>

Occasionally when equality operator was used inside a closure that is passed as an argument  
`distinctUntilChanged { $0 == $1 } // Long time to type check`  
it warned me of type checking, but equality operator used alone warned me too  
`let a = SomeClass2() == SomeClass2() // Long time to type check`

---

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [October 16, 2020, 12:23am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/5 "2020-10-16T00:23:25Z")

</div>

I think this maybe not really about the == operator itself, although it adds to the compile time the number of overloads of this operator defined in the standard library and probably in modules that people write and import on their apps. But this can also goes down to the operands and maybe sub-expressions, specially if they involve literals or types that have to be inferred where solver couldn't place constraints to reduce the possible overloads to this operator the solver will attempt in order to find a solution, so solver has to attempt to bind all possible overload to apply to the expression which makes the type checker time goes up a bit.  
It can happen with any defined operator in fact, but is more likely to see this slow down on type checking with the == operator because there are many overloads defined for it either in stdlib or in the user defined module, so if the solver can place specific type constraints to filter possible operator overloads on a expression for any reason, the solver might end-up with a long list of overloads to attempt which causes type checking to take more time.

---

<div class="post-metadata">

**Author:** ![sanich8355](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@sanich8355](https://forums.swift.org/u/sanich8355)\
**Post date:** [October 16, 2020, 12:30am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/6 "2020-10-16T00:30:04Z")

</div>

ps. I made up function inside Equatable extension which uses pattern matching to compare equality, and this does not warn me about long time to type check.  
What could this differ from `==` operator used explicitly?

```
func equals(_ object: Self?) -> Bool {
    guard case object = self else { return false }
    return true
}

```

---

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [October 16, 2020, 12:32am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/7 "2020-10-16T00:32:20Z")

</div>

In this case, it may depend on the previous code in the chain and whether the solver could have enough information to be able to place constraints that will filter out the possible overloads for this operator. If you explicitly specify the types e.g. distinctUntilChanged { (a: Int, b: Int) in a == b } helps the compiler to type-check faster because it can add constraints on the solver that reduce the list of possible overloads applicable for this on overload resolution. @xedin @hborla may be able to better explain this :)

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 16, 2020, 12:37am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/8 "2020-10-16T00:37:10Z")

</div>

> [@LucianoPAlmeida](#):
>
> If you explicitly specify the types e.g. distinctUntilChanged { (a: Int, b: Int) in a == b }

or even

```swift
distinctUntilChanged { $0 == $1 as Int }

```

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [October 16, 2020, 7:20pm UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/9 "2020-10-16T19:20:24Z")

</div>

> I made up function inside Equatable extension which uses pattern matching to compare equality, and this does not warn me about long time to type check.  
> What could this differ from `==` operator used explicitly?

There are a few factors here:

1. There are a _lot_ more overloads for the `==` operator than for the `equals` method that you wrote. Looking at the debug output of the type checker tells me there are 73 overloads of `==` in the standard library alone. If you wrote 70 overloads of `equals`, it'd probably exhibit the same performance issues as `==`
2. The `equals` function you wrote is a method, which means you call it directly on an instance that conforms to `Equatable`, e.g. `object.equals(otherObject)`. Methods are a lot easier for the compiler to figure out, because the compiler only needs to consider overloads on the base type that you called the method on. With operators, the compiler has to consider every overload of `==` regardless of what type or protocol it's declared on, because operators are always called like global functions.

EDIT:  
To answer your question about why pattern matching type checks faster, the answer is the same as 1. above: there are a lot more overloads of `==` (~70 overloads) than there are of the pattern matching operator `~=` (5 overloads)

---

<div class="post-metadata">

**Author:** ![sanich8355](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@sanich8355](https://forums.swift.org/u/sanich8355)\
**Post date:** [October 17, 2020, 7:46am UTC](https://forums.swift.org/t/swift-equality-operator-takes-long-time-to-type-check/41226/10 "2020-10-17T07:46:00Z")

</div>

Wow. It was super helpful 😄  
Thank you very much @hborla
