Files
swift-async-algorithms/Tests/AsyncAlgorithmsTests/Performance/TestThroughput.swift
T
Franz Busch f05e450f0b Remove AsyncIterator: Sendable requirement from debounce (#198)
* Remove `AsyncIterator: Sendable` requirement from debounce

# Motivation
The current implementation of `AsyncDebounceSequence` requires the base `AsyncIterator` to be `Sendable`. This is causing two problems:

1. It only allows users to use `debounce` if their `AsyncSequence.AsyncIterator` is `Sendable`
2. In `debounce` we are creating a lot of new `Task`s and reating `Task`s is not cheap.

My main goal of this PR was to remove the `Sendable` constraint from `debounce`.

# Modification
This PR overhauls the implementation of `debounce` and aligns it with the implementation of the open `merge` PR https://github.com/apple/swift-async-algorithms/pull/185 . The most important changes are this:
- I removed the `Sendable` requirement from the base sequences `AsyncIterator`.
- Instead of creating new Tasks for the sleep and for the upstream consumption. I am now creating one Task and manipulate it by signalling continuations
- I am not cancelling the sleep. Instead I am recalculating the time left to sleep when a sleep finishes.

# Result
In the end, this PR swaps the implementation of `AsyncDebounceSequence` and drops the `Sendable` constraint and passes all tests. Furthermore, on my local performance testing I saw up 150% speed increase in throughput.

* Fix https://github.com/apple/swift-async-algorithms/issues/174

* Code review

* Remove lock methods

* Cleanup some unused code

* Setup task after first call to next
2022-09-30 10:12:11 -07:00

75 lines
2.1 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
import AsyncAlgorithms
#if canImport(Darwin)
final class TestThroughput: XCTestCase {
func test_chain2() async {
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) {
chain($0, $1)
}
}
func test_chain3() async {
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2, thirdOutput: 3) {
chain($0, $1, $2)
}
}
func test_compacted() async {
await measureSequenceThroughput(output: .some(1)) {
$0.compacted()
}
}
func test_interspersed() async {
await measureSequenceThroughput(output: 1) {
$0.interspersed(with: 0)
}
}
func test_joined() async {
await measureSequenceThroughput(output: [1, 2, 3, 4, 5].async) {
$0.joined(separator: [0, 0, 0, 0, 0].async)
}
}
func test_merge2() async {
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) {
merge($0, $1)
}
}
func test_merge3() async {
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2, thirdOutput: 3) {
merge($0, $1, $2)
}
}
func test_removeDuplicates() async {
await measureSequenceThroughput(source: (1...).async) {
$0.removeDuplicates()
}
}
func test_zip2() async {
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2) {
zip($0, $1)
}
}
func test_zip3() async {
await measureSequenceThroughput(firstOutput: 1, secondOutput: 2, thirdOutput: 3) {
zip($0, $1, $2)
}
}
@available(macOS 13.0, *)
func test_debounce() async {
await measureSequenceThroughput(source: (1...).async) {
$0.debounce(for: .zero, clock: ContinuousClock())
}
}
}
#endif