Skip to content

Commit

Permalink
Merge pull request #31 from msteindorfer/capsule-collections
Browse files Browse the repository at this point in the history
Incubate persistent data structures
  • Loading branch information
lorentey authored Sep 7, 2022
2 parents e2f8b7b + ccd11e5 commit de5fad7
Show file tree
Hide file tree
Showing 31 changed files with 4,357 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Benchmarks/Benchmarks/DictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ 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, reserving capacity",
input: [Int].self
Expand Down Expand Up @@ -174,6 +194,25 @@ extension Benchmark {
}
}

self.add(
title: "Dictionary<Int, Int> [COW] subscript, remove existing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
let 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)
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "Dictionary<Int, Int> subscript, remove missing",
input: ([Int], [Int]).self
Expand All @@ -191,6 +230,19 @@ extension Benchmark {
}
}

self.add(
title: "Dictionary<Int, Int> subscript(position:)",
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> defaulted subscript, successful lookups",
input: ([Int], [Int]).self
Expand Down
52 changes: 52 additions & 0 deletions Benchmarks/Benchmarks/OrderedDictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ extension Benchmark {
}
}

self.add(
title: "OrderedDictionary<Int, Int> subscript(position:)",
input: ([Int], [Int]).self
) { input, lookups in
let d = OrderedDictionary(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) })
let indices = lookups.map { d.index(forKey: $0)! }
return { timer in
for i in indices {
blackHole(d.elements[indices[i]]) // uses `elements` random-access collection view
}
}
}

self.add(
title: "OrderedDictionary<Int, Int> subscript, successful lookups",
input: ([Int], [Int]).self
Expand Down Expand Up @@ -176,6 +189,26 @@ 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, reserving capacity",
input: [Int].self
Expand Down Expand Up @@ -206,6 +239,25 @@ extension Benchmark {
}
}

self.add(
title: "OrderedDictionary<Int, Int> [COW] subscript, remove existing",
input: ([Int], [Int]).self
) { input, lookups in
return { timer in
let 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)
}
}
precondition(d.count == input.count)
blackHole(d)
}
}

self.add(
title: "OrderedDictionary<Int, Int> subscript, remove missing",
input: ([Int], [Int]).self
Expand Down
Loading

0 comments on commit de5fad7

Please sign in to comment.