mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
* Remove `AsyncIterator: Sendable` requirement from merge # Motivation Currently a lot of the operator implementations in here that consume other `AsyncSequence`s require the `AsyncIterator` to be `Sendable`. This is mostly due to the fact that we are calling `makeAsyncIterator` on the upstream `AsyncSequence` and then pass that iterator around to various newly spawned `Task`s. This has two downsides: 1. It only allows users to use operators like `merge` if their `AsyncSequence.AsyncIterator` is `Sendable` 2. In merge we are creating new `Task`s for every new demand. Creating `Task`s is not cheap. My main goal of this PR was to remove the `Sendable` constraint from `merge`. # Modification This PR overhauls the complete inner workings of the `AsyncMerge2Sequence`. It does a couple of things: 1. The main change is that instead of creating new `Task`s for every demand, we are creating one `Task` when the `AsyncIterator` is created. This task has as child task for every upstream sequence. 2. When calling `next` we are signalling the child tasks to demand from the upstream 3. A new state machine that is synchronizing the various concurrent operations that can happen 4. Handling cancellation since we are creating a bunch of continuations. # Result In the end, this PR swaps the implementation of `AsyncMerge2Sequence` and drops the `Sendable` constraint and passes all tests. Furthermore, on my local performance testing I saw up 50% speed increase in throughput. # Open points 1. I need to make this sequence re-throwing but before going down that rabbit whole I wanna get buy-in on the implementation. 2. We should discuss and document if `merge` and other operators are hot or cold, i.e. if they only request if they got downstream demand 3. I need to switch `AsyncMerge3Sequence` over to the same iplementation * Split logic into multiple files, adapt merge3 and incorporate PR feedback * Add more tests and fix upstream throw behaviour * Remove internal class from the sequence * Setup task after first next * Remove unnecessary tests
168 lines
5.7 KiB
Swift
168 lines
5.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Async Algorithms open source project
|
|
//
|
|
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import XCTest
|
|
|
|
fileprivate enum _XCTAssertion {
|
|
case equal
|
|
case equalWithAccuracy
|
|
case identical
|
|
case notIdentical
|
|
case greaterThan
|
|
case greaterThanOrEqual
|
|
case lessThan
|
|
case lessThanOrEqual
|
|
case notEqual
|
|
case notEqualWithAccuracy
|
|
case `nil`
|
|
case notNil
|
|
case unwrap
|
|
case `true`
|
|
case `false`
|
|
case fail
|
|
case throwsError
|
|
case noThrow
|
|
|
|
var name: String? {
|
|
switch(self) {
|
|
case .equal: return "XCTAssertEqual"
|
|
case .equalWithAccuracy: return "XCTAssertEqual"
|
|
case .identical: return "XCTAssertIdentical"
|
|
case .notIdentical: return "XCTAssertNotIdentical"
|
|
case .greaterThan: return "XCTAssertGreaterThan"
|
|
case .greaterThanOrEqual: return "XCTAssertGreaterThanOrEqual"
|
|
case .lessThan: return "XCTAssertLessThan"
|
|
case .lessThanOrEqual: return "XCTAssertLessThanOrEqual"
|
|
case .notEqual: return "XCTAssertNotEqual"
|
|
case .notEqualWithAccuracy: return "XCTAssertNotEqual"
|
|
case .`nil`: return "XCTAssertNil"
|
|
case .notNil: return "XCTAssertNotNil"
|
|
case .unwrap: return "XCTUnwrap"
|
|
case .`true`: return "XCTAssertTrue"
|
|
case .`false`: return "XCTAssertFalse"
|
|
case .throwsError: return "XCTAssertThrowsError"
|
|
case .noThrow: return "XCTAssertNoThrow"
|
|
case .fail: return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
fileprivate enum _XCTAssertionResult {
|
|
case success
|
|
case expectedFailure(String?)
|
|
case unexpectedFailure(Swift.Error)
|
|
|
|
var isExpected: Bool {
|
|
switch self {
|
|
case .unexpectedFailure(_): return false
|
|
default: return true
|
|
}
|
|
}
|
|
|
|
func failureDescription(_ assertion: _XCTAssertion) -> String {
|
|
let explanation: String
|
|
switch self {
|
|
case .success: explanation = "passed"
|
|
case .expectedFailure(let details?): explanation = "failed: \(details)"
|
|
case .expectedFailure(_): explanation = "failed"
|
|
case .unexpectedFailure(let error): explanation = "threw error \"\(error)\""
|
|
}
|
|
|
|
if let name = assertion.name {
|
|
return "\(name) \(explanation)"
|
|
} else {
|
|
return explanation
|
|
}
|
|
}
|
|
}
|
|
|
|
private func _XCTEvaluateAssertion(_ assertion: _XCTAssertion, message: () -> String, file: StaticString, line: UInt, expression: () throws -> _XCTAssertionResult) {
|
|
let result: _XCTAssertionResult
|
|
do {
|
|
result = try expression()
|
|
} catch {
|
|
result = .unexpectedFailure(error)
|
|
}
|
|
|
|
switch result {
|
|
case .success:
|
|
return
|
|
default:
|
|
XCTFail("\(result.failureDescription(assertion)) - \(message())", file: file, line: line)
|
|
}
|
|
}
|
|
|
|
fileprivate func _XCTAssertEqual<T>(_ expression1: () throws -> T, _ expression2: () throws -> T, _ equal: (T, T) -> Bool, _ message: () -> String, file: StaticString = #filePath, line: UInt = #line) {
|
|
_XCTEvaluateAssertion(.equal, message: message, file: file, line: line) {
|
|
let (value1, value2) = (try expression1(), try expression2())
|
|
if equal(value1, value2) {
|
|
return .success
|
|
} else {
|
|
return .expectedFailure("(\"\(value1)\") is not equal to (\"\(value2)\")")
|
|
}
|
|
}
|
|
}
|
|
|
|
public func XCTAssertEqual<A: Equatable, B: Equatable>(_ expression1: @autoclosure () throws -> (A, B), _ expression2: @autoclosure () throws -> (A, B), _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
|
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
|
}
|
|
|
|
fileprivate func ==<A: Equatable, B: Equatable>(_ lhs: [(A, B)], _ rhs: [(A, B)]) -> Bool {
|
|
guard lhs.count == rhs.count else {
|
|
return false
|
|
}
|
|
for (lhsElement, rhsElement) in zip(lhs, rhs) {
|
|
if lhsElement != rhsElement {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
public func XCTAssertEqual<A: Equatable, B: Equatable>(_ expression1: @autoclosure () throws -> [(A, B)], _ expression2: @autoclosure () throws -> [(A, B)], _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
|
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
|
}
|
|
|
|
public func XCTAssertEqual<A: Equatable, B: Equatable, C: Equatable>(_ expression1: @autoclosure () throws -> (A, B, C), _ expression2: @autoclosure () throws -> (A, B, C), _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
|
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
|
}
|
|
|
|
fileprivate func ==<A: Equatable, B: Equatable, C: Equatable>(_ lhs: [(A, B, C)], _ rhs: [(A, B, C)]) -> Bool {
|
|
guard lhs.count == rhs.count else {
|
|
return false
|
|
}
|
|
for (lhsElement, rhsElement) in zip(lhs, rhs) {
|
|
if lhsElement != rhsElement {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
public func XCTAssertEqual<A: Equatable, B: Equatable, C: Equatable>(_ expression1: @autoclosure () throws -> [(A, B, C)], _ expression2: @autoclosure () throws -> [(A, B, C)], _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
|
|
_XCTAssertEqual(expression1, expression2, { $0 == $1 }, message, file: file, line: line)
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
|
internal func XCTAssertThrowsError<T>(
|
|
_ expression: @autoclosure () async throws -> T,
|
|
file: StaticString = #file,
|
|
line: UInt = #line,
|
|
verify: (Error) -> Void = { _ in }
|
|
) async {
|
|
do {
|
|
_ = try await expression()
|
|
XCTFail("Expression did not throw error", file: file, line: line)
|
|
} catch {
|
|
verify(error)
|
|
}
|
|
}
|