Skip to content
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

[benchmark] Extract Setup from Benchmarks #20048

Merged
merged 16 commits into from Nov 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
106 changes: 66 additions & 40 deletions benchmark/single-source/ArrayAppend.swift
Expand Up @@ -14,31 +14,53 @@

import TestsUtils

let t: [BenchmarkCategory] = [.validation, .api, .Array]
public let ArrayAppend = [
BenchmarkInfo(name: "ArrayAppend", runFunction: run_ArrayAppend, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendArrayOfInt", runFunction: run_ArrayAppendArrayOfInt, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendAscii", runFunction: run_ArrayAppendAscii, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendAsciiSubstring", runFunction: run_ArrayAppendAsciiSubstring, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendFromGeneric", runFunction: run_ArrayAppendFromGeneric, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendGenericStructs", runFunction: run_ArrayAppendGenericStructs, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendLatin1", runFunction: run_ArrayAppendLatin1, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendLatin1Substring", runFunction: run_ArrayAppendLatin1Substring, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendLazyMap", runFunction: run_ArrayAppendLazyMap, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendOptionals", runFunction: run_ArrayAppendOptionals, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendRepeatCol", runFunction: run_ArrayAppendRepeatCol, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendReserved", runFunction: run_ArrayAppendReserved, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendSequence", runFunction: run_ArrayAppendSequence, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendStrings", runFunction: run_ArrayAppendStrings, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendToFromGeneric", runFunction: run_ArrayAppendToFromGeneric, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendToGeneric", runFunction: run_ArrayAppendToGeneric, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendUTF16", runFunction: run_ArrayAppendUTF16, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppendUTF16Substring", runFunction: run_ArrayAppendUTF16Substring, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayPlusEqualArrayOfInt", runFunction: run_ArrayPlusEqualArrayOfInt, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayPlusEqualFiveElementCollection", runFunction: run_ArrayPlusEqualFiveElementCollection, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayPlusEqualSingleElementCollection", runFunction: run_ArrayPlusEqualSingleElementCollection, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayPlusEqualThreeElements", runFunction: run_ArrayPlusEqualThreeElements, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ArrayAppend", runFunction: run_ArrayAppend, tags: t),
BenchmarkInfo(name: "ArrayAppendArrayOfInt", runFunction: run_ArrayAppendArrayOfInt, tags: t,
setUpFunction: ones, tearDownFunction: releaseOnes),
BenchmarkInfo(name: "ArrayAppendAscii", runFunction: run_ArrayAppendAscii, tags: t),
BenchmarkInfo(name: "ArrayAppendAsciiSubstring", runFunction: run_ArrayAppendAsciiSubstring, tags: t),
BenchmarkInfo(name: "ArrayAppendFromGeneric", runFunction: run_ArrayAppendFromGeneric, tags: t,
setUpFunction: ones, tearDownFunction: releaseOnes),
BenchmarkInfo(name: "ArrayAppendGenericStructs", runFunction: run_ArrayAppendGenericStructs, tags: t,
setUpFunction: { otherStructs = Array(repeating: S(x: 3, y: 4.2), count: 10_000) },
tearDownFunction: { otherStructs = nil } ),
BenchmarkInfo(name: "ArrayAppendLatin1", runFunction: run_ArrayAppendLatin1, tags: t),
BenchmarkInfo(name: "ArrayAppendLatin1Substring", runFunction: run_ArrayAppendLatin1Substring, tags: t),
BenchmarkInfo(name: "ArrayAppendLazyMap", runFunction: run_ArrayAppendLazyMap, tags: t,
setUpFunction: { blackHole(array) }),
BenchmarkInfo(name: "ArrayAppendOptionals", runFunction: run_ArrayAppendOptionals, tags: t,
setUpFunction: { otherOptionalOnes = Array(repeating: 1, count: 10_000) },
tearDownFunction: { otherOptionalOnes = nil } ),
BenchmarkInfo(name: "ArrayAppendRepeatCol", runFunction: run_ArrayAppendRepeatCol, tags: t),
BenchmarkInfo(name: "ArrayAppendReserved", runFunction: run_ArrayAppendReserved, tags: t),
BenchmarkInfo(name: "ArrayAppendSequence", runFunction: run_ArrayAppendSequence, tags: t),
BenchmarkInfo(name: "ArrayAppendStrings", runFunction: run_ArrayAppendStrings, tags: t,
setUpFunction: { otherStrings = stride(from: 0, to: 10_000, by: 1).map { "\($0)" } },
tearDownFunction: { otherStrings = nil } ),
BenchmarkInfo(name: "ArrayAppendToFromGeneric", runFunction: run_ArrayAppendToFromGeneric, tags: t,
setUpFunction: ones, tearDownFunction: releaseOnes),
BenchmarkInfo(name: "ArrayAppendToGeneric", runFunction: run_ArrayAppendToGeneric, tags: t,
setUpFunction: ones, tearDownFunction: releaseOnes),
BenchmarkInfo(name: "ArrayAppendUTF16", runFunction: run_ArrayAppendUTF16, tags: t),
BenchmarkInfo(name: "ArrayAppendUTF16Substring", runFunction: run_ArrayAppendUTF16Substring, tags: t),
BenchmarkInfo(name: "ArrayPlusEqualArrayOfInt", runFunction: run_ArrayPlusEqualArrayOfInt, tags: t,
setUpFunction: ones, tearDownFunction: releaseOnes),
BenchmarkInfo(name: "ArrayPlusEqualFiveElementCollection", runFunction: run_ArrayPlusEqualFiveElementCollection, tags: t),
BenchmarkInfo(name: "ArrayPlusEqualSingleElementCollection", runFunction: run_ArrayPlusEqualSingleElementCollection, tags: t),
BenchmarkInfo(name: "ArrayPlusEqualThreeElements", runFunction: run_ArrayPlusEqualThreeElements, tags: t),
]

