Skip to content

Commit 6cb4d26

Browse files
committed
---
yaml --- r: 347641 b: refs/heads/master c: 33d9e52 h: refs/heads/master i: 347639: 53fbb18
1 parent 1d03289 commit 6cb4d26

File tree

411 files changed

+13695
-2283
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

411 files changed

+13695
-2283
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 90f691eef4aee378af27084030679214a1a39d50
2+
refs/heads/master: 33d9e52e7222f815352aab0634e21e30d9c624a5
33
refs/heads/master-next: 203b3026584ecad859eb328b2e12490099409cd5
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea

trunk/CMakeLists.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -998,17 +998,17 @@ if(SWIFT_NEED_EXPLICIT_LIBDISPATCH)
998998
else()
999999
set(SOURCEKIT_RUNTIME_DIR lib)
10001000
endif()
1001-
swift_install_in_component(sourcekit-inproc
1002-
FILES
1001+
swift_install_in_component(FILES
10031002
$<TARGET_FILE:dispatch>
10041003
$<TARGET_FILE:BlocksRuntime>
1005-
DESTINATION ${SOURCEKIT_RUNTIME_DIR})
1004+
DESTINATION ${SOURCEKIT_RUNTIME_DIR}
1005+
COMPONENT sourcekit-inproc)
10061006
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
1007-
swift_install_in_component(sourcekit-inproc
1008-
FILES
1007+
swift_install_in_component(FILES
10091008
$<TARGET_LINKER_FILE:dispatch>
10101009
$<TARGET_LINKER_FILE:BlocksRuntime>
1011-
DESTINATION lib)
1010+
DESTINATION lib
1011+
COMPONENT sourcekit-inproc)
10121012
endif()
10131013

10141014

@@ -1083,9 +1083,9 @@ endif()
10831083

10841084
add_subdirectory(cmake/modules)
10851085

1086-
swift_install_in_component(license
1087-
FILES "LICENSE.txt"
1088-
DESTINATION "share/swift")
1086+
swift_install_in_component(FILES "LICENSE.txt"
1087+
DESTINATION "share/swift"
1088+
COMPONENT license)
10891089

10901090
# Add a documentation target so that documentation shows up in the
10911091
# Xcode project.

