Open
Description
Description
It seems that that TaskGroup.addTask doesn't properly use the _swift_retain runtime calls.
We found this when testing https://github.com/ordo-one/package-benchmark that has hooks added to get metrics for the aggregate number of retain/release calls.
As you can see in the sample code here the number of superfluous releases matches the number of addTask calls exactly, so we assume it is not using the normal _swift_retain runtime hook that package-benchmark uses for measuring.
import Benchmark
let benchmarks = {
func concurrentWork(tasks: Int) async {
_ = await withTaskGroup(of: Void.self, returning: Void.self, body: { taskGroup in
for _ in 0 ..< tasks {
taskGroup.addTask {
}
}
for await _ in taskGroup {}
})
}
Benchmark("Retain/release deviation",
configuration: .init(metrics: BenchmarkMetric.arc, maxDuration: .seconds(3))) { _ in
await concurrentWork(tasks: 789)
}
}
Steps to reproduce
hassila@max /tmp> gh repo clone ordo-one/external-reproducers
...
hassila@max /tmp> cd external-reproducers/swift/retain-release-diff/
hassila@max /t/e/s/retain-release-diff (main)> swift package benchmark
...
====================================================================================================
Baseline 'Current run'
====================================================================================================
Host 'max.local' with 10 'arm64' processors with 64 GB memory, running:
Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000
===================
retain-release-diff
===================
Retain/release deviation
╒════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │
╞════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Releases │ 2368 │ 2369 │ 2369 │ 2369 │ 2369 │ 2369 │ 2373 │ 3425 │
├────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Retain / Release Δ │ 789 │ 789 │ 789 │ 789 │ 789 │ 789 │ 793 │ 3425 │
├────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Retains │ 1579 │ 1580 │ 1580 │ 1580 │ 1580 │ 1580 │ 1580 │ 3425 │
╘════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛
Expected behavior
I expect Retain / Release Δ
to be zero for this test.
Environment
- Swift compiler version info
swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51)
Target: arm64-apple-macosx13.0 - Xcode version info
Xcode 14.1
Build version 14B47b - Deployment target: macOS 13