From 4159e719501105c114d6286e67c433a5cb5fb3ed Mon Sep 17 00:00:00 2001 From: Jesse Grosjean Date: Fri, 16 Aug 2024 12:40:00 -0400 Subject: [PATCH] Add loro to benchmarks (#196) --- CollectionBenchmarks/Library.json | 4 ++ CollectionBenchmarks/Package.swift | 4 +- .../Sources/CollectionBenchmarks/main.swift | 70 ++++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CollectionBenchmarks/Library.json b/CollectionBenchmarks/Library.json index c02634c6..1e5ae6bd 100644 --- a/CollectionBenchmarks/Library.json +++ b/CollectionBenchmarks/Library.json @@ -8,6 +8,7 @@ "title": "Text - append", "tasks": [ "Text - append", + "Text - append (Loro)", ] }, { @@ -15,6 +16,7 @@ "title": "Text - append and read", "tasks": [ "Text - append and read", + "Text - append and read (Loro)", ] }, { @@ -22,6 +24,7 @@ "title": "List - Integer append", "tasks": [ "List - Integer append", + "List - Integer append (Loro)", ] }, { @@ -29,6 +32,7 @@ "title": "Map - Integer append", "tasks": [ "Map - Integer append", + "Map - Integer append (Loro)", ] }, ] diff --git a/CollectionBenchmarks/Package.swift b/CollectionBenchmarks/Package.swift index 04713b02..75e77dd6 100644 --- a/CollectionBenchmarks/Package.swift +++ b/CollectionBenchmarks/Package.swift @@ -4,9 +4,10 @@ import PackageDescription let package = Package( name: "AutomergeCollectionBenchmarks", - platforms: [.macOS(.v10_15)], + platforms: [.macOS(.v12)], dependencies: [ .package(url: "https://github.com/apple/swift-collections-benchmark", from: "0.0.1"), + .package(url: "https://github.com/loro-dev/loro-swift.git", from: "0.16.2-alpha.3"), .package(path: "../"), ], targets: [ @@ -16,6 +17,7 @@ let package = Package( name: "CollectionBenchmarks", dependencies: [ .product(name: "Automerge", package: "automerge-swift"), + .product(name: "Loro", package: "loro-swift"), .product( name: "CollectionsBenchmark", package: "swift-collections-benchmark" diff --git a/CollectionBenchmarks/Sources/CollectionBenchmarks/main.swift b/CollectionBenchmarks/Sources/CollectionBenchmarks/main.swift index bdc9c7b4..4d1003f7 100644 --- a/CollectionBenchmarks/Sources/CollectionBenchmarks/main.swift +++ b/CollectionBenchmarks/Sources/CollectionBenchmarks/main.swift @@ -1,6 +1,7 @@ -import Automerge import CollectionsBenchmark import Foundation +import Automerge +import Loro // NOTE(heckj): collections-benchmark implementations can be a bit hard to understand // from the opaque inputs and structure of the code. @@ -76,6 +77,23 @@ benchmark.addSimple( blackHole(doc) } +benchmark.addSimple( + title: "Text - append (Loro)", + input: [String].self +) { input in + let doc = LoroDoc() + let text = doc.getText(id: "text") + + var stringLength = text.lenUnicode() + for strChar in input { + _ = try! text.splice(pos: stringLength, len: 0, s: strChar) + stringLength = text.lenUnicode() + } + // precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent + // lengths + blackHole(doc) +} + benchmark.addSimple( title: "Text - append and read", input: [String].self @@ -94,6 +112,25 @@ benchmark.addSimple( blackHole(resultingString) } +benchmark.addSimple( + title: "Text - append and read (Loro)", + input: [String].self +) { input in + let doc = LoroDoc() + let text = doc.getText(id: "text") + + var stringLength = text.lenUnicode() + for strChar in input { + _ = try! text.splice(pos: stringLength, len: 0, s: strChar) + stringLength = text.lenUnicode() + } + + let resultingString = text.toString() + // precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent + // lengths + blackHole(resultingString) +} + benchmark.addSimple( title: "List - Integer append", input: [Int].self @@ -110,6 +147,22 @@ benchmark.addSimple( blackHole(doc) } +benchmark.addSimple( + title: "List - Integer append (Loro)", + input: [Int].self +) { integerInput in + let doc = LoroDoc() + let numList = doc.getList(id: "list") + + for intValue in integerInput { + let listLength = numList.len() + try! numList.insert(pos: listLength, v: intValue) + } + // precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent + // lengths + blackHole(doc) +} + benchmark.addSimple( title: "Map - Integer append", input: [Int].self @@ -125,5 +178,20 @@ benchmark.addSimple( blackHole(doc) } +benchmark.addSimple( + title: "Map - Integer append (Loro)", + input: [Int].self +) { integerInput in + let doc = LoroDoc() + let numberMap = doc.getMap(id: "map") + + for intValue in integerInput { + try! numberMap.insert(key: String(intValue), v: intValue) + } + // precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent + // lengths + blackHole(doc) +} + // Execute the benchmark tool with the above definitions. benchmark.main()