mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
* 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
72 lines
2.5 KiB
Swift
72 lines
2.5 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
|
|
|
|
final class TestDebounce: XCTestCase {
|
|
func test_delayingValues() throws {
|
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
|
validate {
|
|
"abcd----e---f-g----|"
|
|
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
|
|
"------d----e-----g-|"
|
|
}
|
|
}
|
|
|
|
func test_delayingValues_dangling_last() throws {
|
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
|
validate {
|
|
"abcd----e---f-g-|"
|
|
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
|
|
"------d----e----[g|]"
|
|
}
|
|
}
|
|
|
|
|
|
func test_finishDoesntDebounce() throws {
|
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
|
validate {
|
|
"a|"
|
|
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
|
|
"-[a|]"
|
|
}
|
|
}
|
|
|
|
func test_throwDoesntDebounce() throws {
|
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
|
validate {
|
|
"a^"
|
|
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
|
|
"-^"
|
|
}
|
|
}
|
|
|
|
func test_noValues() throws {
|
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
|
validate {
|
|
"----|"
|
|
$0.inputs[0].debounce(for: .steps(3), clock: $0.clock)
|
|
"----|"
|
|
}
|
|
}
|
|
|
|
func test_Rethrows() async throws {
|
|
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { throw XCTSkip("Skipped due to Clock/Instant/Duration availability") }
|
|
|
|
let debounce = [1].async.debounce(for: .zero, clock: ContinuousClock())
|
|
for await _ in debounce {}
|
|
|
|
let throwingDebounce = [1].async.map { try throwOn(2, $0) }.debounce(for: .zero, clock: ContinuousClock())
|
|
for try await _ in throwingDebounce {}
|
|
}
|
|
}
|