Skip to content

[cxx-interop] Allow initializing std::map from Swift Dictionary #75877

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
Aug 14, 2024
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
14 changes: 14 additions & 0 deletions stdlib/public/Cxx/CxxDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public protocol CxxDictionary<Key, Value> {
}

extension CxxDictionary {
/// Creates a C++ map containing the elements of a Swift Dictionary.
///
/// This initializes the map by copying every key and value of the dictionary.
///
/// - Complexity: O(*n*), where *n* is the number of entries in the Swift
/// dictionary
@inlinable
public init(_ dictionary: Dictionary<Key, Value>) where Key: Hashable {
self.init()
for (key, value) in dictionary {
self[key] = value
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a future performance optimization, we could use __insertUnsafe here to skip the check for the presence of the element in subscript.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to do it in this PR? Are there any concerns about that approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong reasons, but I wanted to add a simple benchmark first (in a different PR) to see if the unsafe APIs actually give us any speedup compared to the subscript.

}
}

@inlinable
public subscript(key: Key) -> Value? {
get {
Expand Down
43 changes: 43 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,49 @@ StdMapTestSuite.test("init") {
expectTrue(m.empty())
}

StdMapTestSuite.test("Map.init(_: Dictionary<Int, Int>)") {
let swiftDict: [Int32 : Int32] = [-1: 2, 2: 3, 33: 44]
let m = Map(swiftDict)
expectEqual(m.size(), 3)

expectEqual(m[-1], 2)
expectEqual(m[2], 3)
expectEqual(m[33], 44)

let emptySwiftDict: [Int32 : Int32] = [:]
let emptyM = Map(emptySwiftDict)
expectEqual(emptyM.size(), 0)
}

/// Same as above, but for std::unordered_map.
StdMapTestSuite.test("UnorderedMap.init(_: Dictionary<Int, Int>)") {
let swiftDict: [Int32 : Int32] = [-1 : 2, 2 : 3, 33 : 44]
let m = UnorderedMap(swiftDict)
expectEqual(m.size(), 3)

expectEqual(m[-1], 2)
expectEqual(m[2], 3)
expectEqual(m[33], 44)

let emptySwiftDict: [Int32 : Int32] = [:]
let emptyM = UnorderedMap(emptySwiftDict)
expectEqual(emptyM.size(), 0)
}

StdMapTestSuite.test("MapStrings.init(_: Dictionary<std.string, std.string>)") {
let swiftDict = [std.string("abc") : std.string("123"),
std.string() : std.string("empty")]
let m = MapStrings(swiftDict)
expectEqual(m.size(), 2)

expectEqual(m[std.string("abc")], std.string("123"))
expectEqual(m[std.string()], std.string("empty"))

let emptySwiftDict: [std.string : std.string] = [:]
let emptyM = MapStrings(emptySwiftDict)
expectEqual(emptyM.size(), 0)
}

StdMapTestSuite.test("Map.subscript") {
// This relies on the `std::map` conformance to `CxxDictionary` protocol.
var m = initMap()
Expand Down