Skip to content

Commit 4a0fdfc

Browse files
tgrapperonmluisbrown
authored andcommitted
Use memcmp to improve ForEachStore's performance (#1307)
* Use `memcmp` to improve `ForEachStore`'s performance * Update Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift Co-authored-by: Stephen Celis <[email protected]> (cherry picked from commit 9a01fb412b4cd0bbc4d1032550b5621939430ddb) # Conflicts: # Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift
1 parent b573e1c commit 4a0fdfc

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@
9595
{
9696
self.data = store.state
9797
self.content = {
98-
WithViewStore(store.scope(state: { $0.ids })) { viewStore in
98+
WithViewStore(
99+
store.scope(state: { $0.ids }),
100+
removeDuplicates: areOrderedSetsDuplicates
101+
) { viewStore in
99102
ForEach(viewStore.state, id: \.self) { id -> EachContent in
100103
// NB: We cache elements here to avoid a potential crash where SwiftUI may re-evaluate
101104
// views for elements no longer in the collection.
@@ -120,4 +123,13 @@
120123
self.content()
121124
}
122125
}
126+
127+
private func areOrderedSetsDuplicates<ID: Hashable>(lhs: OrderedSet<ID>, rhs: OrderedSet<ID>) -> Bool {
128+
var lhs = lhs
129+
var rhs = rhs
130+
if memcmp(&lhs, &rhs, MemoryLayout<OrderedSet<ID>>.size) == 0 {
131+
return true
132+
}
133+
return lhs == rhs
134+
}
123135
#endif

0 commit comments

Comments
 (0)