Skip to content

Commit 9cf2cda

Browse files
committed
Initial commit for SortedCollections
Signed-off-by: Vihan Bhargava <[email protected]>
1 parent cc69069 commit 9cf2cda

40 files changed

+4011
-33
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1250"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "SortedCollections"
18+
BuildableName = "SortedCollections"
19+
BlueprintName = "SortedCollections"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
enableASanStackUseAfterReturn = "YES"
31+
codeCoverageEnabled = "YES">
32+
<AdditionalOptions>
33+
<AdditionalOption
34+
key = "NSZombieEnabled"
35+
value = "YES"
36+
isEnabled = "YES">
37+
</AdditionalOption>
38+
</AdditionalOptions>
39+
<Testables>
40+
<TestableReference
41+
skipped = "NO">
42+
<BuildableReference
43+
BuildableIdentifier = "primary"
44+
BlueprintIdentifier = "SortedCollectionsTests"
45+
BuildableName = "SortedCollectionsTests"
46+
BlueprintName = "SortedCollectionsTests"
47+
ReferencedContainer = "container:">
48+
</BuildableReference>
49+
</TestableReference>
50+
</Testables>
51+
</TestAction>
52+
<LaunchAction
53+
buildConfiguration = "Debug"
54+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
55+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
56+
enableASanStackUseAfterReturn = "YES"
57+
launchStyle = "0"
58+
useCustomWorkingDirectory = "NO"
59+
ignoresPersistentStateOnLaunch = "NO"
60+
debugDocumentVersioning = "YES"
61+
debugServiceExtension = "internal"
62+
allowLocationSimulation = "YES">
63+
</LaunchAction>
64+
<ProfileAction
65+
buildConfiguration = "Release"
66+
shouldUseLaunchSchemeArgsEnv = "YES"
67+
savedToolIdentifier = ""
68+
useCustomWorkingDirectory = "NO"
69+
debugDocumentVersioning = "YES">
70+
<MacroExpansion>
71+
<BuildableReference
72+
BuildableIdentifier = "primary"
73+
BlueprintIdentifier = "SortedCollections"
74+
BuildableName = "SortedCollections"
75+
BlueprintName = "SortedCollections"
76+
ReferencedContainer = "container:">
77+
</BuildableReference>
78+
</MacroExpansion>
79+
</ProfileAction>
80+
<AnalyzeAction
81+
buildConfiguration = "Debug">
82+
</AnalyzeAction>
83+
<ArchiveAction
84+
buildConfiguration = "Release"
85+
revealArchiveInOrganizer = "YES">
86+
</ArchiveAction>
87+
</Scheme>

