pointfreeco / swift-identified-collections

A library of data structures for working with collections of identifiable elements in an ergonomic, performant way.
MIT License
539 stars 46 forks source link

CPU performance issues #29

Closed mihaho closed 3 years ago

mihaho commented 3 years ago

Discussed in https://github.com/pointfreeco/swift-identified-collections/discussions/28

Originally posted by **mihaho** October 13, 2021 I am facing some issues when trying to upgrade from Composable Architecture v20 to a higher version. The problem is that the CPU starts spiking after the introduction of the Identified Collections package. I profiled the app and noticed that the largest stack trace was due to the equality checks. This drew my attention to the equality conformance of `IdentifiedArray`. The difference with the old version is that you now use _[elementsEqual](https://github.com/apple/swift/blob/main/stdlib/public/core/SequenceAlgorithms.swift#L318-L336)_ instead of _[==](https://github.com/apple/swift/blob/main/stdlib/public/core/Array.swift#L1728-L1752)_. I checked the difference in Swift's source code. _elementsEqual_ always creates 2 iterators for each sequence and goes through all elements while _==_ has some shortcuts. Replacing `lhs.elementsEqual(rhs)` with `lhs.elements == rhs.elements` helps a lot. The app becomes responsive again and the CPU drops for about ~50% Is _elementsEqual_ needed? Any comments are welcome
stephencelis commented 3 years ago

Fixed by #29.