Skip to content

[Benchmarks] Update benchmarks to Swift 4 #14623

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 1 commit into from
Feb 15, 2018
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
13 changes: 6 additions & 7 deletions benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ function (swift_benchmark_compile_archopts)
endif()
endif()

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

# Always optimize the driver modules.
Expand Down Expand Up @@ -247,7 +246,7 @@ function (swift_benchmark_compile_archopts)
SOURCE_DIR "${srcdir}"
OBJECT_DIR "${objdir}"
SOURCES ${sources}
LIBRARY_FLAGS ${common_swift3_options})
LIBRARY_FLAGS ${common_swift4_options})
precondition(objfile_out)
list(APPEND bench_library_objects "${objfile_out}")
if (SWIFT_BENCHMARK_EMIT_SIB)
Expand Down Expand Up @@ -316,7 +315,7 @@ function (swift_benchmark_compile_archopts)
${stdlib_dependencies} ${bench_library_objects}
"${srcdir}/${module_name_path}.swift"
COMMAND "${SWIFT_EXEC}"
${common_swift3_options}
${common_swift4_options}
${extra_options}
"-parse-as-library"
${bench_flags}
Expand All @@ -334,7 +333,7 @@ function (swift_benchmark_compile_archopts)
${stdlib_dependencies} ${bench_library_sibfiles}
"${srcdir}/${module_name_path}.swift"
COMMAND "${SWIFT_EXEC}"
${common_swift3_options}
${common_swift4_options}
"-parse-as-library"
${bench_flags}
"-module-name" "${module_name}"
Expand Down Expand Up @@ -364,7 +363,7 @@ function (swift_benchmark_compile_archopts)
SOURCE_DIR "${srcdir}"
OBJECT_DIR "${objdir}"
SOURCES ${${module_name}_sources}
LIBRARY_FLAGS ${common_swift3_options} ${bench_flags}
LIBRARY_FLAGS ${common_swift4_options} ${bench_flags}
DEPENDS ${bench_library_objects} ${stdlib_dependencies})
precondition(objfile_out)
list(APPEND SWIFT_BENCH_OBJFILES "${objfile_out}")
Expand All @@ -382,7 +381,7 @@ function (swift_benchmark_compile_archopts)
SOURCE_DIR "${srcdir}"
OBJECT_DIR "${objdir}"
SOURCES ${${module_name}_sources}
LIBRARY_FLAGS ${common_swift3_options} ${bench_flags}
LIBRARY_FLAGS ${common_swift4_options} ${bench_flags}
DEPENDS ${bench_library_objects} ${stdlib_dependencies})
precondition(objfiles_out)
list(APPEND SWIFT_BENCH_OBJFILES ${objfiles_out})
Expand Down Expand Up @@ -435,7 +434,7 @@ function (swift_benchmark_compile_archopts)
${bench_library_sibfiles} ${bench_driver_sibfiles}
${SWIFT_BENCH_SIBFILES} "${source}"
COMMAND "${SWIFT_EXEC}"
${common_swift3_options}
${common_swift4_options}
"-force-single-frontend-invocation"
"-emit-module" "-module-name" "${module_name}"
"-I" "${objdir}"
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/ArraySubscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public func run_ArraySubscript(_ N: Int) {
var arrays = [[Int]](repeating: [], count: numArrays)
for i in 0..<numArrays {
for _ in 0..<numArrayElements {
arrays[i].append(Int(truncatingBitPattern: Random()))
arrays[i].append(Int(truncatingIfNeeded: Random()))
}
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/CString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public func run_StringWithCString(_ N: Int) {

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

@inline(never)
public func run_CStringLongNonAscii(_ N: Int) {
var res: UInt = 0
var res = 0
for _ in 1...N*500 {
res &= strlen(japanese.withCString(String.init(cString:)))
}
Expand Down
16 changes: 8 additions & 8 deletions benchmark/single-source/Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ class Hash {
final
func rol(_ x: UInt32, _ c: UInt32) -> UInt32 {
// TODO: use the &>> operator.
let a = UInt32(truncatingBitPattern: Int64(x) << Int64(c))
let b = UInt32(truncatingBitPattern: Int64(x) >> (32 - Int64(c)))
let a = UInt32(truncatingIfNeeded: Int64(x) << Int64(c))
let b = UInt32(truncatingIfNeeded: Int64(x) >> (32 - Int64(c)))
return a|b
}

/// \brief Right-rotate \p x by \p c.
final
func ror(_ x: UInt32, _ c: UInt32) -> UInt32 {
// TODO: use the &>> operator.
let a = UInt32(truncatingBitPattern: Int64(x) >> Int64(c))
let b = UInt32(truncatingBitPattern: Int64(x) << (32 - Int64(c)))
let a = UInt32(truncatingIfNeeded: Int64(x) >> Int64(c))
let b = UInt32(truncatingIfNeeded: Int64(x) << (32 - Int64(c)))
return a|b
}
}
Expand Down Expand Up @@ -177,10 +177,10 @@ class MD5 : Hash {
}

func appendBytes(_ Val: Int, _ Message: inout Array<UInt8>, _ Offset : Int) {
Message[Offset] = UInt8(truncatingBitPattern: Val)
Message[Offset + 1] = UInt8(truncatingBitPattern: Val >> 8)
Message[Offset + 2] = UInt8(truncatingBitPattern: Val >> 16)
Message[Offset + 3] = UInt8(truncatingBitPattern: Val >> 24)
Message[Offset] = UInt8(truncatingIfNeeded: Val)
Message[Offset + 1] = UInt8(truncatingIfNeeded: Val >> 8)
Message[Offset + 2] = UInt8(truncatingIfNeeded: Val >> 16)
Message[Offset + 3] = UInt8(truncatingIfNeeded: Val >> 24)
}

override
Expand Down
3 changes: 2 additions & 1 deletion benchmark/single-source/IterateData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public let IterateData = BenchmarkInfo(
@inline(never)
func generateData() -> Data {
var data = Data(count: 16 * 1024)
let n = data.count
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) -> () in
for i in 0..<data.count {
for i in 0..<n {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing optimizer's job ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah, suppressing an exclusive access warning

ptr[i] = UInt8(i % 23)
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/LuhnAlgoEager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ let charToString = { (c: Character) -> String in String(c) }
let charToInt = stringToInt • charToString

func sum<S: Sequence>(_ nums: S)->S.Iterator.Element where S.Iterator.Element: FixedWidthInteger {
return nums.reduce(0) { $0.0 + $0.1 }
return nums.reduce(0, +)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll start failing eventually.. I think. When the SE-110 is reconsidered again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike that change so this is a pro not a con AFAIAC :), but the reason I changed it is cos it's now $0 and $1 so I think it's SE-110-compatible.

}

func reverse<C: LazyCollectionProtocol>(
Expand Down
8 changes: 4 additions & 4 deletions benchmark/single-source/LuhnAlgoLazy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ let stringToInt = freeMemberFunc(String.toInt)
let charToString = { (c: Character) -> String in String(c) }
let charToInt = stringToInt • charToString

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

func reverse<C: LazyCollectionProtocol>(
Expand All @@ -154,14 +154,14 @@ func reverse<C: LazyCollectionProtocol>(
}

func map<S: LazySequenceProtocol, U>(
_ source: S, _ transform: @escaping (S.Elements.Iterator.Element)->U
_ source: S, _ transform: @escaping (S.Elements.Element)->U
) -> LazyMapSequence<S.Elements,U> {
return source.map(transform)
}

func mapSome<C: LazyCollectionProtocol, U>(
_ source: C,
_ transform: @escaping (C.Elements.Iterator.Element)->U?
_ transform: @escaping (C.Elements.Element)->U?
) -> LazySequence<MapSomeSequenceView<C.Elements, U>> {
return source.mapSome(transform)
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/MonteCarloE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public func run_MonteCarloE(scale: Int) {
let N = 200000*scale
var intervals = [Bool](repeating: false, count: N)
for _ in 1...N {
let pos = Int(UInt(truncatingBitPattern: Random())%UInt(N))
let pos = Int(UInt(truncatingIfNeeded: Random())%UInt(N))
intervals[pos] = true
}
let numEmptyIntervals = intervals.filter{!$0}.count
Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/MonteCarloPi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public func run_MonteCarloPi(scale: Int) {
let r = 10000
let N = 500000*scale
for _ in 1...N {
let x = Int(truncatingBitPattern: Random())%r
let y = Int(truncatingBitPattern: Random())%r
let x = Int(truncatingIfNeeded: Random())%r
let y = Int(truncatingIfNeeded: Random())%r
if x*x + y*y < r*r {
pointsInside += 1
}
Expand Down
2 changes: 0 additions & 2 deletions benchmark/single-source/NibbleSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public func run_NibbleSort(_ N: Int) {
}

struct NibbleCollection: RandomAccessCollection, MutableCollection {
typealias Indices = CountableRange<UInt64>

var val: UInt64
init(_ val: UInt64) { self.val = val }

Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/PopFront.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public func run_PopFrontUnsafePointer(_ N: Int) {
CheckResults(result == arrayCount)
}
}
a.deallocate(capacity: arrayCount)
a.deallocate()
}

4 changes: 2 additions & 2 deletions benchmark/single-source/RangeIteration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public func run_RangeIterationSigned(_ N: Int) {

@inline(never)
public func run_RangeIterationSigned64(_ N: Int) {
let range: CountableRange<Int64> = 0..<100000
let range: Range<Int64> = 0..<100000
check = 0
for _ in 1...N {
for e in range {
Expand All @@ -65,7 +65,7 @@ public func run_RangeIterationSigned64(_ N: Int) {

@inline(never)
public func run_RangeIterationUnsigned(_ N: Int) {
let range: CountableRange<UInt> = 0..<100000
let range: Range<UInt> = 0..<100000
check = 0
for _ in 1...N {
for e in range {
Expand Down
32 changes: 16 additions & 16 deletions benchmark/single-source/SetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public func run_SetIsSubsetOf(_ N: Int) {
var otherSet = Set<Int>(minimumCapacity: size)

for _ in 0 ..< size {
set.insert(Int(truncatingBitPattern: Random()))
otherSet.insert(Int(truncatingBitPattern: Random()))
set.insert(Int(truncatingIfNeeded: Random()))
otherSet.insert(Int(truncatingIfNeeded: Random()))
}

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

for _ in 0 ..< size {
set.insert(Int(truncatingBitPattern: Random()))
otherSet.insert(Int(truncatingBitPattern: Random()))
set.insert(Int(truncatingIfNeeded: Random()))
otherSet.insert(Int(truncatingIfNeeded: Random()))
}

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

for _ in 0 ..< size {
set.insert(Int(truncatingBitPattern: Random()))
otherSet.insert(Int(truncatingBitPattern: Random()))
set.insert(Int(truncatingIfNeeded: Random()))
otherSet.insert(Int(truncatingIfNeeded: Random()))
}

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

for _ in 0 ..< size {
set.insert(Int(truncatingBitPattern: Random()))
otherSet.insert(Int(truncatingBitPattern: Random()))
set.insert(Int(truncatingIfNeeded: Random()))
otherSet.insert(Int(truncatingIfNeeded: Random()))
}

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

for _ in 0 ..< size {
set.insert(Box(Int(truncatingBitPattern: Random())))
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
set.insert(Box(Int(truncatingIfNeeded: Random())))
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
}

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

for _ in 0 ..< size {
set.insert(Box(Int(truncatingBitPattern: Random())))
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
set.insert(Box(Int(truncatingIfNeeded: Random())))
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
}

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

for _ in 0 ..< size {
set.insert(Box(Int(truncatingBitPattern: Random())))
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
set.insert(Box(Int(truncatingIfNeeded: Random())))
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
}

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

for _ in 0 ..< size {
set.insert(Box(Int(truncatingBitPattern: Random())))
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
set.insert(Box(Int(truncatingIfNeeded: Random())))
otherSet.insert(Box(Int(truncatingIfNeeded: Random())))
}

var and = Set<Box<Int>>()
Expand Down
4 changes: 1 addition & 3 deletions benchmark/single-source/StaticArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ struct A2X<T : StaticArrayProtocol> : StaticArrayProtocol {

struct StaticArray<
T : StaticArrayProtocol
> : StaticArrayProtocol, RandomAccessCollection, MutableCollection {
typealias Indices = CountableRange<Int>

> : StaticArrayProtocol, RandomAccessCollection, MutableCollection {
init(_ defaultValue : T.ElemTy) { values = T(defaultValue) }
var values : T
func get(_ idx: Int) -> T.ElemTy { return values.get(idx) }
Expand Down
16 changes: 4 additions & 12 deletions benchmark/single-source/StringMatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ public let StringMatch = BenchmarkInfo(
runFunction: run_StringMatch,
tags: [.validation, .api, .String])

extension String {
@inline(__always)
func dropFirst(_ n: Int = 1) -> String {
let startIndex = self.index(self.startIndex, offsetBy: n)
return self[startIndex ..< self.endIndex]
}
}

/* match: search for regexp anywhere in text */
func match(regexp: String, text: String) -> Bool {
if regexp.first == "^" {
return matchHere(regexp.dropFirst(), text)
return matchHere(regexp.dropFirst(), text[...])
}

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

/* matchhere: search for regexp at beginning of text */
func matchHere(_ regexp: String, _ text: String) -> Bool {
func matchHere(_ regexp: Substring, _ text: Substring) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny.. One would think that this would speed things up a little.

StringMatch | 7203 | 8176 | +13.5% | 0.88x

if regexp.isEmpty {
return true
}
Expand All @@ -71,7 +63,7 @@ func matchHere(_ regexp: String, _ text: String) -> Bool {
}

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