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
95 lines
3.0 KiB
Swift
95 lines
3.0 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 DequeModule
|
|
|
|
/// Creates an asynchronous sequence of elements from two underlying asynchronous sequences
|
|
public func merge<Base1: AsyncSequence, Base2: AsyncSequence>(_ base1: Base1, _ base2: Base2) -> AsyncMerge2Sequence<Base1, Base2>
|
|
where
|
|
Base1.Element == Base2.Element,
|
|
Base1: Sendable, Base2: Sendable,
|
|
Base1.Element: Sendable
|
|
{
|
|
return AsyncMerge2Sequence(base1, base2)
|
|
}
|
|
|
|
/// An ``Swift/AsyncSequence`` that takes two upstream ``Swift/AsyncSequence``s and combines their elements.
|
|
public struct AsyncMerge2Sequence<
|
|
Base1: AsyncSequence,
|
|
Base2: AsyncSequence
|
|
>: Sendable where
|
|
Base1.Element == Base2.Element,
|
|
Base1: Sendable, Base2: Sendable,
|
|
Base1.Element: Sendable
|
|
{
|
|
public typealias Element = Base1.Element
|
|
|
|
private let base1: Base1
|
|
private let base2: Base2
|
|
|
|
/// Initializes a new ``AsyncMerge2Sequence``.
|
|
///
|
|
/// - Parameters:
|
|
/// - base1: The first upstream ``Swift/AsyncSequence``.
|
|
/// - base2: The second upstream ``Swift/AsyncSequence``.
|
|
public init(
|
|
_ base1: Base1,
|
|
_ base2: Base2
|
|
) {
|
|
self.base1 = base1
|
|
self.base2 = base2
|
|
}
|
|
}
|
|
|
|
extension AsyncMerge2Sequence: AsyncSequence {
|
|
public func makeAsyncIterator() -> AsyncIterator {
|
|
let storage = MergeStorage<Base1, Base2, Base1>(
|
|
base1: base1,
|
|
base2: base2,
|
|
base3: nil
|
|
)
|
|
return AsyncIterator(storage: storage)
|
|
}
|
|
}
|
|
|
|
extension AsyncMerge2Sequence {
|
|
public struct AsyncIterator: AsyncIteratorProtocol {
|
|
/// This class is needed to hook the deinit to observe once all references to the ``AsyncIterator`` are dropped.
|
|
///
|
|
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
|
|
final class InternalClass: Sendable {
|
|
private let storage: MergeStorage<Base1, Base2, Base1>
|
|
|
|
fileprivate init(storage: MergeStorage<Base1, Base2, Base1>) {
|
|
self.storage = storage
|
|
}
|
|
|
|
deinit {
|
|
self.storage.iteratorDeinitialized()
|
|
}
|
|
|
|
func next() async rethrows -> Element? {
|
|
try await storage.next()
|
|
}
|
|
}
|
|
|
|
let internalClass: InternalClass
|
|
|
|
fileprivate init(storage: MergeStorage<Base1, Base2, Base1>) {
|
|
internalClass = InternalClass(storage: storage)
|
|
}
|
|
|
|
public mutating func next() async rethrows -> Element? {
|
|
try await internalClass.next()
|
|
}
|
|
}
|
|
}
|