Skip to content

Commit 3b4eacc

Browse files
Update benchmarks to Swift 4 (#14623)
1 parent 6c6f0d0 commit 3b4eacc

15 files changed

+51
-63
lines changed

benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ function (swift_benchmark_compile_archopts)
219219
endif()
220220
endif()
221221

222-
set(common_swift3_options ${common_options} "-swift-version" "3")
223222
set(common_swift4_options ${common_options} "-swift-version" "4")
224223

225224
# Always optimize the driver modules.
@@ -247,7 +246,7 @@ function (swift_benchmark_compile_archopts)
247246
SOURCE_DIR "${srcdir}"
248247
OBJECT_DIR "${objdir}"
249248
SOURCES ${sources}
250-
LIBRARY_FLAGS ${common_swift3_options})
249+
LIBRARY_FLAGS ${common_swift4_options})
251250
precondition(objfile_out)
252251
list(APPEND bench_library_objects "${objfile_out}")
253252
if (SWIFT_BENCHMARK_EMIT_SIB)
@@ -316,7 +315,7 @@ function (swift_benchmark_compile_archopts)
316315
${stdlib_dependencies} ${bench_library_objects}
317316
"${srcdir}/${module_name_path}.swift"
318317
COMMAND "${SWIFT_EXEC}"
319-
${common_swift3_options}
318+
${common_swift4_options}
320319
${extra_options}
321320
"-parse-as-library"
322321
${bench_flags}
@@ -334,7 +333,7 @@ function (swift_benchmark_compile_archopts)
334333
${stdlib_dependencies} ${bench_library_sibfiles}
335334
"${srcdir}/${module_name_path}.swift"
336335
COMMAND "${SWIFT_EXEC}"
337-
${common_swift3_options}
336+
${common_swift4_options}
338337
"-parse-as-library"
339338
${bench_flags}
340339
"-module-name" "${module_name}"
@@ -364,7 +363,7 @@ function (swift_benchmark_compile_archopts)
364363
SOURCE_DIR "${srcdir}"
365364
OBJECT_DIR "${objdir}"
366365
SOURCES ${${module_name}_sources}
367-
LIBRARY_FLAGS ${common_swift3_options} ${bench_flags}
366+
LIBRARY_FLAGS ${common_swift4_options} ${bench_flags}
368367
DEPENDS ${bench_library_objects} ${stdlib_dependencies})
369368
precondition(objfile_out)
370369
list(APPEND SWIFT_BENCH_OBJFILES "${objfile_out}")
@@ -382,7 +381,7 @@ function (swift_benchmark_compile_archopts)
382381
SOURCE_DIR "${srcdir}"
383382
OBJECT_DIR "${objdir}"
384383
SOURCES ${${module_name}_sources}
385-
LIBRARY_FLAGS ${common_swift3_options} ${bench_flags}
384+
LIBRARY_FLAGS ${common_swift4_options} ${bench_flags}
386385
DEPENDS ${bench_library_objects} ${stdlib_dependencies})
387386
precondition(objfiles_out)
388387
list(APPEND SWIFT_BENCH_OBJFILES ${objfiles_out})
@@ -435,7 +434,7 @@ function (swift_benchmark_compile_archopts)
435434
${bench_library_sibfiles} ${bench_driver_sibfiles}
436435
${SWIFT_BENCH_SIBFILES} "${source}"
437436
COMMAND "${SWIFT_EXEC}"
438-
${common_swift3_options}
437+
${common_swift4_options}
439438
"-force-single-frontend-invocation"
440439
"-emit-module" "-module-name" "${module_name}"
441440
"-I" "${objdir}"