trunk/apinotes/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ add_custom_target("copy_apinotes"
3131
# This is treated as an OPTIONAL target because if we don't build the SDK
3232
# overlay, the files will be missing anyway. It also allows us to build
3333
# single overlays without installing the API notes.
34-
swift_install_in_component(sdk-overlay
35-
DIRECTORY "${output_dir}"
36-
DESTINATION "lib/swift/"
37-
OPTIONAL)
34+
swift_install_in_component(DIRECTORY "${output_dir}"
35+
DESTINATION "lib/swift/"
36+
COMPONENT sdk-overlay
37+
OPTIONAL)

trunk/benchmark/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set(SWIFT_BENCH_MODULES
4343
single-source/BinaryFloatingPointProperties
4444
single-source/BitCount
4545
single-source/Breadcrumbs
46+
single-source/BucketSort
4647
single-source/ByteSwap
4748
single-source/COWTree
4849
single-source/COWArrayGuaranteedParameterOverhead
@@ -85,6 +86,7 @@ set(SWIFT_BENCH_MODULES
8586
single-source/ExistentialPerformance
8687
single-source/Fibonacci
8788
single-source/FlattenList
89+
single-source/FloatingPointParsing
8890
single-source/FloatingPointPrinting
8991
single-source/Hanoi
9092
single-source/Hash

trunk/benchmark/single-source/Breadcrumbs.swift

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,6 @@ extension String {
4545
}
4646
}
4747

48-
let seed = 0x12345678
49-
50-
/// A linear congruential PRNG.
51-
struct LCRNG: RandomNumberGenerator {
52-
private var state: UInt64
53-
54-
init(seed: Int) {
55-
state = UInt64(truncatingIfNeeded: seed)
56-
for _ in 0..<10 { _ = next() }
57-
}
58-
59-
mutating func next() -> UInt64 {
60-
state = 2862933555777941757 &* state &+ 3037000493
61-
return state
62-
}
63-
}
64-
6548
extension Collection {
6649
/// Returns a randomly ordered array of random non-overlapping index ranges
6750
/// that cover this collection entirely.
@@ -254,7 +237,7 @@ class UTF16ToIdx: BenchmarkBase {
254237

255238
override func setUp() {
256239
super.setUp()
257-
var rng = LCRNG(seed: seed)
240+
var rng = SplitMix64(seed: 42)
258241
let range = 0 ..< inputString.utf16.count
259242
inputOffsets = Array(range.shuffled(using: &rng).prefix(count))
260243
}
@@ -286,7 +269,7 @@ class IdxToUTF16: BenchmarkBase {
286269

287270
override func setUp() {
288271
super.setUp()
289-
var rng = LCRNG(seed: seed)
272+
var rng = SplitMix64(seed: 42)
290273
inputIndices = Array(inputString.indices.shuffled(using: &rng).prefix(count))
291274
}
292275

@@ -318,7 +301,7 @@ class UTF16ToIdxRange: BenchmarkBase {
318301

319302
override func setUp() {
320303
super.setUp()
321-
var rng = LCRNG(seed: seed)
304+
var rng = SplitMix64(seed: 42)
322305
inputOffsets = (
323306
0 ..< inputString.utf16.count
324307
).randomIndexRanges(count: count, using: &rng)
@@ -352,7 +335,7 @@ class IdxToUTF16Range: BenchmarkBase {
352335

353336
override func setUp() {
354337
super.setUp()
355-
var rng = LCRNG(seed: seed)
338+
var rng = SplitMix64(seed: 42)
356339
inputIndices = self.inputString.randomIndexRanges(count: count, using: &rng)
357340
}
358341

@@ -384,7 +367,7 @@ class CopyUTF16CodeUnits: BenchmarkBase {
384367

385368
override func setUp() {
386369
super.setUp()
387-
var rng = LCRNG(seed: seed)
370+
var rng = SplitMix64(seed: 42)
388371
inputIndices = (
389372
0 ..< inputString.utf16.count
390373
).randomIndexRanges(count: count, using: &rng)
@@ -425,7 +408,7 @@ class MutatedUTF16ToIdx: BenchmarkBase {
425408

426409
override func setUp() {
427410
super.setUp()
428-
var generator = LCRNG(seed: seed)
411+
var generator = SplitMix64(seed: 42)
429412
let range = 0 ..< inputString.utf16.count
430413
inputOffsets = Array(range.shuffled(using: &generator).prefix(count))
431414
}
@@ -469,7 +452,7 @@ class MutatedIdxToUTF16: BenchmarkBase {
469452

470453
override func setUp() {
471454
super.setUp()
472-
var rng = LCRNG(seed: seed)
455+
var rng = SplitMix64(seed: 42)
473456
inputIndices = Array(inputString.indices.shuffled(using: &rng).prefix(count))
474457
}
475458

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//===--- BucketSort.swift -------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
// This benchmark demonstrates the benefits of ExistentialSpecializer
13+
// optimization pass. It's a generic version of the classic BucketSort algorithm
14+
// adapted from an original implementation from the Swift Algorithm Club.
15+
// See https://en.wikipedia.org/wiki/Bucket_sort and
16+
// https://github.com/raywenderlich/swift-algorithm-club/tree/master/Bucket%20Sort
17+
//
18+
// It sorts an array of generic `SortableItem`s. If the type of `sortingAlgo`
19+
// is not known to the call site at line 90, the `sort` method can not be
20+
// specialized to integer array sorting, which will lead to a huge performance
21+
// loss. Since `SortingAlgorithm` and `InsertionSort` are declared to be
22+
// `public` and the lines 89-91 can not be inlined in `bucketSort` (due to
23+
// inlining heuristic limitations), compiler without ExistentialSpecializer
24+
// optimization can not achieve this feat. With ExistentialSpecializer which
25+
// enables generic specialization recursively in a call chain, we're able to
26+
// specialize line 90 for `InsertionSort` on integers.
27+
28+
import TestsUtils
29+
import Foundation
30+
31+
public let BucketSort = BenchmarkInfo(
32+
name: "BucketSort",
33+
runFunction: run_BucketSort,
34+
tags: [.validation, .algorithm],
35+
setUpFunction: { blackHole(buckets) }
36+
)
37+
38+
public protocol IntegerConvertible {
39+
func convertToInt() -> Int
40+
}
41+
42+
extension Int: IntegerConvertible, SortableItem {
43+
public func convertToInt() -> Int {
44+
return self
45+
}
46+
}
47+
48+
public protocol SortableItem: IntegerConvertible, Comparable { }
49+
50+
public protocol SortingAlgorithm {
51+
func sort<T: SortableItem>(_ items: [T]) -> [T]
52+
}
53+
54+
public struct InsertionSort: SortingAlgorithm {
55+
public func sort<T: SortableItem>(_ items: [T]) -> [T] {
56+
var sortedItems = items
57+
for i in 0 ..< sortedItems.count {
58+
var j = i
59+
while j > 0 && sortedItems[j-1] > sortedItems[j] {
60+
let temp = sortedItems[j-1]
61+
sortedItems[j-1] = sortedItems[j]
62+
sortedItems[j] = temp
63+
j -= 1
64+
}
65+
}
66+
return sortedItems
67+
}
68+
}
69+
70+
func distribute<T>(_ item: T, bucketArray: inout [Bucket<T>]) {
71+
let val = item.convertToInt()
72+
let capacity = bucketArray.first!.capacity
73+
let index = val / capacity
74+
bucketArray[index].add(item)
75+
}
76+
77+
struct Bucket<T: SortableItem> {
78+
var items: [T]
79+
let capacity: Int
80+
init(capacity: Int) {
81+
self.capacity = capacity
82+
items = [T]()
83+
}
84+
mutating func add(_ item: T) {
85+
if items.count < capacity {
86+
items.append(item)
87+
}
88+
}
89+
func sort(_ sortingAlgo: SortingAlgorithm) -> [T] {
90+
return sortingAlgo.sort(items)
91+
}
92+
}
93+
94+
func bucketSort<T>(
95+
_ items: [T], sortingAlgorithm: SortingAlgorithm, bucketArray: [Bucket<T>]
96+
) -> [T] {
97+
var copyBucketArray = bucketArray
98+
for item in items {
99+
distribute(item, bucketArray: &copyBucketArray)
100+
}
101+
var sortedArray = [T]()
102+
for bucket in copyBucketArray {
103+
sortedArray += bucket.sort(sortingAlgorithm)
104+
}
105+
return sortedArray
106+
}
107+
108+
func isAscending(_ a: [Int]) -> Bool {
109+
return zip(a, a.dropFirst()).allSatisfy(<=)
110+
}
111+
112+
let items: [Int] = {
113+
var g = SplitMix64(seed: 42)
114+
return (0..<10_000).map {_ in Int.random(in: 0..<1000, using: &g) }
115+
}()
116+
117+
let buckets: [Bucket<Int>] = {
118+
let bucketCount = 10
119+
let maxValue = items.max()!.convertToInt()
120+
let maxCapacity = Int(ceil(Double(maxValue + 1) / Double(bucketCount)))
121+
return (0..<bucketCount).map { _ in Bucket<Int>(capacity: maxCapacity) }
122+
}()
123+
124+
@inline(never)
125+
func run_BucketSort(_ N : Int) {
126+
for _ in 0..<N {
127+
let sortedArray = bucketSort(
128+
items, sortingAlgorithm: InsertionSort(), bucketArray: buckets)
129+
CheckResults(isAscending(sortedArray))
130+
}
131+
}

0 commit comments

Comments
 (0)