Skip to content

[benchmark] Janitor Duty: Sweep III #22721

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 6 commits into from
Feb 20, 2019
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
71 changes: 20 additions & 51 deletions benchmark/single-source/DictionaryRemove.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,18 @@ import TestsUtils

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

let size = 100
let numberMap = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
let boxedNums = (1...size).lazy.map { Box($0) }
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))

public let DictionaryRemove = [
BenchmarkInfo(name: "DictionaryRemove",
runFunction: run_DictionaryRemove, tags: t, legacyFactor: 10),
runFunction: remove, tags: t, legacyFactor: 10),
BenchmarkInfo(name: "DictionaryRemoveOfObjects",
runFunction: run_DictionaryRemoveOfObjects, tags: t, legacyFactor: 100),
runFunction: removeObjects, tags: t, legacyFactor: 100),
]

@inline(never)
public func run_DictionaryRemove(_ N: Int) {
let size = 100
var dict = [Int: Int](minimumCapacity: size)

// Fill dictionary
for i in 1...size {
dict[i] = i
}
CheckResults(dict.count == size)

var tmpDict = dict
for _ in 1...100*N {
tmpDict = dict
// Empty dictionary
for i in 1...size {
tmpDict.removeValue(forKey: i)
}
if !tmpDict.isEmpty {
break
}
}

CheckResults(tmpDict.isEmpty)
}

class Box<T : Hashable> : Hashable {
var value: T

Expand All @@ -65,28 +44,18 @@ class Box<T : Hashable> : Hashable {
}
}

@inline(never)
public func run_DictionaryRemoveOfObjects(_ N: Int) {
let size = 100
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)

// Fill dictionary
for i in 1...size {
dict[Box(i)] = Box(i)
}
CheckResults(dict.count == size)

var tmpDict = dict
for _ in 1...10*N {
tmpDict = dict
// Empty dictionary
for i in 1...size {
tmpDict.removeValue(forKey: Box(i))
}
if !tmpDict.isEmpty {
break
}
}
func remove(N: Int) {
for _ in 1...100*N {
var dict = numberMap
for i in 1...size { dict.removeValue(forKey: i) }
CheckResults(dict.isEmpty)
}
}

CheckResults(tmpDict.isEmpty)
func removeObjects(N: Int) {
for _ in 1...10*N {
var dict = boxedNumMap
for i in 1...size { dict.removeValue(forKey: Box(i)) }
CheckResults(dict.isEmpty)
}
}
140 changes: 45 additions & 95 deletions benchmark/single-source/DictionarySwap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,24 @@
// rdar://problem/19804127
import TestsUtils

let size = 100
let numberMap = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
let boxedNums = (1...size).lazy.map { Box($0) }
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))

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

public let DictionarySwap = [
BenchmarkInfo(name: "DictionarySwap",
runFunction: run_DictionarySwap, tags: t, legacyFactor: 4),
runFunction: swap, tags: t, legacyFactor: 4),
BenchmarkInfo(name: "DictionarySwapOfObjects",
runFunction: run_DictionarySwapOfObjects, tags: t, legacyFactor: 40),
runFunction: swapObjects, tags: t, legacyFactor: 40),
BenchmarkInfo(name: "DictionarySwapAt",
runFunction: run_DictionarySwapAt, tags: t, legacyFactor: 4),
runFunction: swapAt, tags: t, legacyFactor: 4),
BenchmarkInfo(name: "DictionarySwapAtOfObjects",
runFunction: run_DictionarySwapAtOfObjects, tags: t, legacyFactor: 40),
runFunction: swapAtObjects, tags: t, legacyFactor: 11),
]

@inline(never)
public func run_DictionarySwap(_ N: Int) {
let size = 100
var dict = [Int: Int](minimumCapacity: size)

// Fill dictionary
for i in 1...size {
dict[i] = i
}
CheckResults(dict.count == size)

var swapped = false
for _ in 1...2500*N {
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
swapped = !swapped
if !swappedCorrectly(swapped, dict[25]!, dict[75]!) {
break
}
}

CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
}

@inline(never)
public func run_DictionarySwapAt(_ N: Int) {
let size = 100
var dict = [Int: Int](minimumCapacity: size)

// Fill dictionary
for i in 1...size {
dict[i] = i
}
CheckResults(dict.count == size)

var swapped = false
for _ in 1...2500*N {
let i25 = dict.index(forKey: 25)!
let i75 = dict.index(forKey: 75)!

dict.values.swapAt(i25, i75)
swapped = !swapped
if !swappedCorrectly(swapped, dict[25]!, dict[75]!) {
break
}
}

CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
}

// Return true if correctly swapped, false otherwise
func swappedCorrectly(_ swapped: Bool, _ p25: Int, _ p75: Int) -> Bool {
return swapped && (p25 == 75 && p75 == 25) ||
Expand All @@ -98,55 +54,49 @@ class Box<T : Hashable> : Hashable {
}
}

@inline(never)
public func run_DictionarySwapOfObjects(_ N: Int) {
let size = 100
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)

// Fill dictionary
for i in 1...size {
dict[Box(i)] = Box(i)
}
CheckResults(dict.count == size)

var swapped = false
for _ in 1...250*N {
let b1 = Box(25)
let b2 = Box(75)
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
swapped = !swapped
if !swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value) {
break
}
func swap(N: Int) {
var dict = numberMap
var swapped = false
for _ in 1...2500*N {
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
swapped = !swapped
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
}

CheckResults(swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value))
}

@inline(never)
public func run_DictionarySwapAtOfObjects(_ N: Int) {
let size = 100
var dict = [Box<Int>: Box<Int>](minimumCapacity: size)

// Fill dictionary
for i in 1...size {
dict[Box(i)] = Box(i)
}
CheckResults(dict.count == size)

func swapObjects(N: Int) {
var dict = boxedNumMap
var swapped = false
for _ in 1...250*N {
let b25 = Box(25)
let b75 = Box(75)
let i25 = dict.index(forKey: b25)!
let i75 = dict.index(forKey: b75)!
let b1 = Box(25)
let b2 = Box(75)
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
swapped = !swapped
CheckResults(swappedCorrectly(swapped,
dict[Box(25)]!.value, dict[Box(75)]!.value))
}
}

func swapAt(N: Int) {
var dict = numberMap
var swapped = false
for _ in 1...2500*N {
let i25 = dict.index(forKey: 25)!
let i75 = dict.index(forKey: 75)!
dict.values.swapAt(i25, i75)
swapped = !swapped
if !swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value) {
break
}
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
}

CheckResults(swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value))
}

func swapAtObjects(N: Int) {
var dict = boxedNumMap
var swapped = false
for _ in 1...1000*N {
let i25 = dict.index(forKey: Box(25))!
let i75 = dict.index(forKey: Box(75))!
dict.values.swapAt(i25, i75)
swapped = !swapped
CheckResults(swappedCorrectly(swapped,
dict[Box(25)]!.value, dict[Box(75)]!.value))
}}