Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix package build in 5.7 #2675

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions Sources/swift-composable-architecture-benchmark/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,41 @@ import Dependencies
import Foundation

let dependenciesSuite = BenchmarkSuite(name: "Dependencies") { suite in
let reducer: some Reducer<Int, Void> = BenchmarkReducer()
.dependency(\.calendar, .autoupdatingCurrent)
.dependency(\.date, .init { Date() })
.dependency(\.locale, .autoupdatingCurrent)
.dependency(\.mainQueue, .immediate)
.dependency(\.mainRunLoop, .immediate)
.dependency(\.timeZone, .autoupdatingCurrent)
.dependency(\.uuid, .init { UUID() })
#if swift(>=5.9)
let reducer: some Reducer<Int, Void> = BenchmarkReducer()
.dependency(\.calendar, .autoupdatingCurrent)
.dependency(\.date, .init { Date() })
.dependency(\.locale, .autoupdatingCurrent)
.dependency(\.mainQueue, .immediate)
.dependency(\.mainRunLoop, .immediate)
.dependency(\.timeZone, .autoupdatingCurrent)
.dependency(\.uuid, .init { UUID() })

suite.benchmark("Dependency key writing") {
var state = 0
_ = reducer.reduce(into: &state, action: ())
precondition(state == 1)
}
suite.benchmark("Dependency key writing") {
var state = 0
_ = reducer.reduce(into: &state, action: ())
precondition(state == 1)
}
#endif
}

@Reducer
private struct BenchmarkReducer {
@Dependency(\.someValue) var someValue
var body: some Reducer<Int, Void> {
Reduce { state, action in
state = self.someValue
return .none
#if swift(>=5.9)
@Reducer
private struct BenchmarkReducer {
@Dependency(\.someValue) var someValue
var body: some Reducer<Int, Void> {
Reduce { state, action in
state = self.someValue
return .none
}
}
}
}
private enum SomeValueKey: DependencyKey {
static let liveValue = 1
}
extension DependencyValues {
var someValue: Int {
self[SomeValueKey.self]
private enum SomeValueKey: DependencyKey {
static let liveValue = 1
}
}
extension DependencyValues {
var someValue: Int {
self[SomeValueKey.self]
}
}
#endif
50 changes: 27 additions & 23 deletions Sources/swift-composable-architecture-benchmark/StoreScope.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import Benchmark
import ComposableArchitecture

@Reducer
private struct Counter {
typealias State = Int
typealias Action = Bool
var body: some Reducer<State, Action> {
Reduce { state, action in
if action {
state += 1
return .none
} else {
state -= 1
return .none
#if swift(>=5.9)
@Reducer
private struct Counter {
typealias State = Int
typealias Action = Bool
var body: some Reducer<State, Action> {
Reduce { state, action in
if action {
state += 1
return .none
} else {
state -= 1
return .none
}
}
}
}
}
#endif

let storeScopeSuite = BenchmarkSuite(name: "Store scoping") { suite in
var store = Store(initialState: 0) { Counter() }
var viewStores: [ViewStore<Int, Bool>] = [ViewStore(store, observe: { $0 })]
for _ in 1...4 {
store = store.scope(state: { $0 }, action: { $0 })
viewStores.append(ViewStore(store, observe: { $0 }))
}
let lastViewStore = viewStores.last!
#if swift(>=5.9)
var store = Store(initialState: 0) { Counter() }
var viewStores: [ViewStore<Int, Bool>] = [ViewStore(store, observe: { $0 })]
for _ in 1...4 {
store = store.scope(state: { $0 }, action: { $0 })
viewStores.append(ViewStore(store, observe: { $0 }))
}
let lastViewStore = viewStores.last!

suite.benchmark("Nested store") {
lastViewStore.send(true)
}
suite.benchmark("Nested store") {
lastViewStore.send(true)
}
#endif
}
144 changes: 74 additions & 70 deletions Sources/swift-composable-architecture-benchmark/StoreSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,88 @@ import Combine
@_spi(Internals) import ComposableArchitecture
import Foundation

let storeSuite = BenchmarkSuite(name: "Store") {
var store: StoreOf<Feature>!
let levels = 5
let storeSuite = BenchmarkSuite(name: "Store") { suite in
#if swift(>=5.9)
var store: StoreOf<Feature>!
let levels = 5

for level in 1...levels {
$0.benchmark("Nested send tap: \(level)") {
store.send(tap(level: level))
} setUp: {
store = Store(initialState: state(level: level)) {
Feature()
for level in 1...levels {
suite.benchmark("Nested send tap: \(level)") {
store.send(tap(level: level))
} setUp: {
store = Store(initialState: state(level: level)) {
Feature()
}
} tearDown: {
precondition(count(of: store.withState { $0 }, level: level) == 1)
_cancellationCancellables.removeAll()
}
} tearDown: {
precondition(count(of: store.withState { $0 }, level: level) == 1)
_cancellationCancellables.removeAll()
}
}
for level in 1...levels {
$0.benchmark("Nested send none: \(level)") {
store.send(none(level: level))
} setUp: {
store = Store(initialState: state(level: level)) {
Feature()
for level in 1...levels {
suite.benchmark("Nested send none: \(level)") {
store.send(none(level: level))
} setUp: {
store = Store(initialState: state(level: level)) {
Feature()
}
} tearDown: {
precondition(count(of: store.withState { $0 }, level: level) == 0)
_cancellationCancellables.removeAll()
}
} tearDown: {
precondition(count(of: store.withState { $0 }, level: level) == 0)
_cancellationCancellables.removeAll()
}
}
#endif
}

@Reducer
private struct Feature {
struct State {
@PresentationState var child: State?
var count = 0
}
enum Action {
indirect case child(PresentationAction<Action>)
case tap
case none
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .child:
return .none
case .tap:
state.count = 1
return .publisher { Empty() }.cancellable(id: UUID())
case .none:
return .none
}
#if swift(>=5.9)
@Reducer
private struct Feature {
struct State {
@PresentationState var child: State?
var count = 0
}
enum Action {
indirect case child(PresentationAction<Action>)
case tap
case none
}
.ifLet(\.$child, action: \.child) {
Feature()
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .child:
return .none
case .tap:
state.count = 1
return .publisher { Empty() }.cancellable(id: UUID())
case .none:
return .none
}
}
.ifLet(\.$child, action: \.child) {
Feature()
}
}
}
}

private func state(level: Int) -> Feature.State {
Feature.State(
child: level == 0
? nil
: state(level: level - 1)
)
}
private func tap(level: Int) -> Feature.Action {
level == 0
? .tap
: Feature.Action.child(.presented(tap(level: level - 1)))
}
private func none(level: Int) -> Feature.Action {
level == 0
? .none
: Feature.Action.child(.presented(none(level: level - 1)))
}
private func count(of state: Feature.State?, level: Int) -> Int? {
level == 0
? state?.count
: count(of: state?.child, level: level - 1)
}
private func state(level: Int) -> Feature.State {
Feature.State(
child: level == 0
? nil
: state(level: level - 1)
)
}
private func tap(level: Int) -> Feature.Action {
level == 0
? .tap
: Feature.Action.child(.presented(tap(level: level - 1)))
}
private func none(level: Int) -> Feature.Action {
level == 0
? .none
: Feature.Action.child(.presented(none(level: level - 1)))
}
private func count(of state: Feature.State?, level: Int) -> Int? {
level == 0
? state?.count
: count(of: state?.child, level: level - 1)
}
#endif