Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ set(SWIFT_BENCH_MODULES
single-source/Hash
single-source/Histogram
single-source/HTTP2StateMachine
single-source/IndexPathTest
single-source/InsertCharacter
single-source/IntegerParsing
single-source/Integrate
Expand Down
163 changes: 163 additions & 0 deletions benchmark/single-source/IndexPathTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//===--- IndexPathTest.swift ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils
import Foundation

let size = 200
let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath]

public let IndexPathTest = [
BenchmarkInfo(name: "IndexPathSubscriptMutation",
runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size)},
tags: tags),
BenchmarkInfo(name: "IndexPathSubscriptRangeMutation",
runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size)},
tags: tags),

BenchmarkInfo(name: "IndexPathMaxBeginning",
runFunction: { n in run_IndexPathMaxBeginning(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMaxMiddle",
runFunction: { n in run_IndexPathMaxMiddle(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMaxEnd",
runFunction: { n in run_IndexPathMaxEnd(n * 1000)},
tags: tags),

BenchmarkInfo(name: "IndexPathMinBeginning",
runFunction: { n in run_IndexPathMinBeginning(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMinMiddle",
runFunction: { n in run_IndexPathMinMiddle(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMinEnd",
runFunction: { n in run_IndexPathMinEnd(n * 1000)},
tags: tags),
]

@inline(__always)
func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath {
let indexes = Array(0..<size)
return IndexPath(indexes: reversed ? indexes.reversed() : indexes)
}

@inline(__always)
func indexPath(_ size: Int, middle: Int) -> IndexPath {
var indexes = Array(0..<size)
indexes.insert(middle, at: (indexes.count - 1) / 2)
return IndexPath(indexes: indexes)
}

// Subscript Mutations

@inline(__always)
func subscriptMutation(
n: Int,
mutations: Int,
mutate: (inout IndexPath, Int) -> ()) {
for _ in 0..<n {
var ip = indexPath(size)

for i in 0..<mutations {
mutate(&ip, i)
}

blackHole(ip)
}
}

@inline(never)
public func run_IndexPathSubscriptMutation(_ n: Int, _ count: Int) {
subscriptMutation(n: n, mutations: count, mutate: { ip, i in
ip[i % 4] += 1
})
}

@inline(never)
public func run_IndexPathSubscriptRangeMutation(_ n: Int, _ count: Int) {
subscriptMutation(n: count, mutations: count, mutate: { ip, i in
ip[0..<i] += [i]
})
}

// Max, Min

@inline(__always)
func maxMin(
n: Int,
creator: () -> IndexPath,
maxMinFunc: (inout IndexPath) -> Int?) {
for _ in 0..<n {
var ip = creator()
let found = maxMinFunc(&ip) != nil
blackHole(found)
}
}

// Max

@inline(never)
public func run_IndexPathMaxBeginning(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, reversed: true)
}, maxMinFunc: { ip in
ip.max()
})
}

@inline(never)
public func run_IndexPathMaxMiddle(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, middle: size + 1)
}, maxMinFunc: { ip in
ip.max()
})
}

@inline(never)
public func run_IndexPathMaxEnd(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size)
}, maxMinFunc: { ip in
ip.max()
})
}

// Min

@inline(never)
public func run_IndexPathMinBeginning(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size)
}, maxMinFunc: { ip in
ip.min()
})
}

@inline(never)
public func run_IndexPathMinMiddle(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, middle: -1)
}, maxMinFunc: { ip in
ip.min()
})
}

@inline(never)
public func run_IndexPathMinEnd(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, reversed: true)
}, maxMinFunc: { ip in
ip.min()
})
}
2 changes: 1 addition & 1 deletion benchmark/utils/TestsUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum BenchmarkCategory : String {
// we know is important to measure.
case validation
// subsystems to validate and their subcategories.
case api, Array, String, Dictionary, Codable, Set, Data
case api, Array, String, Dictionary, Codable, Set, Data, IndexPath
case sdk
case runtime, refcount, metadata
// Other general areas of compiled code validation.
Expand Down
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import Hanoi
import Hash
import Histogram
import HTTP2StateMachine
import IndexPathTest
import InsertCharacter
import IntegerParsing
import Integrate
Expand Down Expand Up @@ -274,6 +275,7 @@ registerBenchmark(Hanoi)
registerBenchmark(HashTest)
registerBenchmark(Histogram)
registerBenchmark(HTTP2StateMachine)
registerBenchmark(IndexPathTest)
registerBenchmark(InsertCharacter)
registerBenchmark(IntegerParsing)
registerBenchmark(IntegrateTest)
Expand Down