Skip to content

Commit

Permalink
[Capsule] Copy DictionaryBenchmark and add some Dictionary API
Browse files Browse the repository at this point in the history
  • Loading branch information
msteindorfer committed Jun 4, 2021
1 parent e8dbd8d commit 37e957f
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 5 deletions.
343 changes: 343 additions & 0 deletions Benchmarks/Benchmarks/CapsuleHashMapBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 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
//
//===----------------------------------------------------------------------===//

import CollectionsBenchmark
import Capsule

extension Benchmark {
public mutating func addCapsuleHashMapBenchmarks() {
self.add(
title: "HashMap<Int, Int> init(uniqueKeysWithValues:)",
input: [Int].self
) { input in
let keysAndValues = input.map { ($0, 2 * $0) }
return { timer in
blackHole(HashMap(uniqueKeysWithValues: keysAndValues))
}
}

self.add(
title: "HashMap<Int, Int> sequential iteration",
input: [Int].self
) { input in
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
for item in d {
blackHole(item)
}
}
}

// self.add(
// title: "HashMap<Int, Int>.Keys sequential iteration",
// input: [Int].self
// ) { input in
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
// return { timer in
// for item in d.keys {
// blackHole(item)
// }
// }
// }
//
// self.add(
// title: "HashMap<Int, Int>.Values sequential iteration",
// input: [Int].self
// ) { input in
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
// return { timer in
// for item in d.values {
// blackHole(item)
// }
// }
// }

self.add(
title: "HashMap<Int, Int> subscript, successful lookups",
input: ([Int], [Int]).self
) { input, lookups in
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
for i in lookups {
precondition(d[i] == 2 * i)
}
}
}

self.add(
title: "HashMap<Int, Int> subscript, unsuccessful lookups",
input: ([Int], [Int]).self
) { input, lookups in
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
return { timer in
for i in lookups {
precondition(d[i + c] == nil)
}
}
}

self.add(
title: "HashMap<Int, Int> subscript, noop setter",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
timer.measure {
for i in lookups {
d[i + c] = nil
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> subscript, set existing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
d[i] = 0
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> subscript, _modify",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
d[i]! *= 2
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.addSimple(
title: "HashMap<Int, Int> subscript, insert",
input: [Int].self
) { input in
var d: [Int: Int] = [:]
for i in input {
d[i] = 2 * i
}
precondition(d.count == input.count)
blackHole(d)
}

self.addSimple(
title: "HashMap<Int, Int> subscript, insert, reserving capacity",
input: [Int].self
) { input in
var d: [Int: Int] = [:]
d.reserveCapacity(input.count)
for i in input {
d[i] = 2 * i
}
precondition(d.count == input.count)
blackHole(d)
}

self.add(
title: "HashMap<Int, Int> subscript, remove existing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
d[i] = nil
}
}
precondition(d.isEmpty)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> subscript, remove missing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
timer.measure {
for i in lookups {
d[i + c] = nil
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> defaulted subscript, successful lookups",
input: ([Int], [Int]).self
) { input, lookups in
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
for i in lookups {
precondition(d[i, default: -1] != -1)
}
}
}

self.add(
title: "HashMap<Int, Int> defaulted subscript, unsuccessful lookups",
input: ([Int], [Int]).self
) { input, lookups in
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
let c = d.count
for i in lookups {
precondition(d[i + c, default: -1] == -1)
}
}
}

self.add(
title: "HashMap<Int, Int> defaulted subscript, _modify existing",
input: [Int].self
) { input in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in input {
d[i, default: -1] *= 2
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> defaulted subscript, _modify missing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
timer.measure {
for i in lookups {
d[c + i, default: -1] *= 2
}
}
precondition(d.count == 2 * input.count)
blackHole(d)
}
}

// self.add(
// title: "HashMap<Int, Int> successful index(forKey:)",
// input: ([Int], [Int]).self
// ) { input, lookups in
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
// return { timer in
// for i in lookups {
// precondition(d.index(forKey: i) != nil)
// }
// }
// }
//
// self.add(
// title: "HashMap<Int, Int> unsuccessful index(forKey:)",
// input: ([Int], [Int]).self
// ) { input, lookups in
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
// return { timer in
// for i in lookups {
// precondition(d.index(forKey: lookups.count + i) == nil)
// }
// }
// }

self.add(
title: "HashMap<Int, Int> updateValue(_:forKey:), existing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
d.updateValue(0, forKey: i)
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> updateValue(_:forKey:), insert",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
d.updateValue(0, forKey: input.count + i)
}
}
precondition(d.count == 2 * input.count)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> random removals (existing keys)",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
precondition(d.removeValue(forKey: i) != nil)
}
}
precondition(d.count == 0)
blackHole(d)
}
}

self.add(
title: "HashMap<Int, Int> random removals (missing keys)",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
let c = input.count
var d = HashMap(uniqueKeysWithValues: input.lazy.map { (c + $0, 2 * $0) })
timer.measure {
for i in lookups {
precondition(d.removeValue(forKey: i) == nil)
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

}
}
1 change: 1 addition & 0 deletions Benchmarks/swift-collections-benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var benchmark = Benchmark(title: "Collection Benchmarks")
benchmark.addArrayBenchmarks()
benchmark.addSetBenchmarks()
benchmark.addDictionaryBenchmarks()
benchmark.addCapsuleHashMapBenchmarks()
benchmark.addDequeBenchmarks()
benchmark.addOrderedSetBenchmarks()
benchmark.addOrderedDictionaryBenchmarks()
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ let package = Package(
.library(name: "Collections", targets: ["Collections"]),
.library(name: "DequeModule", targets: ["DequeModule"]),
.library(name: "OrderedCollections", targets: ["OrderedCollections"]),
.library(name: "Capsule", targets: ["Capsule"]),
],
dependencies: [
// This is only used in the benchmark executable target.
Expand All @@ -65,6 +66,7 @@ let package = Package(
dependencies: [
"DequeModule",
"OrderedCollections",
"Capsule",
],
path: "Sources/Collections",
exclude: ["CMakeLists.txt"],
Expand Down
Loading

0 comments on commit 37e957f

Please sign in to comment.