Skip to content

[benchmark][NFC] Use Swift naming conventions #39336

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 9 commits into from
Sep 21, 2021
Merged
12 changes: 6 additions & 6 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ set(SWIFT_BENCH_MODULES
single-source/CharacterProperties
single-source/Chars
single-source/ClassArrayGetter
single-source/Codable
single-source/CodableTest
single-source/Combos
single-source/DataBenchmarks
single-source/DeadArray
Expand All @@ -76,7 +76,7 @@ set(SWIFT_BENCH_MODULES
single-source/DictionaryCopy
single-source/DictionaryGroup
single-source/DictionaryKeysContains
single-source/DictionaryLiteral
single-source/DictionaryLiteralTest
single-source/DictionaryOfAnyHashableStrings
single-source/DictionaryRemove
single-source/DictionarySubscriptDefault
Expand Down Expand Up @@ -112,11 +112,11 @@ set(SWIFT_BENCH_MODULES
single-source/LuhnAlgoLazy
single-source/MapReduce
single-source/Memset
single-source/Mirror
single-source/MirrorTest
single-source/MonteCarloE
single-source/MonteCarloPi
single-source/NSDictionaryCastToSwift
single-source/NSError
single-source/NSErrorTest
single-source/NSStringConversion
single-source/NibbleSort
single-source/NIOChannelPipeline
Expand All @@ -143,7 +143,7 @@ set(SWIFT_BENCH_MODULES
single-source/ProtocolConformance
single-source/ProtocolDispatch
single-source/ProtocolDispatch2
single-source/Queue
single-source/QueueTest
single-source/RC4
single-source/RGBHistogram
single-source/Radix2CooleyTukey
Expand Down Expand Up @@ -186,7 +186,7 @@ set(SWIFT_BENCH_MODULES
single-source/StringSwitch
single-source/StringTests
single-source/StringWalk
single-source/Substring
single-source/SubstringTest
single-source/Suffix
single-source/SuperChars
single-source/TwoSum
Expand Down
14 changes: 8 additions & 6 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,18 @@ If needed you can multiply N by a fixed amount (e.g. `1...100*N`) to achieve thi
// rdar://problem/00000000
import TestsUtils

public let YourTestName = BenchmarkInfo(
name: "YourTestName",
runFunction: run_YourTestName,
tags: [.regression])
public let benchmarks = [
BenchmarkInfo(
name: "YourTestName",
runFunction: run_YourTestName,
tags: [.regression])
]

@inline(never)
public func run_YourTestName(N: Int) {
public func run_YourTestName(n: Int) {
# Declare variables

for i in 1...N {
for i in 1...n {
# Perform work

# Verify work was done; break otherwise
Expand Down
14 changes: 8 additions & 6 deletions benchmark/cxx-source/CreateObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
import TestsUtils
import CxxCreateObjects

public let CreateObjects = BenchmarkInfo(
name: "CreateObjects",
runFunction: run_CreateObjects,
tags: [.validation, .bridging])
public let benchmarks = [
BenchmarkInfo(
name: "CreateObjects",
runFunction: run_CreateObjects,
tags: [.validation, .bridging])
]

@inline(never)
public func run_CreateObjects(_ N: Int) {
for i in 0...(N * 10_000) {
public func run_CreateObjects(_ n: Int) {
for i in 0...(n * 10_000) {
let x = Int32(i)
let f = CxxLoadableIntWrapper(value: x)
blackHole(f)
Expand Down
16 changes: 8 additions & 8 deletions benchmark/multi-source/PrimsSplit/Prims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class PriorityQueue {
while (ind != 0) {
let p = getParentIndex(ind)
if heap[p].cost > c {
Swap(p, with: ind)
swap(p, with: ind)
ind = p
} else {
break
Expand All @@ -83,7 +83,7 @@ class PriorityQueue {
if (heap.isEmpty) {
return nil
}
Swap(0, with:heap.count-1)
swap(0, with:heap.count-1)
let r = heap.removeLast()
graphIndexToHeapIndexMap[r.to] = nil
bubbleDown(0)
Expand Down Expand Up @@ -111,21 +111,21 @@ class PriorityQueue {
if (heap[ind].cost <= heap[min].cost) {
break
}
Swap(ind, with: min)
swap(ind, with: min)
ind = min
}
}

// Swaps elements I and J in the heap and correspondingly updates
// graphIndexToHeapIndexMap.
func Swap(_ i: Int, with j : Int) {
func swap(_ i: Int, with j : Int) {
if (i == j) {
return
}
(heap[i], heap[j]) = (heap[j], heap[i])
let (I, J) = (heap[i].to, heap[j].to)
(graphIndexToHeapIndexMap[I], graphIndexToHeapIndexMap[J]) =
(graphIndexToHeapIndexMap[J], graphIndexToHeapIndexMap[I])
let (i2, j2) = (heap[i].to, heap[j].to)
(graphIndexToHeapIndexMap[i2], graphIndexToHeapIndexMap[j2]) =
(graphIndexToHeapIndexMap[j2], graphIndexToHeapIndexMap[i2])
}

// Dumps the heap.
Expand Down Expand Up @@ -182,7 +182,7 @@ extension Edge : Hashable {
}
}

func Prims(_ graph : Array<GraphNode>, _ fun : (Int, Int) -> Double) -> Array<Int?> {
func prims(_ graph : Array<GraphNode>, _ fun : (Int, Int) -> Double) -> Array<Int?> {
var treeEdges = Array<Int?>(repeating:nil, count:graph.count)

let queue = PriorityQueue(Num:graph.count)
Expand Down
20 changes: 11 additions & 9 deletions benchmark/multi-source/PrimsSplit/Prims_main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@

import TestsUtils

public let PrimsSplit = BenchmarkInfo(
name: "PrimsSplit",
runFunction: run_PrimsSplit,
tags: [.validation, .algorithm],
legacyFactor: 5)
public let benchmarks = [
BenchmarkInfo(
name: "PrimsSplit",
runFunction: run_PrimsSplit,
tags: [.validation, .algorithm],
legacyFactor: 5)
]

@inline(never)
public func run_PrimsSplit(_ N: Int) {
for _ in 1...N {
public func run_PrimsSplit(_ n: Int) {
for _ in 1...n {
let nodes : [Int] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
Expand Down Expand Up @@ -547,7 +549,7 @@ public func run_PrimsSplit(_ N: Int) {
}

// Find spanning tree
let treeEdges = Prims(graph, { (start: Int, end: Int) in
let treeEdges = prims(graph, { (start: Int, end: Int) in
return map[Edge(start: start, end: end)]!
})

Expand All @@ -556,6 +558,6 @@ public func run_PrimsSplit(_ N: Int) {
for i in 1..<treeEdges.count {
if let n = treeEdges[i] { cost += map[Edge(start: n, end: i)]! }
}
CheckResults(Int(cost) == 49324)
check(Int(cost) == 49324)
}
}
2 changes: 1 addition & 1 deletion benchmark/scripts/Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public let {name} = [
]

@inline(never)
public func run_{name}(N: Int) {{
public func run_{name}(n: Int) {{
// TODO
}}
37 changes: 19 additions & 18 deletions benchmark/single-source/Ackermann.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 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
Expand All @@ -14,38 +14,39 @@
// for performance measuring.
import TestsUtils

public let Ackermann = BenchmarkInfo(
name: "Ackermann",
runFunction: run_Ackermann,
tags: [.algorithm])
public let benchmarks =
BenchmarkInfo(
name: "Ackermann",
runFunction: run_Ackermann,
tags: [.algorithm])

func ackermann(_ M: Int, _ N : Int) -> Int {
if (M == 0) { return N + 1 }
if (N == 0) { return ackermann(M - 1, 1) }
return ackermann(M - 1, ackermann(M, N - 1))
func _ackermann(_ m: Int, _ n : Int) -> Int {
if (m == 0) { return n + 1 }
if (n == 0) { return _ackermann(m - 1, 1) }
return _ackermann(m - 1, _ackermann(m, n - 1))
}

@inline(never)
func Ackermann(_ M: Int, _ N : Int) -> Int {
func ackermann(_ m: Int, _ n : Int) -> Int {
// This if prevents optimizer from computing return value of Ackermann(3,9)
// at compile time.
if False() { return 0 }
if (M == 0) { return N + 1 }
if (N == 0) { return ackermann(M - 1, 1) }
return ackermann(M - 1, ackermann(M, N - 1))
if getFalse() { return 0 }
if (m == 0) { return n + 1 }
if (n == 0) { return _ackermann(m - 1, 1) }
return _ackermann(m - 1, _ackermann(m, n - 1))
}

let ref_result = [5, 13, 29, 61, 125, 253, 509, 1021, 2045, 4093, 8189, 16381, 32765, 65533, 131069]

@inline(never)
public func run_Ackermann(_ N: Int) {
public func run_Ackermann(_ n: Int) {
let (m, n) = (3, 6)
var result = 0
for _ in 1...N {
result = Ackermann(m, n)
for _ in 1...n {
result = ackermann(m, n)
if result != ref_result[n] {
break
}
}
CheckResults(result == ref_result[n])
check(result == ref_result[n])
}
16 changes: 8 additions & 8 deletions benchmark/single-source/AngryPhonebook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 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
Expand All @@ -16,7 +16,7 @@ import TestsUtils

let t: [BenchmarkCategory] = [.validation, .api, .String]

public let AngryPhonebook = [
public let benchmarks = [
BenchmarkInfo(
name: "AngryPhonebook",
runFunction: run_AngryPhonebook,
Expand Down Expand Up @@ -76,9 +76,9 @@ let words = [
"Nicholas", "Eric", "Stephen", "Jacob", "Larry", "Frank"]

@inline(never)
public func run_AngryPhonebook(_ N: Int) {
public func run_AngryPhonebook(_ n: Int) {
// Permute the names.
for _ in 1...N {
for _ in 1...n {
for firstname in words {
for lastname in words {
_ = (firstname.uppercased(), lastname.lowercased())
Expand Down Expand Up @@ -132,10 +132,10 @@ let longArmenian = phonebook(armenian)
let longCyrillic = phonebook(cyrillic)

@inline(never)
public func angryPhonebook(_ N: Int, _ names: [String]) {
public func angryPhonebook(_ n: Int, _ names: [String]) {
assert(names.count == 20)
// Permute the names.
for _ in 1...N {
for _ in 1...n {
for firstname in names {
for lastname in names {
blackHole((firstname.uppercased(), lastname.lowercased()))
Expand All @@ -145,8 +145,8 @@ public func angryPhonebook(_ N: Int, _ names: [String]) {
}

@inline(never)
public func angryPhonebook(_ N: Int, precomposed names: String) {
for _ in 1...N {
public func angryPhonebook(_ n: Int, precomposed names: String) {
for _ in 1...n {
blackHole((names.uppercased(), names.lowercased()))
}
}
19 changes: 10 additions & 9 deletions benchmark/single-source/AnyHashableWithAClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 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
Expand All @@ -22,12 +22,13 @@ import TestsUtils
// 11% _swift_stdlib_makeAnyHashableUpcastingToHashableBaseType
// 16% _swift_retain_[n]
// 5% swift_conformsToProtocol
public let AnyHashableWithAClass = BenchmarkInfo(
name: "AnyHashableWithAClass",
runFunction: run_AnyHashableWithAClass,
tags: [.abstraction, .runtime, .cpubench],
legacyFactor: 500
)
public let benchmarks =
BenchmarkInfo(
name: "AnyHashableWithAClass",
runFunction: run_AnyHashableWithAClass,
tags: [.abstraction, .runtime, .cpubench],
legacyFactor: 500
)

class TestHashableBase : Hashable {
var value: Int
Expand All @@ -54,9 +55,9 @@ class TestHashableDerived4 : TestHashableDerived3 {}
class TestHashableDerived5 : TestHashableDerived4 {}

@inline(never)
public func run_AnyHashableWithAClass(_ N: Int) {
public func run_AnyHashableWithAClass(_ n: Int) {
let c = TestHashableDerived5(10)
for _ in 0...(N*1000) {
for _ in 0...(n*1000) {
_ = AnyHashable(c)
}
}
Loading