.swiftpm/xcode/xcshareddata/xcschemes/swift-collections-Package.xcscheme

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
<BuildActionEntry
3838
buildForTesting = "YES"
3939
buildForRunning = "YES"
40-
buildForProfiling = "YES"
41-
buildForArchiving = "YES"
40+
buildForProfiling = "NO"
41+
buildForArchiving = "NO"
4242
buildForAnalyzing = "YES">
4343
<BuildableReference
4444
BuildableIdentifier = "primary"
45-
BlueprintIdentifier = "OrderedCollections"
46-
BuildableName = "OrderedCollections"
47-
BlueprintName = "OrderedCollections"
45+
BlueprintIdentifier = "DequeTests"
46+
BuildableName = "DequeTests"
47+
BlueprintName = "DequeTests"
4848
ReferencedContainer = "container:">
4949
</BuildableReference>
5050
</BuildActionEntry>
@@ -56,9 +56,9 @@
5656
buildForAnalyzing = "YES">
5757
<BuildableReference
5858
BuildableIdentifier = "primary"
59-
BlueprintIdentifier = "swift-collections-benchmark"
60-
BuildableName = "swift-collections-benchmark"
61-
BlueprintName = "swift-collections-benchmark"
59+
BlueprintIdentifier = "CollectionsTestSupport"
60+
BuildableName = "CollectionsTestSupport"
61+
BlueprintName = "CollectionsTestSupport"
6262
ReferencedContainer = "container:">
6363
</BuildableReference>
6464
</BuildActionEntry>
@@ -70,9 +70,9 @@
7070
buildForAnalyzing = "YES">
7171
<BuildableReference
7272
BuildableIdentifier = "primary"
73-
BlueprintIdentifier = "CollectionsTestSupport"
74-
BuildableName = "CollectionsTestSupport"
75-
BlueprintName = "CollectionsTestSupport"
73+
BlueprintIdentifier = "swift-collections-benchmark"
74+
BuildableName = "swift-collections-benchmark"
75+
BlueprintName = "swift-collections-benchmark"
7676
ReferencedContainer = "container:">
7777
</BuildableReference>
7878
</BuildActionEntry>
@@ -106,29 +106,15 @@
106106
</BuildActionEntry>
107107
<BuildActionEntry
108108
buildForTesting = "YES"
109-
buildForRunning = "NO"
110-
buildForProfiling = "NO"
111-
buildForArchiving = "NO"
112-
buildForAnalyzing = "NO">
113-
<BuildableReference
114-
BuildableIdentifier = "primary"
115-
BlueprintIdentifier = "DequeTests"
116-
BuildableName = "DequeTests"
117-
BlueprintName = "DequeTests"
118-
ReferencedContainer = "container:">
119-
</BuildableReference>
120-
</BuildActionEntry>
121-
<BuildActionEntry
122-
buildForTesting = "YES"
123-
buildForRunning = "NO"
124-
buildForProfiling = "NO"
125-
buildForArchiving = "NO"
126-
buildForAnalyzing = "NO">
109+
buildForRunning = "YES"
110+
buildForProfiling = "YES"
111+
buildForArchiving = "YES"
112+
buildForAnalyzing = "YES">
127113
<BuildableReference
128114
BuildableIdentifier = "primary"
129-
BlueprintIdentifier = "CollectionsTestSupportTests"
130-
BuildableName = "CollectionsTestSupportTests"
131-
BlueprintName = "CollectionsTestSupportTests"
115+
BlueprintIdentifier = "OrderedCollections"
116+
BuildableName = "OrderedCollections"
117+
BlueprintName = "OrderedCollections"
132118
ReferencedContainer = "container:">
133119
</BuildableReference>
134120
</BuildActionEntry>

Benchmarks/Benchmarks/CppBenchmarks.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ internal class CppPriorityQueue {
139139
}
140140
}
141141

142+
internal class CppMap {
143+
var ptr: UnsafeMutableRawPointer?
144+
145+
init(_ input: [Int]) {
146+
self.ptr = input.withUnsafeBufferPointer { buffer in
147+
cpp_map_create(buffer.baseAddress, buffer.count)
148+
}
149+
}
150+
151+
deinit {
152+
destroy()
153+
}
154+
155+
func destroy() {
156+
if let ptr = ptr {
157+
cpp_map_destroy(ptr)
158+
}
159+
ptr = nil
160+
}
161+
}
142162

