Skip to content

Commit 4159e71

Browse files
Add loro to benchmarks (#196)
1 parent 9548e39 commit 4159e71

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

CollectionBenchmarks/Library.json

+4
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
"title": "Text - append",
99
"tasks": [
1010
"Text - append",
11+
"Text - append (Loro)",
1112
]
1213
},
1314
{
1415
"kind": "chart",
1516
"title": "Text - append and read",
1617
"tasks": [
1718
"Text - append and read",
19+
"Text - append and read (Loro)",
1820
]
1921
},
2022
{
2123
"kind": "chart",
2224
"title": "List - Integer append",
2325
"tasks": [
2426
"List - Integer append",
27+
"List - Integer append (Loro)",
2528
]
2629
},
2730
{
2831
"kind": "chart",
2932
"title": "Map - Integer append",
3033
"tasks": [
3134
"Map - Integer append",
35+
"Map - Integer append (Loro)",
3236
]
3337
},
3438
]

CollectionBenchmarks/Package.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import PackageDescription
44

55
let package = Package(
66
name: "AutomergeCollectionBenchmarks",
7-
platforms: [.macOS(.v10_15)],
7+
platforms: [.macOS(.v12)],
88
dependencies: [
99
.package(url: "https://github.com/apple/swift-collections-benchmark", from: "0.0.1"),
10+
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "0.16.2-alpha.3"),
1011
.package(path: "../"),
1112
],
1213
targets: [
@@ -16,6 +17,7 @@ let package = Package(
1617
name: "CollectionBenchmarks",
1718
dependencies: [
1819
.product(name: "Automerge", package: "automerge-swift"),
20+
.product(name: "Loro", package: "loro-swift"),
1921
.product(
2022
name: "CollectionsBenchmark",
2123
package: "swift-collections-benchmark"

CollectionBenchmarks/Sources/CollectionBenchmarks/main.swift

+69-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import Automerge
21
import CollectionsBenchmark
32
import Foundation
3+
import Automerge
4+
import Loro
45

56
// NOTE(heckj): collections-benchmark implementations can be a bit hard to understand
67
// from the opaque inputs and structure of the code.
@@ -76,6 +77,23 @@ benchmark.addSimple(
7677
blackHole(doc)
7778
}
7879

80+
benchmark.addSimple(
81+
title: "Text - append (Loro)",
82+
input: [String].self
83+
) { input in
84+
let doc = LoroDoc()
85+
let text = doc.getText(id: "text")
86+
87+
var stringLength = text.lenUnicode()
88+
for strChar in input {
89+
_ = try! text.splice(pos: stringLength, len: 0, s: strChar)
90+
stringLength = text.lenUnicode()
91+
}
92+
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
93+
// lengths
94+
blackHole(doc)
95+
}
96+
7997
benchmark.addSimple(
8098
title: "Text - append and read",
8199
input: [String].self
@@ -94,6 +112,25 @@ benchmark.addSimple(
94112
blackHole(resultingString)
95113
}
96114

115+
benchmark.addSimple(
116+
title: "Text - append and read (Loro)",
117+
input: [String].self
118+
) { input in
119+
let doc = LoroDoc()
120+
let text = doc.getText(id: "text")
121+
122+
var stringLength = text.lenUnicode()
123+
for strChar in input {
124+
_ = try! text.splice(pos: stringLength, len: 0, s: strChar)
125+
stringLength = text.lenUnicode()
126+
}
127+
128+
let resultingString = text.toString()
129+
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
130+
// lengths
131+
blackHole(resultingString)
132+
}
133+
97134
benchmark.addSimple(
98135
title: "List - Integer append",
99136
input: [Int].self
@@ -110,6 +147,22 @@ benchmark.addSimple(
110147
blackHole(doc)
111148
}
112149

150+
benchmark.addSimple(
151+
title: "List - Integer append (Loro)",
152+
input: [Int].self
153+
) { integerInput in
154+
let doc = LoroDoc()
155+
let numList = doc.getList(id: "list")
156+
157+
for intValue in integerInput {
158+
let listLength = numList.len()
159+
try! numList.insert(pos: listLength, v: intValue)
160+
}
161+
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
162+
// lengths
163+
blackHole(doc)
164+
}
165+
113166
benchmark.addSimple(
114167
title: "Map - Integer append",
115168
input: [Int].self
@@ -125,5 +178,20 @@ benchmark.addSimple(
125178
blackHole(doc)
126179
}
127180

181+
benchmark.addSimple(
182+
title: "Map - Integer append (Loro)",
183+
input: [Int].self
184+
) { integerInput in
185+
let doc = LoroDoc()
186+
let numberMap = doc.getMap(id: "map")
187+
188+
for intValue in integerInput {
189+
try! numberMap.insert(key: String(intValue), v: intValue)
190+
}
191+
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
192+
// lengths
193+
blackHole(doc)
194+
}
195+
128196
// Execute the benchmark tool with the above definitions.
129197
benchmark.main()

0 commit comments

Comments
 (0)