Skip to content

[6.0] Fix quadratic performance of the ListMerger in specific usage pattern #74112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(SWIFT_BENCH_MODULES
single-source/ArrayRemoveAll
single-source/ArraySetElement
single-source/ArraySubscript
single-source/AsyncTree
single-source/BinaryFloatingPointConversionFromBinaryInteger
single-source/BinaryFloatingPointProperties
single-source/BitCount
Expand Down
77 changes: 77 additions & 0 deletions benchmark/single-source/AsyncTree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===--- AsyncTree.swift -------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils
import Dispatch

public var benchmarks: [BenchmarkInfo] {
guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) else {
return []
}
return [
BenchmarkInfo(
name: "AsyncTree.100",
runFunction: run_AsyncTree(treeSize: 100),
tags: [.concurrency]
),
BenchmarkInfo(
name: "AsyncTree.5000",
runFunction: run_AsyncTree(treeSize: 5000),
tags: [.concurrency]
)
]
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
private actor MyActor {
let g: DispatchGroup

init(_ g: DispatchGroup) {
self.g = g
}

func test(_ n: Int) {
let L = n / 2
let R = n - 1 - L

if L > 0 {
Task {
self.test(L)
}
}

if R > 0 {
Task {
self.test(R)
}
}

g.leave()
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
private func run_AsyncTree(treeSize: Int) -> (Int) -> Void {
return { n in
for _ in 0..<n {
let g = DispatchGroup()
for _ in 0..<treeSize {
g.enter()
}
let actor = MyActor(g)
Task {
await actor.test(treeSize)
}
g.wait()
}
}
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import ArrayOfRef
import ArrayRemoveAll
import ArraySetElement
import ArraySubscript
import AsyncTree
import BinaryFloatingPointConversionFromBinaryInteger
import BinaryFloatingPointProperties
import BitCount
Expand Down Expand Up @@ -223,6 +224,7 @@ register(ArrayOfRef.benchmarks)
register(ArrayRemoveAll.benchmarks)
register(ArraySetElement.benchmarks)
register(ArraySubscript.benchmarks)
register(AsyncTree.benchmarks)
register(BinaryFloatingPointConversionFromBinaryInteger.benchmarks)
register(BinaryFloatingPointProperties.benchmarks)
register(BitCount.benchmarks)
Expand Down
26 changes: 26 additions & 0 deletions include/swift/ABI/MetadataValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,32 @@ inline int descendingPriorityOrder(JobPriority lhs,
return (lhs == rhs ? 0 : lhs > rhs ? -1 : 1);
}

enum { PriorityBucketCount = 5 };

inline int getPriorityBucketIndex(JobPriority priority) {
// Any unknown priorities will be rounded up to a known one.
// Priorities higher than UserInteractive are clamped to UserInteractive.
// Jobs of unknown priorities will end up in the same bucket as jobs of a
// corresponding known priority. Within the bucket they will be sorted in
// FIFO order.
if (priority > JobPriority::UserInitiated) {
// UserInteractive and higher
return 0;
} else if (priority > JobPriority::Default) {
// UserInitiated
return 1;
} else if (priority > JobPriority::Utility) {
// Default
return 2;
} else if (priority > JobPriority::Background) {
// Utility
return 3;
} else {
// Background and lower
return 4;
}
}

inline JobPriority withUserInteractivePriorityDowngrade(JobPriority priority) {
return (priority == JobPriority::UserInteractive) ? JobPriority::UserInitiated
: priority;
Expand Down
59 changes: 59 additions & 0 deletions include/swift/Basic/HeaderFooterLayout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===--- HeaderFooterLayout.h -----------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H
#define SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H

#include <cstddef>

namespace swift {

template <class T>
class size_without_trailing_padding {
struct ExtraByte { char _size_without_trailing_padding_probe; };
struct Probe: T, ExtraByte {};
public:
enum { value = offsetof(Probe, _size_without_trailing_padding_probe) };
};

namespace detail {

template <ptrdiff_t size>
struct LayoutPadding {
char padding[size];
};
template <>
struct LayoutPadding<0> {};

template <class Header, class Footer, size_t TotalSize>
struct HeaderFooterLayoutPaddingSize {
enum : ptrdiff_t {
maxFooterOffset = TotalSize - (ptrdiff_t)size_without_trailing_padding<Footer>::value,
footerAlignment = (ptrdiff_t)alignof(Footer),
footerOffset = maxFooterOffset - (maxFooterOffset % footerAlignment),
value = footerOffset - (ptrdiff_t)size_without_trailing_padding<Header>::value
};
};

} // namespace detail

template <class Header, class Footer, size_t TotalSize>
struct HeaderFooterLayout
: Header,
detail::LayoutPadding<detail::HeaderFooterLayoutPaddingSize<
Header, Footer, TotalSize>::value>,
Footer {};

} // namespace swift

#endif // SWIFT_BASIC_HEADER_FOOTER_LAYOUT_H

Loading