Skip to content

[4.0] Move Dictionary(grouping:by) down a level #10450

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
Jun 23, 2017
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 @@ -40,6 +40,7 @@ set(SWIFT_BENCH_MODULES
single-source/DictTest2
single-source/DictTest3
single-source/DictionaryBridge
single-source/DictionaryGroup
single-source/DictionaryLiteral
single-source/DictionaryRemove
single-source/DictionarySwap
Expand Down
51 changes: 51 additions & 0 deletions benchmark/single-source/DictionaryGroup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===--- DictionaryGroup.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

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 })
CheckResults(dict.count == 10)
CheckResults(dict[0]!.count == result)
}
}

class Box<T : Hashable> : Hashable {
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_DictionaryGroupOfObjects(_ N: Int) {
let objects = (0..<count).map { Box($0) }
for _ in 1...N {
let dict = Dictionary(grouping: objects, by: { Box($0.value % 10) })
CheckResults(dict.count == 10)
CheckResults(dict[Box(0)]!.count == result)
}
}
3 changes: 3 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import DictTest
import DictTest2
import DictTest3
import DictionaryBridge
import DictionaryGroup
import DictionaryLiteral
import DictionaryRemove
import DictionarySwap
Expand Down Expand Up @@ -181,6 +182,8 @@ addTo(&precommitTests, "Dictionary2OfObjects", run_Dictionary2OfObjects)
addTo(&precommitTests, "Dictionary3", run_Dictionary3)
addTo(&precommitTests, "Dictionary3OfObjects", run_Dictionary3OfObjects)
addTo(&precommitTests, "DictionaryBridge", run_DictionaryBridge)
addTo(&precommitTests, "DictionaryGroup", run_DictionaryGroup)
addTo(&precommitTests, "DictionaryGroupOfObjects", run_DictionaryGroupOfObjects)
addTo(&precommitTests, "DictionaryLiteral", run_DictionaryLiteral)
addTo(&precommitTests, "DictionaryOfObjects", run_DictionaryOfObjects)
addTo(&precommitTests, "DictionaryRemove", run_DictionaryRemove)
Expand Down
30 changes: 27 additions & 3 deletions stdlib/public/core/HashedCollections.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1810,9 +1810,7 @@ public struct Dictionary<Key : Hashable, Value> :
by keyForValue: (S.Element) throws -> Key
) rethrows where Value == [S.Element] {
self = [:]
for value in values {
self[try keyForValue(value), default: []].append(value)
}
try _variantBuffer.nativeGroup(values, by: keyForValue)
}

internal init(_nativeBuffer: _NativeDictionaryBuffer<Key, Value>) {
Expand Down Expand Up @@ -5053,6 +5051,32 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
#endif
}
}

internal mutating func nativeGroup<S: Sequence>(
_ values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows where Value == [S.Element] {
defer { _fixLifetime(asNative) }
for value in values {
let key = try keyForValue(value)
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
if found {
asNative.values[i.offset].append(value)
} else {
let minCapacity = NativeBuffer.minimumCapacity(
minimumCount: asNative.count + 1,
maxLoadFactorInverse: _hashContainerDefaultMaxLoadFactorInverse)

let (_, capacityChanged) = ensureUniqueNativeBuffer(minCapacity)
if capacityChanged {
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
}

asNative.initializeKey(key, value: [value], at: i.offset)
asNative.count += 1
}
}
}
%end

/// - parameter idealBucket: The ideal bucket for the element being deleted.
Expand Down