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
[stdlib] Use addressor for Dictionary's subscript(_:default:) #12752
Merged
lorentey
merged 6 commits into
apple:master
from
hamishknight:dictionary-subscript-addressor
Dec 4, 2017
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79d8209
[stdlib] Use addressor for Dictionary's subscript(_:default:)
hamishknight f7015c2
[stdlib] Added benchmarks for Dictionary's subscript(_:default:)
hamishknight 76ea3a2
[stdlib] Fix uniqueness check in nativePointerToValue(forKey:insertin…
hamishknight 12416f9
[stdlib] Don't use autoclosure on pointerToValue(forKey:insertingDefa…
hamishknight 4b3f037
[stdlib] Don't check for native when known native in ensureNativeBuff…
hamishknight 7876391
[stdlib] Call nativePointerToValue(at:) from nativePointerToValue(for…
hamishknight File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
benchmark/single-source/DictionarySubscriptDefault.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//===--- DictionarySubscriptDefault.swift ---------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2017 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 | ||
|
||
public let DictionarySubscriptDefault = [ | ||
BenchmarkInfo(name: "DictionarySubscriptDefaultMutation", | ||
runFunction: run_DictionarySubscriptDefaultMutation, | ||
tags: [.validation, .api, .Dictionary]), | ||
BenchmarkInfo(name: "DictionarySubscriptDefaultMutationArray", | ||
runFunction: run_DictionarySubscriptDefaultMutationArray, | ||
tags: [.validation, .api, .Dictionary]), | ||
BenchmarkInfo(name: "DictionarySubscriptDefaultMutationOfObjects", | ||
runFunction: run_DictionarySubscriptDefaultMutationOfObjects, | ||
tags: [.validation, .api, .Dictionary]), | ||
BenchmarkInfo(name: "DictionarySubscriptDefaultMutationArrayOfObjects", | ||
runFunction: | ||
run_DictionarySubscriptDefaultMutationArrayOfObjects, | ||
tags: [.validation, .api, .Dictionary]), | ||
] | ||
|
||
let count = 10_000 | ||
let result = count / 100 | ||
|
||
@inline(never) | ||
public func run_DictionarySubscriptDefaultMutation(_ N: Int) { | ||
for _ in 1...N { | ||
|
||
var dict = [Int: Int]() | ||
|
||
for i in 0..<count { | ||
dict[i % 100, default: 0] += 1 | ||
} | ||
|
||
CheckResults(dict.count == 100) | ||
CheckResults(dict[0]! == result) | ||
} | ||
} | ||
|
||
@inline(never) | ||
public func run_DictionarySubscriptDefaultMutationArray(_ N: Int) { | ||
for _ in 1...N { | ||
|
||
var dict = [Int: [Int]]() | ||
|
||
for i in 0..<count { | ||
dict[i % 100, default: []].append(i) | ||
} | ||
|
||
CheckResults(dict.count == 100) | ||
CheckResults(dict[0]!.count == result) | ||
} | ||
} | ||
|
||
// Hack to workaround the fact that if we attempt to increment the Box's value | ||
// from the subscript, the compiler will just call the subscript's getter (and | ||
// therefore not insert the instance) as it's dealing with a reference type. | ||
// By using a mutating method in a protocol extension, the compiler is forced to | ||
// treat this an actual mutation, so cannot call the getter. | ||
protocol P { | ||
associatedtype T | ||
var value: T { get set } | ||
} | ||
|
||
extension P { | ||
mutating func mutateValue(_ mutations: (inout T) -> Void) { | ||
mutations(&value) | ||
} | ||
} | ||
|
||
class Box<T : Hashable> : Hashable, P { | ||
var value: T | ||
|
||
init(_ v: T) { | ||
value = v | ||
} | ||
|
||
var hashValue: Int { | ||
return value.hashValue | ||
} | ||
|
||
static func ==(lhs: Box, rhs: Box) -> Bool { | ||
return lhs.value == rhs.value | ||
} | ||
} | ||
|
||
@inline(never) | ||
public func run_DictionarySubscriptDefaultMutationOfObjects(_ N: Int) { | ||
for _ in 1...N { | ||
|
||
var dict = [Box<Int>: Box<Int>]() | ||
|
||
for i in 0..<count { | ||
dict[Box(i % 100), default: Box(0)].mutateValue { $0 += 1 } | ||
} | ||
|
||
CheckResults(dict.count == 100) | ||
CheckResults(dict[Box(0)]!.value == result) | ||
} | ||
} | ||
|
||
@inline(never) | ||
public func run_DictionarySubscriptDefaultMutationArrayOfObjects(_ N: Int) { | ||
for _ in 1...N { | ||
|
||
var dict = [Box<Int>: [Box<Int>]]() | ||
|
||
for i in 0..<count { | ||
dict[Box(i % 100), default: []].append(Box(i)) | ||
} | ||
|
||
CheckResults(dict.count == 100) | ||
CheckResults(dict[Box(0)]!.count == result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right place to define this, and is there an already existing mock COW wrapper I could use here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's perfectly fine to add this here!