benchmark/single-source/ArraySubscript.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func run_ArraySubscript(_ N: Int) {
3030
var arrays = [[Int]](repeating: [], count: numArrays)
3131
for i in 0..<numArrays {
3232
for _ in 0..<numArrayElements {
33-
arrays[i].append(Int(truncatingBitPattern: Random()))
33+
arrays[i].append(Int(truncatingIfNeeded: Random()))
3434
}
3535
}
3636

benchmark/single-source/CString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public func run_StringWithCString(_ N: Int) {
3737

3838
@inline(never)
3939
public func run_CStringLongAscii(_ N: Int) {
40-
var res: UInt = 0
40+
var res = 0
4141
for _ in 1...N*500 {
4242
// static string to c -> from c to String -> implicit conversion
4343
res &= strlen(ascii.withCString(String.init(cString:)))
@@ -47,7 +47,7 @@ public func run_CStringLongAscii(_ N: Int) {
4747

4848
@inline(never)
4949
public func run_CStringLongNonAscii(_ N: Int) {
50-
var res: UInt = 0
50+
var res = 0
5151
for _ in 1...N*500 {
5252
res &= strlen(japanese.withCString(String.init(cString:)))
5353
}

benchmark/single-source/Hash.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ class Hash {
114114
final
115115
func rol(_ x: UInt32, _ c: UInt32) -> UInt32 {
116116
// TODO: use the &>> operator.
117-
let a = UInt32(truncatingBitPattern: Int64(x) << Int64(c))
118-
let b = UInt32(truncatingBitPattern: Int64(x) >> (32 - Int64(c)))
117+
let a = UInt32(truncatingIfNeeded: Int64(x) << Int64(c))
118+
let b = UInt32(truncatingIfNeeded: Int64(x) >> (32 - Int64(c)))
119119
return a|b
120120
}
121121

122122
/// \brief Right-rotate \p x by \p c.
123123
final
124124
func ror(_ x: UInt32, _ c: UInt32) -> UInt32 {
125125
// TODO: use the &>> operator.
126-
let a = UInt32(truncatingBitPattern: Int64(x) >> Int64(c))
127-
let b = UInt32(truncatingBitPattern: Int64(x) << (32 - Int64(c)))
126+
let a = UInt32(truncatingIfNeeded: Int64(x) >> Int64(c))
127+
let b = UInt32(truncatingIfNeeded: Int64(x) << (32 - Int64(c)))
128128
return a|b
129129
}
130130
}
@@ -177,10 +177,10 @@ class MD5 : Hash {
177177
}
178178

179179
func appendBytes(_ Val: Int, _ Message: inout Array<UInt8>, _ Offset : Int) {
180-
Message[Offset] = UInt8(truncatingBitPattern: Val)
181-
Message[Offset + 1] = UInt8(truncatingBitPattern: Val >> 8)
182-
Message[Offset + 2] = UInt8(truncatingBitPattern: Val >> 16)
183-
Message[Offset + 3] = UInt8(truncatingBitPattern: Val >> 24)
180+
Message[Offset] = UInt8(truncatingIfNeeded: Val)
181+
Message[Offset + 1] = UInt8(truncatingIfNeeded: Val >> 8)
182+
Message[Offset + 2] = UInt8(truncatingIfNeeded: Val >> 16)
183+
Message[Offset + 3] = UInt8(truncatingIfNeeded: Val >> 24)
184184
}
185185

186186
override

benchmark/single-source/IterateData.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public let IterateData = BenchmarkInfo(
2121
@inline(never)
2222
func generateData() -> Data {
2323
var data = Data(count: 16 * 1024)
24+
let n = data.count
2425
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) -> () in
25-
for i in 0..<data.count {
26+
for i in 0..<n {
2627
ptr[i] = UInt8(i % 23)
2728
}
2829
}

benchmark/single-source/LuhnAlgoEager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ let charToString = { (c: Character) -> String in String(c) }
144144
let charToInt = stringToInt charToString
145145

146146
func sum<S: Sequence>(_ nums: S)->S.Iterator.Element where S.Iterator.Element: FixedWidthInteger {
147-
return nums.reduce(0) { $0.0 + $0.1 }
147+
return nums.reduce(0, +)
148148
}
149149

150150
func reverse<C: LazyCollectionProtocol>(

benchmark/single-source/LuhnAlgoLazy.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ let stringToInt = freeMemberFunc(String.toInt)
143143
let charToString = { (c: Character) -> String in String(c) }
144144
let charToInt = stringToInt charToString
145145

146-
func sum<S: Sequence>(_ nums: S)->S.Iterator.Element where S.Iterator.Element: FixedWidthInteger {
147-
return nums.reduce(0) { $0.0 + $0.1 }
146+
func sum<S: Sequence>(_ nums: S)->S.Element where S.Element: FixedWidthInteger {
147+
return nums.reduce(0,+)
148148
}
149149

150150
func reverse<C: LazyCollectionProtocol>(
@@ -154,14 +154,14 @@ func reverse<C: LazyCollectionProtocol>(
154154
}
155155

156156
func map<S: LazySequenceProtocol, U>(
157-
_ source: S, _ transform: @escaping (S.Elements.Iterator.Element)->U
157+
_ source: S, _ transform: @escaping (S.Elements.Element)->U
158158
) -> LazyMapSequence<S.Elements,U> {
159159
return source.map(transform)
160160
}
161161

162162
func mapSome<C: LazyCollectionProtocol, U>(
163163
_ source: C,
164-
_ transform: @escaping (C.Elements.Iterator.Element)->U?
164+
_ transform: @escaping (C.Elements.Element)->U?
165165
) -> LazySequence<MapSomeSequenceView<C.Elements, U>> {
166166
return source.mapSome(transform)
167167
}

benchmark/single-source/MonteCarloE.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public func run_MonteCarloE(scale: Int) {
2828
let N = 200000*scale
2929
var intervals = [Bool](repeating: false, count: N)
3030
for _ in 1...N {
31-
let pos = Int(UInt(truncatingBitPattern: Random())%UInt(N))
31+
let pos = Int(UInt(truncatingIfNeeded: Random())%UInt(N))
3232
intervals[pos] = true
3333
}
3434
let numEmptyIntervals = intervals.filter{!$0}.count

benchmark/single-source/MonteCarloPi.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public func run_MonteCarloPi(scale: Int) {
2222
let r = 10000
2323
let N = 500000*scale
2424
for _ in 1...N {
25-
let x = Int(truncatingBitPattern: Random())%r
26-
let y = Int(truncatingBitPattern: Random())%r
25+
let x = Int(truncatingIfNeeded: Random())%r
26+
let y = Int(truncatingIfNeeded: Random())%r
2727
if x*x + y*y < r*r {
2828
pointsInside += 1
2929
}

benchmark/single-source/NibbleSort.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public func run_NibbleSort(_ N: Int) {
2929
}
3030

3131
struct NibbleCollection: RandomAccessCollection, MutableCollection {
32-
typealias Indices = CountableRange<UInt64>
33-
3432
var val: UInt64
3533
init(_ val: UInt64) { self.val = val }
3634

benchmark/single-source/PopFront.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ public func run_PopFrontUnsafePointer(_ N: Int) {
5656
CheckResults(result == arrayCount)
5757
}
5858
}
59-
a.deallocate(capacity: arrayCount)
59+
a.deallocate()
6060
}
6161

benchmark/single-source/RangeIteration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public func run_RangeIterationSigned(_ N: Int) {
5252

5353
@inline(never)
5454
public func run_RangeIterationSigned64(_ N: Int) {
55-
let range: CountableRange<Int64> = 0..<100000
55+
let range: Range<Int64> = 0..<100000
5656
check = 0
5757
for _ in 1...N {
5858
for e in range {
@@ -65,7 +65,7 @@ public func run_RangeIterationSigned64(_ N: Int) {
6565

6666
@inline(never)
6767
public func run_RangeIterationUnsigned(_ N: Int) {
68-
let range: CountableRange<UInt> = 0..<100000
68+
let range: Range<UInt> = 0..<100000
6969
check = 0
7070
for _ in 1...N {
7171
for e in range {

benchmark/single-source/SetTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public func run_SetIsSubsetOf(_ N: Int) {
3333
var otherSet = Set<Int>(minimumCapacity: size)
3434

3535
for _ in 0 ..< size {
36-
set.insert(Int(truncatingBitPattern: Random()))
37-
otherSet.insert(Int(truncatingBitPattern: Random()))
36+
set.insert(Int(truncatingIfNeeded: Random()))
37+
otherSet.insert(Int(truncatingIfNeeded: Random()))
3838
}
3939

4040
var isSubset = false
@@ -62,8 +62,8 @@ public func run_SetExclusiveOr(_ N: Int) {
6262
var otherSet = Set<Int>(minimumCapacity: size)
6363

6464
for _ in 0 ..< size {
65-
set.insert(Int(truncatingBitPattern: Random()))
66-
otherSet.insert(Int(truncatingBitPattern: Random()))
65+
set.insert(Int(truncatingIfNeeded: Random()))
66+
otherSet.insert(Int(truncatingIfNeeded: Random()))
6767
}
6868

6969
var xor = Set<Int>()
@@ -83,8 +83,8 @@ public func run_SetUnion(_ N: Int) {
8383
var otherSet = Set<Int>(minimumCapacity: size)
8484

8585
for _ in 0 ..< size {
86-
set.insert(Int(truncatingBitPattern: Random()))
87-
otherSet.insert(Int(truncatingBitPattern: Random()))
86+
set.insert(Int(truncatingIfNeeded: Random()))
87+
otherSet.insert(Int(truncatingIfNeeded: Random()))
8888
}
8989

9090
var or = Set<Int>()
@@ -104,8 +104,8 @@ public func run_SetIntersect(_ N: Int) {
104104
var otherSet = Set<Int>(minimumCapacity: size)
105105

106106
for _ in 0 ..< size {
107-
set.insert(Int(truncatingBitPattern: Random()))
108-
otherSet.insert(Int(truncatingBitPattern: Random()))
107+
set.insert(Int(truncatingIfNeeded: Random()))
108+
otherSet.insert(Int(truncatingIfNeeded: Random()))
109109
}
110110

111111
var and = Set<Int>()
@@ -141,8 +141,8 @@ public func run_SetIsSubsetOf_OfObjects(_ N: Int) {
141141
var otherSet = Set<Box<Int>>(minimumCapacity: size)
142142

143143
for _ in 0 ..< size {
144-
set.insert(Box(Int(truncatingBitPattern: Random())))
145-
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
144+
set.insert(Box(Int(truncatingIfNeeded: Random())))
145+
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
146146
}
147147

148148
var isSubset = false
@@ -170,8 +170,8 @@ public func run_SetExclusiveOr_OfObjects(_ N: Int) {
170170
var otherSet = Set<Box<Int>>(minimumCapacity: size)
171171

172172
for _ in 0 ..< size {
173-
set.insert(Box(Int(truncatingBitPattern: Random())))
174-
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
173+
set.insert(Box(Int(truncatingIfNeeded: Random())))
174+
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
175175
}
176176

177177
var xor = Set<Box<Int>>()
@@ -191,8 +191,8 @@ public func run_SetUnion_OfObjects(_ N: Int) {
191191
var otherSet = Set<Box<Int>>(minimumCapacity: size)
192192

193193
for _ in 0 ..< size {
194-
set.insert(Box(Int(truncatingBitPattern: Random())))
195-
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
194+
set.insert(Box(Int(truncatingIfNeeded: Random())))
195+
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
196196
}
197197

198198
var or = Set<Box<Int>>()
@@ -212,8 +212,8 @@ public func run_SetIntersect_OfObjects(_ N: Int) {
212212
var otherSet = Set<Box<Int>>(minimumCapacity: size)
213213

214214
for _ in 0 ..< size {
215-
set.insert(Box(Int(truncatingBitPattern: Random())))
216-
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
215+
set.insert(Box(Int(truncatingIfNeeded: Random())))
216+
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
217217
}
218218

219219
var and = Set<Box<Int>>()

benchmark/single-source/StaticArray.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ struct A2X<T : StaticArrayProtocol> : StaticArrayProtocol {
4848

4949
struct StaticArray<
5050
T : StaticArrayProtocol
51-
> : StaticArrayProtocol, RandomAccessCollection, MutableCollection {
52-
typealias Indices = CountableRange<Int>
53-
51+
> : StaticArrayProtocol, RandomAccessCollection, MutableCollection {
5452
init(_ defaultValue : T.ElemTy) { values = T(defaultValue) }
5553
var values : T
5654
func get(_ idx: Int) -> T.ElemTy { return values.get(idx) }

benchmark/single-source/StringMatch.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@ public let StringMatch = BenchmarkInfo(
2222
runFunction: run_StringMatch,
2323
tags: [.validation, .api, .String])
2424

25-
extension String {
26-
@inline(__always)
27-
func dropFirst(_ n: Int = 1) -> String {
28-
let startIndex = self.index(self.startIndex, offsetBy: n)
29-
return self[startIndex ..< self.endIndex]
30-
}
31-
}
32-
3325
/* match: search for regexp anywhere in text */
3426
func match(regexp: String, text: String) -> Bool {
3527
if regexp.first == "^" {
36-
return matchHere(regexp.dropFirst(), text)
28+
return matchHere(regexp.dropFirst(), text[...])
3729
}
3830

3931
var idx = text.startIndex
4032
while true { // must look even if string is empty
41-
if matchHere(regexp, text[idx..<text.endIndex]) {
33+
if matchHere(regexp[...], text[idx..<text.endIndex]) {
4234
return true
4335
}
4436
guard idx != text.endIndex else { break }
@@ -50,7 +42,7 @@ func match(regexp: String, text: String) -> Bool {
5042
}
5143

5244
/* matchhere: search for regexp at beginning of text */
53-
func matchHere(_ regexp: String, _ text: String) -> Bool {
45+
func matchHere(_ regexp: Substring, _ text: Substring) -> Bool {
5446
if regexp.isEmpty {
5547
return true
5648
}
@@ -71,7 +63,7 @@ func matchHere(_ regexp: String, _ text: String) -> Bool {
7163
}
7264

7365
/* matchstar: search for c*regexp at beginning of text */
74-
func matchStar(_ c: Character, _ regexp: String, _ text: String) -> Bool {
66+
func matchStar(_ c: Character, _ regexp: Substring, _ text: Substring) -> Bool {
7567
var idx = text.startIndex
7668
while true { /* a * matches zero or more instances */
7769
if matchHere(regexp, text[idx..<text.endIndex]) {

0 commit comments

Comments
 (0)