Skip to content

Sorted collections #65

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 15 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
87 changes: 87 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/SortedCollections.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SortedCollections"
BuildableName = "SortedCollections"
BlueprintName = "SortedCollections"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
enableASanStackUseAfterReturn = "YES"
codeCoverageEnabled = "YES">
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SortedCollectionsTests"
BuildableName = "SortedCollectionsTests"
BlueprintName = "SortedCollectionsTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableASanStackUseAfterReturn = "YES"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SortedCollections"
BuildableName = "SortedCollections"
BlueprintName = "SortedCollections"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "OrderedCollections"
BuildableName = "OrderedCollections"
BlueprintName = "OrderedCollections"
BlueprintIdentifier = "DequeTests"
BuildableName = "DequeTests"
BlueprintName = "DequeTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -56,9 +56,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "swift-collections-benchmark"
BuildableName = "swift-collections-benchmark"
BlueprintName = "swift-collections-benchmark"
BlueprintIdentifier = "CollectionsTestSupport"
BuildableName = "CollectionsTestSupport"
BlueprintName = "CollectionsTestSupport"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -70,9 +70,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CollectionsTestSupport"
BuildableName = "CollectionsTestSupport"
BlueprintName = "CollectionsTestSupport"
BlueprintIdentifier = "swift-collections-benchmark"
BuildableName = "swift-collections-benchmark"
BlueprintName = "swift-collections-benchmark"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand Down Expand Up @@ -106,29 +106,15 @@
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DequeTests"
BuildableName = "DequeTests"
BlueprintName = "DequeTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CollectionsTestSupportTests"
BuildableName = "CollectionsTestSupportTests"
BlueprintName = "CollectionsTestSupportTests"
BlueprintIdentifier = "OrderedCollections"
BuildableName = "OrderedCollections"
BlueprintName = "OrderedCollections"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand Down
43 changes: 43 additions & 0 deletions Benchmarks/Benchmarks/CppBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ internal class CppPriorityQueue {
}
}

internal class CppMap {
var ptr: UnsafeMutableRawPointer?

init(_ input: [Int]) {
self.ptr = input.withUnsafeBufferPointer { buffer in
cpp_map_create(buffer.baseAddress, buffer.count)
}
}

deinit {
destroy()
}

func destroy() {
if let ptr = ptr {
cpp_map_destroy(ptr)
}
ptr = nil
}
}

extension Benchmark {
public mutating func addCppBenchmarks() {
Expand Down Expand Up @@ -706,5 +726,28 @@ extension Benchmark {
pq.destroy()
}
}

//--------------------------------------------------------------------------

self.addSimple(
title: "std::map<intptr_t, intptr_t> insert",
input: [Int].self
) { input in
input.withUnsafeBufferPointer { buffer in
cpp_map_insert_integers(buffer.baseAddress, buffer.count)
}
}

self.add(
title: "std::map<intptr_t, intptr_t> successful find",
input: ([Int], [Int]).self
) { input, lookups in
let map = CppMap(input)
return { timer in
lookups.withUnsafeBufferPointer { buffer in
cpp_map_lookups(map.ptr, buffer.baseAddress, buffer.count)
}
}
}
}
}
74 changes: 74 additions & 0 deletions Benchmarks/Benchmarks/SortedDictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//

import CollectionsBenchmark

extension Benchmark {
public mutating func addSortedDictionaryBenchmarks() {
// self.add(
// title: "SortedDictionary<Int, Int> init(uniqueKeysWithValues:)",
// input: [Int].self
// ) { input in
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
//
// return { timer in
// blackHole(SortedDictionary(uniqueKeysWithValues: keysAndValues))
// }
// }
//
// self.add(
// title: "SortedDictionary<Int, Int> subscript, append",
// input: [Int].self
// ) { input in
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
// var sortedDictionary = SortedDictionary<Int, Int>()
//
// return { timer in
// for (key, value) in keysAndValues {
// sortedDictionary[key] = value
// }
// blackHole(sortedDictionary)
// }
// }
//
// self.add(
// title: "SortedDictionary<Int, Int> subscript, successful lookups",
// input: ([Int], [Int]).self
// ) { input, lookups in
// let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
// let sortedDictionary = SortedDictionary<Int, Int>(uniqueKeysWithValues: keysAndValues)
//
// return { timer in
// for key in lookups {
// precondition(sortedDictionary._root.contains(key: key))
// }
// }
// }

self.add(
title: "SortedDictionary<Int, Int>._BTree subscript, successful lookups",
input: ([Int], [Int]).self
) { input, lookups in
let keysAndValues = input.lazy.map { (key: $0, value: 2 * $0) }
var tree = _BTree<Int, Int>()

for (k, v) in keysAndValues {
tree.setAnyValue(v, forKey: k)
}

return { timer in
for key in lookups {
precondition(tree.contains(key: key))
}
}
}
}
}
40 changes: 40 additions & 0 deletions Benchmarks/CppBenchmarks/include/MapBenchmarks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//

#ifndef CPPBENCHMARKS_MAP_BENCHMARKS_H
#define CPPBENCHMARKS_MAP_BENCHMARKS_H

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/// Create a std::map, populating it with data from the supplied buffer.
/// Returns an opaque pointer to the created instance.
extern void *cpp_map_create(const intptr_t *start, size_t count);

/// Destroys an ordered map previously returned by `cpp_map_create`.
extern void cpp_map_destroy(void *ptr);

extern void cpp_map_insert_integers(const intptr_t *start, size_t count);

extern void cpp_map_lookups(void *ptr, const intptr_t *start, size_t count);
extern void cpp_map_subscript(void *ptr, const intptr_t *start, size_t count);

#ifdef __cplusplus
}
#endif


#endif /* CPPBENCHMARKS_MAP_BENCHMARKS_H */
1 change: 1 addition & 0 deletions Benchmarks/CppBenchmarks/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module CppBenchmarks {
header "DequeBenchmarks.h"
header "UnorderedSetBenchmarks.h"
header "UnorderedMapBenchmarks.h"
header "MapBenchmarks.h"
header "PriorityQueueBenchmarks.h"
export *
}
Loading