Update the proposal with some initial feedback and add a first draft implementation of share

This commit is contained in:
Philippe Hausler committed 2025-09-08 16:24:24 -07:00
1 parent 5c70c5994b
commit 1f2d0a1a87
2 files changed
+451 -50

No files matched your search

+14 -50
View File
@@ -22,65 +22,33 @@ It is critical to identify that this is one algorithm in the family of algorithm
## Detailed design
It is not just likely but perhaps a certainty that other algorithms will end up needing the same concept of a buffering policy beyond just AsyncStream and the new sharing mechanism. A new type in AsyncAlgorithms will be introduced to handle this. [^BufferingPolicy]
A new extension will be added to return a `Sendable` `AsyncSequence`. This extension will take a buffering policy to identify how the buffer will be handled when iterations do not consume at the same rate.
```swift
/// A strategy that handles exhaustion of a buffer’s capacity.
public enum BufferingPolicy: Sendable {
/// Continue to add to the buffer, without imposing a limit on the number
/// of buffered elements.
case unbounded
/// When the buffer is full, discard the newly received element.
///
/// This strategy enforces keeping at most the specified number of oldest
/// values.
case bufferingOldest(Int)
/// When the buffer is full, discard the oldest element in the buffer.
///
/// This strategy enforces keeping at most the specified number of newest
/// values.
case bufferingNewest(Int)
}
```
A new extension will be added to return a concrete type representing the share algorithm. This extension will take a buffering policy to identify how the buffer will be handled when iterations do not consume at the same rate.
A new AsyncSequence type will be introduced that is explicitly marked as `Sendable`. This annotation identifies to the developer that this sequence can be shared and stored. Because the type is intended to be stored it cannot be returned by the extension as a `some AsyncSequence<Element, Failure> & Sendable` since that cannot be assigned to a stored property. Additionally the type of `AsyncShareSequence`, since indented to be stored, will act as a quasi erasing-barrier to the type information of previous sequences in the chain of algorithms in that it will only hold the generic information of the `Element` and `Failure` as part of it's public interface and not the "Base" asynchronous sequence it was created from.
The `Sendable` annotation identifies to the developer that this sequence can be shared and stored in an existental `any`.
```swift
extension AsyncSequence where Element: Sendable {
public func share(
bufferingPolicy: BufferingPolicy = .unbounded
) -> AsyncShareSequence<Element, Failure>
bufferingPolicy: AsyncBufferSequencePolicy = .unbounded
) -> some AsyncSequence<Element, Failure> & Sendable
}
public struct AsyncShareSequence<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
public struct Iterator: AsyncIteratorProtocol {
public mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element?
}
public func makeAsyncIterator() -> Iterator
}
@available(*, unavailable)
extension AsyncShareSequence.Iterator: Sendable { }
```
The buffer internally to the share algorithm will only extend back to the furthest element available but there will only be a singular buffer shared across all iterators. This ensures that with the application of the buffering policy the storage size is as minimal as possible while still allowing all iterations to avoid dropping values and keeping the memory usage in check.
The buffer internally to the share algorithm will only extend back to the furthest element available but there will only be a singular buffer shared across all iterators. This ensures that with the application of the buffering policy the storage size is as minimal as possible while still allowing all iterations to avoid dropping values and keeping the memory usage in check. The signature reuses the existing `AsyncBufferSequencePolicy` type to specify the behavior around buffering either responding to how it should limit emitting to the buffer or what should happen when the buffer is exceeded.
## Runtime Behavior
The construction of the `AsyncShareSequence` will initially construct a shared iteration reference. This means that all instances of the structure of the `AsyncShareSequence` will reference to the same iteration.
The runtime behaviors fall into a few categories; ordering, iteration isolation, cancellation, and lifetimes. To understand the beahviors there are a terms useful to define. Each creation of the AsyncIterator of the sequence and invocation of next will be referred to a side of the share iteration. The back pressure to the system to fetch a new element or termination is refered to as demand. The limit which is the pending gate for awaiting until the buffer has been serviced used for the `AsyncBufferSequencePolicy.bounded(_ : Int)` policy. The last special definition is that of the extent which is specifically in this case the lifetime of the asynchronous sequence itself.
Upon creation of the `Iterator` via `makeAsyncIterator` a new "side" will be constructed to identify the specific iterator interacting with the shared iteration. Then when next is invoked is where the first actual action takes place.
When the underlying type backing the share algorithm is constructed a new extent is created; this is used for tracking the reference lifetime under the hood and is used to both house the iteration but also to identify the point at which no more sides can be constructed. When no more sides can be constructed and no sides are left to iterate then the backing iteration is canceled. This prevents any un-referenced task backing the iteration to not be leaked by the algorith itself.
The next method will first checkout from a critical region the underlying AsyncIterator from the base. If that is successful (i.e. no other iteration sides have already checked it out) then it will invoke the next method of that iterator (forwarding in the actor isolation). If an element is produced then it enqueues the element to the shared buffer, checks in the iterator, adjusts the index in the buffer, and finds all pending continuations all in a shared critical region by a mutex. Then those continuations will be resumed with the given element.
That construction then creates an initial shared state and buffer. No task is started initially; it is only upon the first demand that the task backing the iteration is started; this means on the first call to next a task is spun up servicing all potential sides. The order of which the sides are serviced is not specified and cannot be relied upon, however the order of delivery within a side is always guarenteed to be ordered. The singular task servicing the iteration will be the only place holding any sort of iterator from the base `AsyncSequence`; so that iterator is isolated and not sent from one isolation to another. That iteration first awaits any limit availability and then awaits for a demand given by a side. After-which it then awaits an element or terminal event from the iterator and enqueues the elements to the buffer.
If no element is returned by the base iterator (signifying the terminal state);then the process is similar except it will instead mark the sequence as finished and resume with nil to any active continuations. Similarly with failures that will set the state as terminal but also store the error for further iteration points that need eventual termination.
The buffer itself is only held in one location, each side however has a cursor index into that buffer and when values are consumed it adjusts the indexes accordingly; leaving the buffer usage only as big as the largest deficit. This means that new sides that are started post initial start up will not have a "replay" effect; that is a similar but distinct algorithm and is not addressed by this proposal. Any buffer size sensitive systems that wish to adjust behavior should be aware that specifying a policy is a suggested step. However in common usage similar to other such systems servicing desktop and mobile applications the default and common behavior is to be unbounded. This allows for a progressive disclosure from common usage that just works out of the box with no configuration, to more advanced cases that need finer grained control. Furthermore there are scenarios where one might want ways of identifing dropped value events within the iteration of a side, this is something that will be addressed later in an upcoming proposal.
Then all sides are "drained" such that continuations are placed into the shared state and resumed when an element is available for that position.
As previously stated, the isolation of the iteration of the upstream/base AsyncSequence is to a detached task, this ensures that individual sides can have independent cancellation. Those cancellations will have the effect of remvoing that side from the shared iteration and cleaning up accordingly (including adjusting the trimming of the internal buffer).
Representing concurrent access is difficult to express all potential examples but there are a few cases included with this proposal to illustrate some of the behaviors. If a more comprehensive behavioral analysis is needed, it is strongly suggested to try out the pending pull request to identify how specific behaviors work. Please keep in mind that the odering between tasks is not specified, only the order within one side of iteration.
Practically this all means that a given iteration may be "behind" another and can eventually catch up (provided it is within the buffer limit).
@@ -127,7 +95,7 @@ Task 1 4
The order of the interleaving of the prints are not guaranteed; however the order of the elements per iteration is. Likewise in this buffering case it is guaranteed that all values are represented in the output.
If the creation were altered to the following:
If the creation were instead altered to the following:
```swift
let exampleSource = [0, 1, 2, 3, 4].async.share(bufferingPolicy: .bufferingNewest(2))
@@ -163,16 +131,12 @@ Task 1 2
However in this particular case the newest values are the dropped elements.
## Usage
It is expected that this operator will be unlike other
## Effect on API resilience
This is an additive API and no existing systems are changed, however it will introduce a few new types that will need to be maintained as ABI interfaces. Since the intent of this is to provide a mechanism to store AsyncSequences to a shared context the type must be exposed as ABI (for type sizing).
## Alternatives considered
[^BufferingPolicy] It has been considered that this particular policy would be nested inside the `AsyncShareSequence` type. However since this seems to be something that will be useful for other types it makes sense to expose it as a top level type. However if it is determined that a general form of a buffering policy would require additional behaviors this might be a debatable placement to move back to an interior type similar to AsyncStream.
It has been considered that the buffering policy would be nested inside the `AsyncShareSequence` type. However since this seems to be something that will be useful for other types it makes sense to use an existing type from a top level type. However if it is determined that a general form of a buffering policy would require additional behaviors this might be a debatable placement to move back to an interior type similar to AsyncStream.
@@ -0,0 +1,437 @@
import Synchronization
@available(macOS 26.0, *)
extension AsyncSequence where Element: Sendable {
@available(macOS 26.0, *) // TODO: fix the availability for this to be defined as @available(AsyncAlgorithms 1.1, *)
public func share(bufferingPolicy: AsyncBufferSequencePolicy = .unbounded) -> some AsyncSequence<Element, Failure> & Sendable {
return AsyncShareSequence(self, bufferingPolicy: bufferingPolicy)
}
}
@available(macOS 26.0, *)
struct AsyncShareSequence<Base: AsyncSequence>: Sendable where Base.Element: Sendable {
final class Side {
struct State {
var continuaton: CheckedContinuation<Result<Element?, Failure>, Never>?
var position = 0
func offset(_ adjustment: Int) -> State {
State(continuaton: continuaton, position: position - adjustment)
}
}
let iteration: Iteration
let id: Int
init(_ iteration: Iteration) {
self.iteration = iteration
id = iteration.registerSide()
}
deinit {
iteration.unregisterSide(id)
}
func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? {
try await iteration.next(isolation: actor, id: id)
}
}
final class Iteration: Sendable {
// this is the swapped state of transferring the base to the iterating task
// it does send the Base... but only one transfer
enum IteratingTask: @unchecked Sendable {
case pending(Base)
case starting
case running(Task<Void, Never>)
case cancelled
var isStarting: Bool {
switch self {
case .starting: true
default: false
}
}
func cancel() {
switch self {
case .running(let task):
task.cancel()
default:
break
}
}
}
struct State: Sendable {
enum StoragePolicy: Sendable {
case unbounded
case bufferingOldest(Int)
case bufferingNewest(Int)
}
var generation = 0
var sides = [Int: Side.State]()
var iteratingTask: IteratingTask
var buffer = [Element]()
var finished = false
var failure: Failure?
var limit: CheckedContinuation<Bool, Never>?
var demand: CheckedContinuation<Void, Never>?
let storagePolicy: StoragePolicy
init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) {
self.iteratingTask = .pending(base)
switch bufferingPolicy.policy {
case .bounded: self.storagePolicy = .unbounded
case .bufferingOldest(let bound): self.storagePolicy = .bufferingOldest(bound)
case .bufferingNewest(let bound): self.storagePolicy = .bufferingNewest(bound)
case .unbounded: self.storagePolicy = .unbounded
}
}
mutating func trimBuffer() {
if let minimumIndex = sides.values.map({ $0.position }).min(), minimumIndex > 0 {
buffer.removeFirst(minimumIndex)
sides = sides.mapValues {
$0.offset(minimumIndex)
}
}
}
mutating func emit<T>(_ value: T) -> (T, CheckedContinuation<Bool, Never>?, CheckedContinuation<Void, Never>?, Bool) {
defer {
limit = nil
demand = nil
}
if case .cancelled = iteratingTask {
return (value, limit, demand, true)
} else {
return (value, limit, demand, false)
}
}
mutating func enqueue(_ element: Element) {
let count = buffer.count
switch storagePolicy {
case .unbounded:
buffer.append(element)
case .bufferingOldest(let limit):
if count < limit {
buffer.append(element)
}
case .bufferingNewest(let limit):
if count < limit {
buffer.append(element)
} else if count > 0 {
buffer.removeFirst()
buffer.append(element)
}
}
}
mutating func finish() {
finished = true
}
mutating func fail(_ error: Failure) {
finished = true
failure = error
}
}
let state: Mutex<State>
let limit: Int?
init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) {
state = Mutex(State(base, bufferingPolicy: bufferingPolicy))
switch bufferingPolicy.policy {
case .bounded(let limit):
self.limit = limit
default:
self.limit = nil
}
}
func cancel() {
// TODO: this currently is a hard cancel, it should be refined to only cancel when everything is terminal
let (task, limit, demand, cancelled) = state.withLock { state -> (IteratingTask?, CheckedContinuation<Bool, Never>?, CheckedContinuation<Void, Never>?, Bool) in
defer {
state.iteratingTask = .cancelled
state.limit = nil
state.demand = nil
}
return state.emit(state.iteratingTask)
}
task?.cancel()
limit?.resume(returning: cancelled)
demand?.resume()
}
func registerSide() -> Int {
state.withLock { state in
defer { state.generation += 1 }
state.sides[state.generation] = Side.State()
return state.generation
}
}
func unregisterSide(_ id: Int) {
let (side, continuation, cancelled) = state.withLock { state -> (Side.State?, CheckedContinuation<Bool, Never>?, Bool) in
let side = state.sides.removeValue(forKey: id)
state.trimBuffer()
if let limit, state.buffer.count < limit {
defer { state.limit = nil }
if case .cancelled = state.iteratingTask {
return (side, state.limit, true)
} else {
return (side, state.limit, false)
}
} else {
if case .cancelled = state.iteratingTask {
return (side, nil, true)
} else {
return (side, nil, false)
}
}
}
if let continuation {
continuation.resume(returning: cancelled)
}
if let side {
side.continuaton?.resume(returning: .success(nil))
}
}
func iterate() async -> Bool {
if let limit {
let cancelled = await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
let (resume, cancelled) = state.withLock { state -> (CheckedContinuation<Bool, Never>?, Bool) in
if state.buffer.count >= limit {
state.limit = continuation
if case .cancelled = state.iteratingTask {
return (nil, true)
} else {
return (nil, false)
}
} else {
assert(state.limit == nil)
if case .cancelled = state.iteratingTask {
return (continuation, true)
} else {
return (continuation, false)
}
}
}
if let resume {
resume.resume(returning: cancelled)
}
}
if cancelled {
return false
}
}
// await a demand
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
let hasPendingDemand = state.withLock { state in
for (_, side) in state.sides {
if side.continuaton != nil {
return true
}
}
state.demand = continuation
return false
}
if hasPendingDemand {
continuation.resume()
}
}
return state.withLock { state in
switch state.iteratingTask {
case .cancelled:
return false
default:
return true
}
}
}
func cancel(id: Int) {
unregisterSide(id) // doubly unregistering is idempotent but has a side effect of emitting nil if present
}
struct Resumption {
let continuation: CheckedContinuation<Result<Element?, Failure>, Never>
let result: Result<Element?, Failure>
func resume() {
continuation.resume(returning: result)
}
}
func emit(_ result: Result<Element?, Failure>) {
let (resumptions, demandContinuation) = state.withLock { state -> ([Resumption], CheckedContinuation<Void, Never>?) in
var resumptions = [Resumption]()
switch result {
case .success(let element):
if let element {
state.enqueue(element)
} else {
state.finished = true
}
case .failure(let failure):
state.finished = true
state.failure = failure
}
for (id, side) in state.sides {
if let continuation = side.continuaton {
if side.position < state.buffer.count {
resumptions.append(Resumption(continuation: continuation, result: .success(state.buffer[side.position])))
state.sides[id]?.position += 1
state.sides[id]?.continuaton = nil
} else if state.finished {
state.sides[id]?.continuaton = nil
if let failure = state.failure {
resumptions.append(Resumption(continuation: continuation, result: .failure(failure)))
} else {
resumptions.append(Resumption(continuation: continuation, result: .success(nil)))
}
}
}
}
state.trimBuffer()
if let limit, state.buffer.count < limit {
defer {
state.demand = nil
}
return (resumptions, state.demand)
} else {
return (resumptions, nil)
}
}
if let demandContinuation {
demandContinuation.resume()
}
for resumption in resumptions {
resumption.resume()
}
}
func next(isolation actor: isolated (any Actor)?, id: Int) async throws(Failure) -> Element? {
let (base, cancelled) = state.withLock { state -> (Base?, Bool) in
switch state.iteratingTask {
case .pending(let base):
state.iteratingTask = .starting
return (base, false)
case .cancelled:
return (nil, true)
default:
return (nil, false)
}
}
if cancelled { return nil }
if let base {
nonisolated(unsafe) let transfer = base.makeAsyncIterator()
let task = Task.detached { [transfer, self] in
var iterator = transfer
do {
while await iterate() {
if let element = try await iterator.next() {
emit(.success(element))
} else {
emit(.success(nil))
}
}
} catch {
emit(.failure(error as! Failure))
}
}
state.withLock { state in
precondition(state.iteratingTask.isStarting)
state.iteratingTask = .running(task)
}
}
let result: Result<Element?, Failure> = await withTaskCancellationHandler {
await withCheckedContinuation { continuation in
let (res, limitContinuation, demandContinuation, cancelled) = state.withLock { state -> (Result<Element?, Failure>?, CheckedContinuation<Bool, Never>?, CheckedContinuation<Void, Never>?, Bool) in
let side = state.sides[id]!
if side.position < state.buffer.count {
// There's an element available at this position
let element = state.buffer[side.position]
state.sides[id]?.position += 1
state.trimBuffer()
return state.emit(.success(element))
} else {
// Position is beyond the buffer
if let failure = state.failure {
return state.emit(.failure(failure))
} else if state.finished {
return state.emit(.success(nil))
} else {
state.sides[id]?.continuaton = continuation
return state.emit(nil)
}
}
}
if let limitContinuation {
limitContinuation.resume(returning: cancelled)
}
if let demandContinuation {
demandContinuation.resume()
}
if let res {
continuation.resume(returning: res)
}
}
} onCancel: {
cancel(id: id)
}
return try result.get()
}
}
final class Extent: Sendable {
let iteration: Iteration
init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) {
iteration = Iteration(base, bufferingPolicy: bufferingPolicy)
}
deinit {
iteration.cancel()
}
}
let extent: Extent
init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) {
extent = Extent(base, bufferingPolicy: bufferingPolicy)
}
}
@available(macOS 26.0, *)
extension AsyncShareSequence: AsyncSequence {
typealias Element = Base.Element
typealias Failure = Base.Failure
struct Iterator: AsyncIteratorProtocol {
let side: Side
init(_ iteration: Iteration) {
side = Side(iteration)
}
mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? {
try await side.next(isolation: actor)
}
}
func makeAsyncIterator() -> Iterator {
Iterator(extent.iteration)
}
}