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] Replace hashValue implementations with hash(into:) #16157

Merged
merged 1 commit into from
Apr 27, 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.
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 @@ -63,6 +63,7 @@ set(SWIFT_BENCH_MODULES
single-source/DictTest2
single-source/DictTest3
single-source/DictTest4
single-source/DictTest4Legacy
single-source/DictionaryBridge
single-source/DictionaryCopy
single-source/DictionaryGroup
Expand Down
7 changes: 3 additions & 4 deletions benchmark/multi-source/PrimsSplit/Prims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ func ==(lhs: Edge, rhs: Edge) -> Bool {
}

extension Edge : Hashable {
var hashValue: Int {
get {
return start.hashValue ^ end.hashValue
}
func hash(into hasher: inout Hasher) {
hasher.combine(start)
hasher.combine(end)
}
}

Expand Down
6 changes: 4 additions & 2 deletions benchmark/single-source/AnyHashableWithAClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ class TestHashableBase : Hashable {
init(_ value: Int) {
self.value = value
}
var hashValue: Int {
return value

func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func == (
lhs: TestHashableBase,
rhs: TestHashableBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ extension MockBinaryInteger : Comparable {
}

extension MockBinaryInteger : Hashable {
var hashValue: Int {
return _value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(_value)
}
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/DictTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/DictTest2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/single-source/DictTest3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
return lhs.value == rhs.value
}
Expand Down
16 changes: 11 additions & 5 deletions benchmark/single-source/DictTest4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ import TestsUtils
// exercising the default hash compression function.

public let Dictionary4 = [
BenchmarkInfo(name: "Dictionary4", runFunction: run_Dictionary4, tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(name: "Dictionary4OfObjects", runFunction: run_Dictionary4OfObjects, tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(
name: "Dictionary4",
runFunction: run_Dictionary4,
tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(
name: "Dictionary4OfObjects",
runFunction: run_Dictionary4OfObjects,
tags: [.validation, .api, .Dictionary]),
]

struct LargeKey: Hashable {
Expand Down Expand Up @@ -85,10 +91,10 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
return lhs.value == rhs.value
}
Expand Down
158 changes: 158 additions & 0 deletions benchmark/single-source/DictTest4Legacy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//===--- DictTest4.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

// This benchmark mostly measures lookups in dictionaries with complex keys,
// using the legacy hashValue API.

public let Dictionary4Legacy = [
BenchmarkInfo(
name: "Dictionary4Legacy",
runFunction: run_Dictionary4Legacy,
tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(
name: "Dictionary4OfObjectsLegacy",
runFunction: run_Dictionary4OfObjectsLegacy,
tags: [.validation, .api, .Dictionary]),
]

extension Int {
mutating func combine(_ value: Int) {
self = 16777619 &* self ^ value
}
}

struct LargeKey: Hashable {
let i: Int
let j: Int
let k: Double
let l: UInt32
let m: Bool
let n: Bool
let o: Bool
let p: Bool
let q: Bool

var hashValue: Int {
var hash = i.hashValue
hash.combine(j.hashValue)
hash.combine(k.hashValue)
hash.combine(l.hashValue)
hash.combine(m.hashValue)
hash.combine(n.hashValue)
hash.combine(o.hashValue)
hash.combine(p.hashValue)
hash.combine(q.hashValue)
return hash
}

init(_ value: Int) {
self.i = value
self.j = 2 * value
self.k = Double(value) / 7
self.l = UInt32(truncatingIfNeeded: value)
self.m = value & 1 == 0
self.n = value & 2 == 0
self.o = value & 4 == 0
self.p = value & 8 == 0
self.q = value & 16 == 0
}
}

@inline(never)
public func run_Dictionary4Legacy(_ N: Int) {
let size1 = 100
let reps = 20
let ref_result = "1 99 \(reps) \(reps * 99)"
var hash1 = [LargeKey: Int]()
var hash2 = [LargeKey: Int]()
var res = ""

for _ in 1...N {
// Test insertions
hash1 = [:]
for i in 0..<size1 {
hash1[LargeKey(i)] = i
}

hash2 = hash1

// Test lookups & value replacement
for _ in 1..<reps {
for (k, v) in hash1 {
hash2[k] = hash2[k]! + v
}
}

res = "\(hash1[LargeKey(1)]!) \(hash1[LargeKey(99)]!)" +
" \(hash2[LargeKey(1)]!) \(hash2[LargeKey(99)]!)"
if res != ref_result {
break
}
}
CheckResults(res == ref_result)
}

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

init(_ v: T) {
value = v
}

func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

var hashValue: Int {
return value.hashValue
}

static func ==(lhs: Box, rhs: Box) -> Bool {
return lhs.value == rhs.value
}
}

@inline(never)
public func run_Dictionary4OfObjectsLegacy(_ N: Int) {
let size1 = 100
let reps = 20
let ref_result = "1 99 \(reps) \(reps * 99)"
var hash1 = [Box<LargeKey>: Int]()
var hash2 = [Box<LargeKey>: Int]()
var res = ""

for _ in 1...N {
// Test insertions
hash1 = [:]
for i in 0..<size1 {
hash1[Box(LargeKey(i))] = i
}

hash2 = hash1

// Test lookups & value replacement
for _ in 1..<reps {
for (k, v) in hash1 {
hash2[k] = hash2[k]! + v
}
}

res = "\(hash1[Box(LargeKey(1))]!) \(hash1[Box(LargeKey(99))]!)" +
" \(hash2[Box(LargeKey(1))]!) \(hash2[Box(LargeKey(99))]!)"
if res != ref_result {
break
}
}
CheckResults(res == ref_result)
}
4 changes: 2 additions & 2 deletions benchmark/single-source/DictionaryGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/DictionaryRemove.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/DictionarySubscriptDefault.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class Box<T : Hashable> : Hashable, P {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/DictionarySwap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
7 changes: 3 additions & 4 deletions benchmark/single-source/Prims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ func ==(lhs: Edge, rhs: Edge) -> Bool {
}

extension Edge : Hashable {
var hashValue: Int {
get {
return start.hashValue ^ end.hashValue
}
func hash(into hasher: inout Hasher) {
hasher.combine(start)
hasher.combine(end)
}
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/RGBHistogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/single-source/SetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class Box<T : Hashable> : Hashable {
value = v
}

var hashValue: Int {
return value.hashValue
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}

static func ==(lhs: Box, rhs: Box) -> Bool {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/utils/TestsUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ extension BenchmarkInfo : Comparable {
}

extension BenchmarkInfo : Hashable {
public var hashValue: Int {
return name.hashValue
public func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}

Expand Down
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import DictTest
import DictTest2
import DictTest3
import DictTest4
import DictTest4Legacy
import DictionaryBridge
import DictionaryCopy
import DictionaryGroup
Expand Down Expand Up @@ -203,6 +204,7 @@ registerBenchmark(Dictionary)
registerBenchmark(Dictionary2)
registerBenchmark(Dictionary3)
registerBenchmark(Dictionary4)
registerBenchmark(Dictionary4Legacy)
registerBenchmark(DictionaryBridge)
registerBenchmark(DictionaryCopy)
registerBenchmark(DictionaryGroup)
Expand Down