Skip to content

Commit 6298d41

Browse files
Reapply "Fix quadratic performance of the ListMerger in specific usage pattern"
This reverts commit 2640ff6.
1 parent d812e11 commit 6298d41

File tree

16 files changed

+857
-977
lines changed

16 files changed

+857
-977
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)

include/swift/ABI/MetadataValues.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,32 @@ inline int descendingPriorityOrder(JobPriority lhs,
25112511
return (lhs == rhs ? 0 : lhs > rhs ? -1 : 1);
25122512
}
25132513

2514+
enum { PriorityBucketCount = 5 };
2515+
2516+
inline int getPriorityBucketIndex(JobPriority priority) {
2517+
// Any unknown priorities will be rounded up to a known one.
2518+
// Priorities higher than UserInteractive are clamped to UserInteractive.
2519+
// Jobs of unknown priorities will end up in the same bucket as jobs of a
2520+
// corresponding known priority. Within the bucket they will be sorted in
2521+
// FIFO order.
2522+
if (priority > JobPriority::UserInitiated) {
2523+
// UserInteractive and higher
2524+
return 0;
2525+
} else if (priority > JobPriority::Default) {
2526+
// UserInitiated
2527+
return 1;
2528+
} else if (priority > JobPriority::Utility) {
2529+
// Default
2530+
return 2;
2531+
} else if (priority > JobPriority::Background) {
2532+
// Utility
2533+
return 3;
2534+
} else {
2535+
// Background and lower
2536+
return 4;
2537+
}
2538+
}
2539+
25142540
inline JobPriority withUserInteractivePriorityDowngrade(JobPriority priority) {
25152541
return (priority == JobPriority::UserInteractive) ? JobPriority::UserInitiated
25162542
: priority;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- HeaderFooterLayout.h -----------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 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+
#ifndef SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H
14+
#define SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H
15+
16+
namespace swift {
17+
18+
template <class Header, class Footer, size_t TotalSize>
19+
struct HeaderFooterLayoutPadding {
20+
private:
21+
enum : ptrdiff_t {
22+
maxFooterOffset = TotalSize - (ptrdiff_t)sizeof(Footer),
23+
footerAlignment = (ptrdiff_t)alignof(Footer),
24+
footerOffset = maxFooterOffset - (maxFooterOffset % footerAlignment),
25+
size = footerOffset - (ptrdiff_t)sizeof(Header)
26+
};
27+
char padding[size];
28+
};
29+
30+
template <class Header, class Footer, size_t TotalSize>
31+
struct HeaderFooterLayout
32+
: Header,
33+
HeaderFooterLayoutPadding<Header, Footer, TotalSize>,
34+
Footer {};
35+
36+
} // namespace swift
37+
38+
#endif // SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H
39+

0 commit comments

Comments
 (0)