Replies: 3 comments 1 reply
-
As expected, the biggest difference is when we compare equal elements. I have run a benchmark test which is attached below. If you agree, I will be happy to create a pull request. Legend
Source code: import CollectionsBenchmark
import CollectionsA // red line
import CollectionsB // blue line
struct DummyState: Equatable, Identifiable {
let value: Int
var id: Int {
value
}
init(_ value: Int) {
self.value = value
}
}
var benchmark = Benchmark(title: "Benchmark-Equality")
benchmark.add(
title: "Equality ==",
input: ([Int]).self
) { input in
return { timer in
let values = input.map(DummyState.init)
let copiedValues = values
let d1 = CollectionsA.IdentifiedArray(uniqueElements: values)
let d2 = CollectionsA.IdentifiedArray(uniqueElements: copiedValues)
timer.measure {
blackHole(d1 == d2)
}
}
}
benchmark.add(
title: "Equality elementsEqual",
input: ([Int]).self
) { input in
return { timer in
let values = input.map(DummyState.init)
let copiedValues = values
let d1 = CollectionsB.IdentifiedArray(uniqueElements: values)
let d2 = CollectionsB.IdentifiedArray(uniqueElements: copiedValues)
timer.measure {
blackHole(d1 == d2)
}
}
}
benchmark.main()
|
Beta Was this translation helpful? Give feedback.
-
@stephencelis, thanks for answering. Sure, I'll submit a PR |
Beta Was this translation helpful? Give feedback.
-
Also, I noticed that a thing mentioned in inline docs that direct access for |
Beta Was this translation helpful? Give feedback.
-
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 instead of ==. 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)
withlhs.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
Beta Was this translation helpful? Give feedback.
All reactions