var otherOnes: [Int]!
var otherOptionalOnes: [Int?]!
var otherStrings: [String]!
var otherStructs: [S<Int, Double>]!
let array = Array(0..<10_000)

func ones() { otherOnes = Array(repeating: 1, count: 10_000) }
func releaseOnes() { otherOnes = nil }

// Append single element
@inline(never)
public func run_ArrayAppend(_ N: Int) {
Expand Down Expand Up @@ -67,10 +89,11 @@ public func run_ArrayAppendReserved(_ N: Int) {
}

// Append a sequence. Length of sequence unknown so
// can't pre-reserve capacity.
// can't pre-reserve capacity.
@inline(never)
public func run_ArrayAppendSequence(_ N: Int) {
let seq = stride(from: 0, to: 10_000, by: 1)

for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
Expand All @@ -85,7 +108,8 @@ public func run_ArrayAppendSequence(_ N: Int) {
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendArrayOfInt(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
let other: [Int] = otherOnes

for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
Expand All @@ -100,7 +124,8 @@ public func run_ArrayAppendArrayOfInt(_ N: Int) {
// except +=, to check equally performant.
@inline(never)
public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
let other: [Int] = otherOnes

for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
Expand All @@ -115,7 +140,8 @@ public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendStrings(_ N: Int) {
let other = stride(from: 0, to: 10_000, by: 1).map { "\($0)" }
let other: [String] = otherStrings

for _ in 0..<N {
for _ in 0..<10 {
var nums = [String]()
Expand All @@ -136,10 +162,11 @@ struct S<T,U> {
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendGenericStructs(_ N: Int) {
let other = Array(repeating: S(x: 3, y: 4.2), count: 10_000)
let other: [S<Int, Double>] = otherStructs

for _ in 0..<N {
for _ in 0..<10 {
var nums = [S<Int,Double>]()
var nums = [S<Int, Double>]()
for _ in 0..<8 {
nums += other
}
Expand All @@ -151,7 +178,7 @@ public func run_ArrayAppendGenericStructs(_ N: Int) {
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendOptionals(_ N: Int) {
let other: [Int?] = Array(repeating: 1, count: 10_000)
let other: [Int?] = otherOptionalOnes

for _ in 0..<N {
for _ in 0..<10 {
Expand All @@ -168,7 +195,7 @@ public func run_ArrayAppendOptionals(_ N: Int) {
// can pre-reserve capacity, but no optimization points used.
@inline(never)
public func run_ArrayAppendLazyMap(_ N: Int) {
let other = Array(0..<10_000).lazy.map { $0 * 2 }
let other = array.lazy.map { $0 * 2 }

for _ in 0..<N {
for _ in 0..<10 {
Expand Down Expand Up @@ -208,7 +235,7 @@ public func appendFromGeneric<

@inline(never)
public func run_ArrayAppendFromGeneric(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
let other: [Int] = otherOnes

for _ in 0..<N {
for _ in 0..<10 {
Expand All @@ -230,7 +257,7 @@ public func appendToGeneric<

@inline(never)
public func run_ArrayAppendToGeneric(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
let other: [Int] = otherOnes

for _ in 0..<N {
for _ in 0..<10 {
Expand All @@ -247,14 +274,14 @@ public func run_ArrayAppendToGeneric(_ N: Int) {
@inline(never)
public func appendToFromGeneric<
R: RangeReplaceableCollection, S: Sequence
>(collection: inout R, sequence: S)
>(collection: inout R, sequence: S)
where R.Element == S.Element {
collection.append(contentsOf: sequence)
}

@inline(never)
public func run_ArrayAppendToFromGeneric(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
let other: [Int] = otherOnes

for _ in 0..<N {
for _ in 0..<10 {
Expand Down Expand Up @@ -319,7 +346,7 @@ public func run_ArrayAppendAscii(_ N: Int) {
}
}

// Append the utf8 elements of an ascii string to a [UInt8]
// Append the utf8 elements of a latin1 string to a [UInt8]
@inline(never)
public func run_ArrayAppendLatin1(_ N: Int) {
let s = "the quick brown fox jumps over the lazy dog\u{00A1}"
Expand All @@ -333,7 +360,7 @@ public func run_ArrayAppendLatin1(_ N: Int) {
}
}

// Append the utf8 elements of an ascii string to a [UInt8]
// Append the utf8 elements of an utf16 string to a [UInt8]
@inline(never)
public func run_ArrayAppendUTF16(_ N: Int) {
let s = "the quick brown 🦊 jumps over the lazy dog"
Expand All @@ -347,7 +374,7 @@ public func run_ArrayAppendUTF16(_ N: Int) {
}
}

// Append the utf8 elements of an ascii string to a [UInt8]
// Append the utf8 elements of an ascii substring to a [UInt8]
@inline(never)
public func run_ArrayAppendAsciiSubstring(_ N: Int) {
let s = "the quick brown fox jumps over the lazy dog!"[...]
Expand All @@ -361,7 +388,7 @@ public func run_ArrayAppendAsciiSubstring(_ N: Int) {
}
}

// Append the utf8 elements of an ascii string to a [UInt8]
// Append the utf8 elements of a latin1 substring to a [UInt8]
@inline(never)
public func run_ArrayAppendLatin1Substring(_ N: Int) {
let s = "the quick brown fox jumps over the lazy dog\u{00A1}"[...]
Expand All @@ -375,7 +402,7 @@ public func run_ArrayAppendLatin1Substring(_ N: Int) {
}
}

// Append the utf8 elements of an ascii string to a [UInt8]
// Append the utf8 elements of an utf16 substring to a [UInt8]
@inline(never)
public func run_ArrayAppendUTF16Substring(_ N: Int) {
let s = "the quick brown 🦊 jumps over the lazy dog"[...]
Expand All @@ -388,4 +415,3 @@ public func run_ArrayAppendUTF16Substring(_ N: Int) {
}
}
}

13 changes: 6 additions & 7 deletions benchmark/single-source/ArrayInClass.swift
Expand Up @@ -14,7 +14,11 @@ import TestsUtils
public let ArrayInClass = BenchmarkInfo(
name: "ArrayInClass",
runFunction: run_ArrayInClass,
tags: [.validation, .api, .Array])
tags: [.validation, .api, .Array],
setUpFunction: { ac = ArrayContainer() },
tearDownFunction: { ac = nil })

var ac: ArrayContainer!

class ArrayContainer {
final var arr : [Int]
Expand All @@ -32,13 +36,8 @@ class ArrayContainer {
}
}

@inline(never)
func getArrayContainer() -> ArrayContainer {
return ArrayContainer()
}

@inline(never)
public func run_ArrayInClass(_ N: Int) {
let a = getArrayContainer()
let a = ac!
a.runLoop(N)
}