Skip to content

Commit de5fad7

Browse files
authored
Merge pull request #31 from msteindorfer/capsule-collections
Incubate persistent data structures
2 parents e2f8b7b + ccd11e5 commit de5fad7

31 files changed

+4357
-0
lines changed

Benchmarks/Benchmarks/DictionaryBenchmarks.swift

+52
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ extension Benchmark {
145145
blackHole(d)
146146
}
147147

148+
self.add(
149+
title: "Dictionary<Int, Int> [COW] subscript, insert",
150+
input: ([Int], [Int]).self
151+
) { input, insert in
152+
return { timer in
153+
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
154+
let c = input.count
155+
timer.measure {
156+
for i in insert {
157+
var e = d
158+
e[c + i] = 2 * (c + i)
159+
precondition(e.count == input.count + 1)
160+
blackHole(e)
161+
}
162+
}
163+
precondition(d.count == input.count)
164+
blackHole(d)
165+
}
166+
}
167+
148168
self.addSimple(
149169
title: "Dictionary<Int, Int> subscript, insert, reserving capacity",
150170
input: [Int].self
@@ -174,6 +194,25 @@ extension Benchmark {
174194
}
175195
}
176196

197+
self.add(
198+
title: "Dictionary<Int, Int> [COW] subscript, remove existing",
199+
input: ([Int], [Int]).self
200+
) { input, lookups in
201+
return { timer in
202+
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
203+
timer.measure {
204+
for i in lookups {
205+
var e = d
206+
e[i] = nil
207+
precondition(e.count == input.count - 1)
208+
blackHole(e)
209+
}
210+
}
211+
precondition(d.count == input.count)
212+
blackHole(d)
213+
}
214+
}
215+
177216
self.add(
178217
title: "Dictionary<Int, Int> subscript, remove missing",
179218
input: ([Int], [Int]).self
@@ -191,6 +230,19 @@ extension Benchmark {
191230
}
192231
}
193232

233+
self.add(
234+
title: "Dictionary<Int, Int> subscript(position:)",
235+
input: ([Int], [Int]).self
236+
) { input, lookups in
237+
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
238+
let indices = lookups.map { d.index(forKey: $0)! }
239+
return { timer in
240+
for i in indices {
241+
blackHole(d[i])
242+
}
243+
}
244+
}
245+
194246
self.add(
195247
title: "Dictionary<Int, Int> defaulted subscript, successful lookups",
196248
input: ([Int], [Int]).self

Benchmarks/Benchmarks/OrderedDictionaryBenchmarks.swift

+52
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ extension Benchmark {
8585
}
8686
}
8787

88+
self.add(
89+
title: "OrderedDictionary<Int, Int> subscript(position:)",
90+
input: ([Int], [Int]).self
91+
) { input, lookups in
92+
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
93+
let indices = lookups.map { d.index(forKey: $0)! }
94+
return { timer in
95+
for i in indices {
96+
blackHole(d.elements[indices[i]]) // uses `elements` random-access collection view
97+
}
98+
}
99+
}
100+
88101
self.add(
89102
title: "OrderedDictionary<Int, Int> subscript, successful lookups",
90103
input: ([Int], [Int]).self
@@ -176,6 +189,26 @@ extension Benchmark {
176189
blackHole(d)
177190
}
178191

192+
self.add(
193+
title: "OrderedDictionary<Int, Int> [COW] subscript, append",
194+
input: ([Int], [Int]).self
195+
) { input, insert in
196+
return { timer in
197+
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
198+
let c = input.count
199+
timer.measure {
200+
for i in insert {
201+
var e = d
202+
e[c + i] = 2 * (c + i)
203+
precondition(e.count == input.count + 1)
204+
blackHole(e)
205+
}
206+
}
207+
precondition(d.count == input.count)
208+
blackHole(d)
209+
}
210+
}
211+
179212
self.addSimple(
180213
title: "OrderedDictionary<Int, Int> subscript, append, reserving capacity",
181214
input: [Int].self
@@ -206,6 +239,25 @@ extension Benchmark {
206239
}
207240
}
208241

242+
self.add(
243+
title: "OrderedDictionary<Int, Int> [COW] subscript, remove existing",
244+
input: ([Int], [Int]).self
245+
) { input, lookups in
246+
return { timer in
247+
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
248+
timer.measure {
249+
for i in lookups {
250+
var e = d
251+
e[i] = nil
252+
precondition(e.count == input.count - 1)
253+
blackHole(e)
254+
}
255+
}
256+
precondition(d.count == input.count)
257+
blackHole(d)
258+
}
259+
}
260+
209261
self.add(
210262
title: "OrderedDictionary<Int, Int> subscript, remove missing",
211263
input: ([Int], [Int]).self

0 commit comments

Comments
 (0)