Skip to content

[benchmark] Janitor Duty: Sweep II #22690

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 3 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
93 changes: 32 additions & 61 deletions benchmark/single-source/DictionaryCompactMapValues.swift
Original file line number Diff line number Diff line change
@@ -1,76 +1,47 @@
// Dictionary compact map values benchmark
//===--- DictionaryCompactMapValues.swift ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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

let size = 100
let oddNumbers = stride(from: 1, to: size, by: 2)
let smallOddNumMap: [Int: Int?] =
Dictionary(uniqueKeysWithValues: zip(oddNumbers, oddNumbers))
let compactOddNums: [Int: Int] =
Dictionary(uniqueKeysWithValues: zip(oddNumbers, oddNumbers))
let oddStringMap: [Int: String] = Dictionary(uniqueKeysWithValues:
(1...size).lazy.map { ($0, $0 % 2 == 0 ? "dummy" : "\($0)") })

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

public let DictionaryCompactMapValues = [
BenchmarkInfo(name: "DictionaryCompactMapValuesOfNilValue",
runFunction: run_DictionaryCompactMapValuesOfNilValue,
tags: [.validation, .api, .Dictionary],
runFunction: compactMapValues, tags: t,
setUpFunction: { blackHole(smallOddNumMap); blackHole(compactOddNums)},
legacyFactor: 50),
BenchmarkInfo(name: "DictionaryCompactMapValuesOfCastValue",
runFunction: run_DictionaryCompactMapValuesOfCastValue,
tags: [.validation, .api, .Dictionary],
legacyFactor: 50),
runFunction: compactMapValuesInt, tags: t,
setUpFunction: { blackHole(oddStringMap); blackHole(compactOddNums)},
legacyFactor: 54),
]

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

// Fill Dictionary
for i in 1...size {
if i % 2 == 0 {
dict[i] = nil
} else {
dict[i] = i
}
}
CheckResults(dict.count == size / 2)

var refDict = [Int: Int]()
for i in stride(from: 1, to: 100, by: 2) {
refDict[i] = i
}

var newDict = [Int: Int]()
func compactMapValues(N: Int) {
for _ in 1...20*N {
newDict = dict.compactMapValues({$0})
if newDict != refDict {
break
}
CheckResults(smallOddNumMap.compactMapValues({$0}) == compactOddNums)
}

CheckResults(newDict == refDict)
}

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

// Fill Dictionary
for i in 1...size {
if i % 2 == 0 {
dict[i] = "dummy"
} else {
dict[i] = "\(i)"
}
}

CheckResults(dict.count == size)

var refDict = [Int: Int]()
for i in stride(from: 1, to: 100, by: 2) {
refDict[i] = i
}

var newDict = [Int: Int]()
func compactMapValuesInt(N: Int) {
for _ in 1...20*N {
newDict = dict.compactMapValues(Int.init)
if newDict != refDict {
break
}
CheckResults(oddStringMap.compactMapValues(Int.init) == compactOddNums)
}

CheckResults(newDict == refDict)
}
23 changes: 13 additions & 10 deletions benchmark/single-source/DictionaryGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
import TestsUtils

public let DictionaryGroup = [
BenchmarkInfo(name: "DictionaryGroup", runFunction: run_DictionaryGroup, tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(name: "DictionaryGroupOfObjects", runFunction: run_DictionaryGroupOfObjects, tags: [.validation, .api, .Dictionary],
BenchmarkInfo(name: "DictionaryGroup",
runFunction: run_DictionaryGroup,
tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(name: "DictionaryGroupOfObjects",
runFunction: run_DictionaryGroupOfObjects,
tags: [.validation, .api, .Dictionary],
setUpFunction: { blackHole(inputObjects) },
tearDownFunction: { inputObjects = nil }),
tearDownFunction: { inputObjects = nil },
legacyFactor: 9
),
]

let count = 10_000
let result = count / 10

@inline(never)
public func run_DictionaryGroup(_ N: Int) {
for _ in 1...N {
let dict = Dictionary(grouping: 0..<count, by: { $0 % 10 })
let dict = Dictionary(grouping: 0..<10_000, by: { $0 % 10 })
CheckResults(dict.count == 10)
CheckResults(dict[0]!.count == result)
CheckResults(dict[0]!.count == 1_000)
}
}

Expand All @@ -47,14 +50,14 @@ class Box<T : Hashable> : Hashable {
}
}

var inputObjects: [Box<Int>]! = (0..<count).map { Box($0) }
var inputObjects: [Box<Int>]! = (0..<1_000).lazy.map { Box($0) }

@inline(never)
public func run_DictionaryGroupOfObjects(_ N: Int) {
let objects: [Box<Int>] = inputObjects
for _ in 1...N {
let dict = Dictionary(grouping: objects, by: { Box($0.value % 10) })
CheckResults(dict.count == 10)
CheckResults(dict[Box(0)]!.count == result)
CheckResults(dict[Box(0)]!.count == 100)
}
}