Skip to content

Fix quadratic COW copies in DictionaryOfDictionaries #654

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 1 commit into from
May 12, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,50 +92,48 @@ extension DictionaryOfDictionaries {
}

public subscript(key: Key) -> Value? {
get {outerDict[key.0]?[key.1]}
get { outerDict[key.0]?[key.1] }
set {
if let v = newValue { _ = updateValue(v, forKey: key) }
else { _ = removeValue(forKey: key) }
if let v = newValue {
outerDict[key.0, default: [:]][key.1] = v
} else {
// Remove the inner key from the inner dictionary.
outerDict[key.0]?[key.1] = nil

// If that was the last entry in the inner dictionary,
// remove the entire inner entry.
if outerDict[key.0]?.isEmpty == true {
outerDict[key.0] = nil
}
}
}
}

public subscript(key: OuterKey) -> [InnerKey: Value]? {
get {outerDict[key]}
get { outerDict[key] }
set {
if let v = newValue { _ = outerDict.updateValue(v, forKey: key) }
else { _ = outerDict.removeValue(forKey: key) }
outerDict[key] = newValue
}
}
}

// MARK: - mutating
extension DictionaryOfDictionaries {
mutating func updateValue(_ v: Value, forKey keys : (OuterKey,InnerKey)
mutating func updateValue(_ v: Value, forKey key: Key
) -> Value? {
if var innerDict = outerDict[keys.0] {
let old = innerDict.updateValue(v, forKey: keys.1)
outerDict.updateValue(innerDict, forKey: keys.0)
return old
}
outerDict.updateValue([keys.1: v], forKey: keys.0)
return nil
let old = self[key]
self[key] = v
return old
}

mutating func removeValue(forKey keys : (OuterKey,InnerKey)
mutating func removeValue(forKey key: Key
) -> Value? {
guard var innerDict = outerDict[keys.0]
else { return nil }
let old = innerDict.removeValue(forKey: keys.1)
if innerDict.isEmpty {
outerDict.removeValue(forKey: keys.0)
}
else {
outerDict.updateValue(innerDict, forKey: keys.0)
}
let old = self[key]
self[key] = nil
return old
}
}

// MARK: - identity

extension DictionaryOfDictionaries: Equatable where Value: Equatable {}
extension DictionaryOfDictionaries: Equatable where Value: Equatable {}
2 changes: 0 additions & 2 deletions Sources/SwiftDriver/IncrementalCompilation/TwoDMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public struct TwoDMap<Key1: Hashable, Key2: Hashable, Value: Equatable>: Mutable
public typealias Element = (Key, Value)
public typealias Index = DictionaryOfDictionaries<Key1, Key2, Value>.Index



public init() {}

public subscript(position: Index) -> Element {
Expand Down