Skip to content

Commit 69f5450

Browse files
Added benchmark for adding jobs to default actor when visiting a tree
1 parent 1b848ac commit 69f5450

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set(SWIFT_BENCH_MODULES
4040
single-source/ArrayRemoveAll
4141
single-source/ArraySetElement
4242
single-source/ArraySubscript
43+
single-source/AsyncTree
4344
single-source/BinaryFloatingPointConversionFromBinaryInteger
4445
single-source/BinaryFloatingPointProperties
4546
single-source/BitCount
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===--- AsyncTree.swift -------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
import Dispatch
15+
16+
public var benchmarks: [BenchmarkInfo] {
17+
guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) else {
18+
return []
19+
}
20+
return [
21+
BenchmarkInfo(
22+
name: "AsyncTree.100",
23+
runFunction: run_AsyncTree(treeSize: 100),
24+
tags: [.concurrency]
25+
),
26+
BenchmarkInfo(
27+
name: "AsyncTree.5000",
28+
runFunction: run_AsyncTree(treeSize: 5000),
29+
tags: [.concurrency]
30+
)
31+
]
32+
}
33+
34+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
35+
private actor MyActor {
36+
let g: DispatchGroup
37+
38+
init(_ g: DispatchGroup) {
39+
self.g = g
40+
}
41+
42+
func test(_ n: Int) {
43+
let L = n / 2
44+
let R = n - 1 - L
45+
46+
if L > 0 {
47+
Task {
48+
self.test(L)
49+
}
50+
}
51+
52+
if R > 0 {
53+
Task {
54+
self.test(R)
55+
}
56+
}
57+
58+
g.leave()
59+
}
60+
}
61+
62+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
63+
private func run_AsyncTree(treeSize: Int) -> (Int) -> Void {
64+
return { n in
65+
for _ in 0..<n {
66+
let g = DispatchGroup()
67+
for _ in 0..<treeSize {
68+
g.enter()
69+
}
70+
let actor = MyActor(g)
71+
Task {
72+
await actor.test(treeSize)
73+
}
74+
g.wait()
75+
}
76+
}
77+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import ArrayOfRef
2828
import ArrayRemoveAll
2929
import ArraySetElement
3030
import ArraySubscript
31+
import AsyncTree
3132
import BinaryFloatingPointConversionFromBinaryInteger
3233
import BinaryFloatingPointProperties
3334
import BitCount
@@ -223,6 +224,7 @@ register(ArrayOfRef.benchmarks)
223224
register(ArrayRemoveAll.benchmarks)
224225
register(ArraySetElement.benchmarks)
225226
register(ArraySubscript.benchmarks)
227+
register(AsyncTree.benchmarks)
226228
register(BinaryFloatingPointConversionFromBinaryInteger.benchmarks)
227229
register(BinaryFloatingPointProperties.benchmarks)
228230
register(BitCount.benchmarks)

0 commit comments

Comments
 (0)