143163
extension Benchmark {
144164
public mutating func addCppBenchmarks() {
@@ -706,5 +726,28 @@ extension Benchmark {
706726
pq.destroy()
707727
}
708728
}
729+
730+
//--------------------------------------------------------------------------
731+
732+
// self.addSimple(
733+
// title: "std::map<intptr_t, intptr_t> insert",
734+
// input: [Int].self
735+
// ) { input in
736+
// input.withUnsafeBufferPointer { buffer in
737+
// cpp_map_insert_integers(buffer.baseAddress, buffer.count)
738+
// }
739+
// }
740+
741+
self.add(
742+
title: "std::map<intptr_t, intptr_t> successful find",
743+
input: ([Int], [Int]).self
744+
) { input, lookups in
745+
let map = CppMap(input)
746+
return { timer in
747+
lookups.withUnsafeBufferPointer { buffer in
748+
cpp_map_lookups(map.ptr, buffer.baseAddress, buffer.count, true)
749+
}
750+
}
751+
}
709752
}
710753
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Collections open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import CollectionsBenchmark
13+
14+
extension Benchmark {
15+
public mutating func addSortedDictionaryBenchmarks() {
16+
// self.add(
17+
// title: "SortedDictionary<Int, Int> init(uniqueKeysWithValues:)",
18+
// input: [Int].self
19+
// ) { input in
20+
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
21+
//
22+
// return { timer in
23+
// blackHole(SortedDictionary(uniqueKeysWithValues: keysAndValues))
24+
// }
25+
// }
26+
//
27+
// self.add(
28+
// title: "SortedDictionary<Int, Int> subscript, append",
29+
// input: [Int].self
30+
// ) { input in
31+
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
32+
// var sortedDictionary = SortedDictionary<Int, Int>()
33+
//
34+
// return { timer in
35+
// for (key, value) in keysAndValues {
36+
// sortedDictionary[key] = value
37+
// }
38+
// blackHole(sortedDictionary)
39+
// }
40+
// }
41+
42+
self.add(
43+
title: "SortedDictionary<Int, Int> subscript, successful lookups",
44+
input: [Int].self
45+
) { input in
46+
let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
47+
let sortedDictionary = SortedDictionary<Int, Int>(uniqueKeysWithValues: keysAndValues)
48+
49+
return { timer in
50+
for (key, value) in keysAndValues {
51+
precondition(sortedDictionary[key] == value)
52+
}
53+
}
54+
}
55+
56+
// self.add(
57+
// title: "SortedDictionary<Int, Int>._BTree firstValue",
58+
// input: [Int].self
59+
// ) { input in
60+
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
61+
// var tree = _BTree<Int, Int>()
62+
//
63+
// for (key, value) in keysAndValues {
64+
// tree.insertOrUpdate((key, value))
65+
// }
66+
//
67+
// return { timer in
68+
// for (key, value) in keysAndValues {
69+
// precondition(tree.anyValue(for: key) != nil)
70+
// }
71+
// }
72+
// }
73+
74+
// self.add(
75+
// title: "SortedDictionary<Int, Int>._BTree insertOrUpdate(element:)",
76+
// input: [Int].self
77+
// ) { input in
78+
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
79+
//
80+
// return { timer in
81+
// var tree = _BTree<Int, Int>()
82+
// for (key, value) in keysAndValues {
83+
// tree.insertOrUpdate((key, value))
84+
// }
85+
// blackHole(tree)
86+
// }
87+
// }
88+
}
89+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Collections open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef CPPBENCHMARKS_MAP_BENCHMARKS_H
13+
#define CPPBENCHMARKS_MAP_BENCHMARKS_H
14+
15+
#include <stdint.h>
16+
#include <stddef.h>
17+
#include <stdbool.h>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
/// Create a std::map, populating it with data from the supplied buffer.
24+
/// Returns an opaque pointer to the created instance.
25+
extern void *cpp_map_create(const intptr_t *start, size_t count);
26+
27+
/// Destroys an ordered map previously returned by `cpp_map_create`.
28+
extern void cpp_map_destroy(void *ptr);
29+
30+
extern void cpp_map_insert_integers(const intptr_t *start, size_t count);
31+
32+
extern void cpp_map_lookups(void *ptr, const intptr_t *start, size_t count, bool expectMatch);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
39+
#endif /* CPPBENCHMARKS_MAP_BENCHMARKS_H */

Benchmarks/CppBenchmarks/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module CppBenchmarks {
55
header "DequeBenchmarks.h"
66
header "UnorderedSetBenchmarks.h"
77
header "UnorderedMapBenchmarks.h"
8+
header "MapBenchmarks.h"
89
header "PriorityQueueBenchmarks.h"
910
export *
1011
}

0 commit comments

Comments
 (0)