Skip to content
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

Persistent collections updates #174

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
70 changes: 44 additions & 26 deletions Benchmarks/Benchmarks/DictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ extension Benchmark {
}
}

self.add(
title: "Dictionary<Int, Int> sequential iteration, indices",
input: [Int].self
) { input in
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
for i in d.indices {
blackHole(d[i])
}
}
}

self.add(
title: "Dictionary<Int, Int> indexing subscript",
input: ([Int], [Int]).self
) { input, lookups in
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let indices = lookups.map { d.index(forKey: $0)! }
return { timer in
for i in indices {
blackHole(d[i])
}
}
}

self.add(
title: "Dictionary<Int, Int> subscript, successful lookups",
input: ([Int], [Int]).self
Expand Down Expand Up @@ -134,7 +159,7 @@ extension Benchmark {
}

self.addSimple(
title: "Dictionary<Int, Int> subscript, insert",
title: "Dictionary<Int, Int> subscript, insert, unique",
input: [Int].self
) { input in
var d: [Int: Int] = [:]
Expand All @@ -145,24 +170,18 @@ extension Benchmark {
blackHole(d)
}

self.add(
title: "Dictionary<Int, Int> [COW] subscript, insert",
input: ([Int], [Int]).self
) { input, insert in
return { timer in
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
timer.measure {
for i in insert {
var e = d
e[c + i] = 2 * (c + i)
precondition(e.count == input.count + 1)
blackHole(e)
}
}
precondition(d.count == input.count)
blackHole(d)
self.addSimple(
title: "Dictionary<Int, Int> subscript, insert, shared",
input: [Int].self
) { input in
var d: [Int: Int] = [:]
for i in input {
let copy = d
d[i] = 2 * i
blackHole((copy, d))
}
precondition(d.count == input.count)
blackHole(d)
}

self.addSimple(
Expand All @@ -179,7 +198,7 @@ extension Benchmark {
}

self.add(
title: "Dictionary<Int, Int> subscript, remove existing",
title: "Dictionary<Int, Int> subscript, remove existing, unique",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
Expand All @@ -195,20 +214,19 @@ extension Benchmark {
}

self.add(
title: "Dictionary<Int, Int> [COW] subscript, remove existing",
title: "Dictionary<Int, Int> subscript, remove existing, shared",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
let d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
var d = Dictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
var e = d
e[i] = nil
precondition(e.count == input.count - 1)
blackHole(e)
let copy = d
d[i] = nil
blackHole((copy, d))
}
}
precondition(d.count == input.count)
precondition(d.isEmpty)
blackHole(d)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Benchmarks/Benchmarks/Library.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@
"tasks": [
"Dictionary<Int, Int> sequential iteration",
"Dictionary<Int, Int>.Keys sequential iteration",
"Dictionary<Int, Int>.Values sequential iteration"
"Dictionary<Int, Int>.Values sequential iteration",
"Dictionary<Int, Int> sequential iteration, indices"
]
},
{
Expand Down
59 changes: 32 additions & 27 deletions Benchmarks/Benchmarks/OrderedDictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,19 @@ extension Benchmark {
}

self.add(
title: "OrderedDictionary<Int, Int> subscript(position:)",
title: "OrderedDictionary<Int, Int> sequential iteration, indices",
input: [Int].self
) { input in
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
for i in d.elements.indices {
blackHole(d.elements[i])
}
}
}

self.add(
title: "OrderedDictionary<Int, Int> indexing subscript",
input: ([Int], [Int]).self
) { input, lookups in
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
Expand Down Expand Up @@ -178,7 +190,7 @@ extension Benchmark {
}

self.addSimple(
title: "OrderedDictionary<Int, Int> subscript, append",
title: "OrderedDictionary<Int, Int> subscript, append, unique",
input: [Int].self
) { input in
var d: OrderedDictionary<Int, Int> = [:]
Expand All @@ -189,24 +201,18 @@ extension Benchmark {
blackHole(d)
}

self.add(
title: "OrderedDictionary<Int, Int> [COW] subscript, append",
input: ([Int], [Int]).self
) { input, insert in
return { timer in
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
timer.measure {
for i in insert {
var e = d
e[c + i] = 2 * (c + i)
precondition(e.count == input.count + 1)
blackHole(e)
}
}
precondition(d.count == input.count)
blackHole(d)
self.addSimple(
title: "OrderedDictionary<Int, Int> subscript, append, shared",
input: [Int].self
) { input in
var d: OrderedDictionary<Int, Int> = [:]
for i in input {
let copy = d
d[i] = 2 * i
blackHole((copy, d))
}
precondition(d.count == input.count)
blackHole(d)
}

self.addSimple(
Expand All @@ -223,7 +229,7 @@ extension Benchmark {
}

self.add(
title: "OrderedDictionary<Int, Int> subscript, remove existing",
title: "OrderedDictionary<Int, Int> subscript, remove existing, unique",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
Expand All @@ -240,20 +246,19 @@ extension Benchmark {
}

self.add(
title: "OrderedDictionary<Int, Int> [COW] subscript, remove existing",
title: "OrderedDictionary<Int, Int> subscript, remove existing, shared",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
var d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
var e = d
e[i] = nil
precondition(e.count == input.count - 1)
blackHole(e)
let copy = d
d[i] = nil
blackHole((copy, d))
}
}
precondition(d.count == input.count)
precondition(d.isEmpty)
blackHole(d)
}
}
Expand Down
60 changes: 32 additions & 28 deletions Benchmarks/Benchmarks/PersistentDictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,19 @@ extension Benchmark {
}

self.add(
title: "PersistentDictionary<Int, Int> subscript(position:)",
title: "PersistentDictionary<Int, Int> sequential iteration, indices",
input: [Int].self
) { input in
let d = PersistentDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
return { timer in
for i in d.indices {
blackHole(d[i])
}
}
}

self.add(
title: "PersistentDictionary<Int, Int> indexing subscript",
input: ([Int], [Int]).self
) { input, lookups in
let d = PersistentDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
Expand Down Expand Up @@ -148,7 +160,7 @@ extension Benchmark {
}

self.addSimple(
title: "PersistentDictionary<Int, Int> subscript, insert",
title: "PersistentDictionary<Int, Int> subscript, insert, unique",
input: [Int].self
) { input in
var d: PersistentDictionary<Int, Int> = [:]
Expand All @@ -159,28 +171,22 @@ extension Benchmark {
blackHole(d)
}

self.add(
title: "PersistentDictionary<Int, Int> [COW] subscript, insert",
input: ([Int], [Int]).self
) { input, insert in
return { timer in
let d = PersistentDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let c = input.count
timer.measure {
for i in insert {
var e = d
e[c + i] = 2 * (c + i)
precondition(e.count == input.count + 1)
blackHole(e)
}
}
precondition(d.count == input.count)
blackHole(d)
self.addSimple(
title: "PersistentDictionary<Int, Int> subscript, insert, shared",
input: [Int].self
) { input in
var d: PersistentDictionary<Int, Int> = [:]
for i in input {
let copy = d
d[i] = 2 * i
blackHole((copy, d))
}
precondition(d.count == input.count)
blackHole(d)
}

self.add(
title: "PersistentDictionary<Int, Int> subscript, remove existing",
title: "PersistentDictionary<Int, Int> subscript, remove existing, unique",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
Expand All @@ -196,20 +202,19 @@ extension Benchmark {
}

self.add(
title: "PersistentDictionary<Int, Int> [COW] subscript, remove existing",
title: "PersistentDictionary<Int, Int> subscript, remove existing, shared",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
let d = PersistentDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
var d = PersistentDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
timer.measure {
for i in lookups {
var e = d
e[i] = nil
precondition(e.count == input.count - 1)
blackHole(e)
let copy = d
d[i] = nil
blackHole((copy, d))
}
}
precondition(d.count == input.count)
precondition(d.isEmpty)
blackHole(d)
}
}
Expand Down Expand Up @@ -377,6 +382,5 @@ extension Benchmark {
blackHole(d)
}
}

}
}
Loading