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

Apply updates from yorkie-js-sdk v0.4.28 #193

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 11 additions & 11 deletions Sources/API/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -983,18 +983,18 @@ extension Converter {

let nodes = pbTreeNodes.compactMap { fromTreeNode($0) }

let root = nodes[nodes.count - 1]
let rootIndex = nodes.count - 1
let root = nodes[rootIndex]

var depthTable: [Int32: CRDTTreeNode] = [
pbTreeNodes[rootIndex].depth: root
]

for index in stride(from: nodes.count - 2, to: -1, by: -1) {
var parent: CRDTTreeNode?
for index2 in index + 1 ..< nodes.count {
if pbTreeNodes[index].depth - 1 == pbTreeNodes[index2].depth {
parent = nodes[index2]
break
}
}

try parent?.prepend(contentsOf: [nodes[index]])
for index in stride(from: rootIndex - 1, to: -1, by: -1) {
let node = nodes[index]
let parent = depthTable[pbTreeNodes[index].depth - 1]
try parent?.prepend(contentsOf: [node])
depthTable[pbTreeNodes[index].depth] = node
}

root.updateDescendantsSize()
Expand Down
73 changes: 68 additions & 5 deletions Tests/Benchmark/DocumentBenchmarkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
@testable import Yorkie

final class DocumentBenchmarkTests: XCTestCase {
func benchmarkTree(_ size: Int) async {
func benchmarkTreeEdit(_ size: Int) async {
let doc = Document(key: "test-doc")

do {
Expand Down Expand Up @@ -130,27 +130,51 @@ final class DocumentBenchmarkTests: XCTestCase {
}
}

func testDocumentTree100() throws {
func benchmarkTreeConvert(_ size: Int) async {
let doc = Document(key: "test-doc")

do {
try await doc.update { root, _ in
var children: [JSONTreeTextNode] = []
for _ in 1 ... size {
children.append(JSONTreeTextNode(value: "a"))
}

root.tree = JSONTree(initialRoot:
JSONTreeElementNode(type: "doc",
children: [JSONTreeElementNode(type: "p", children: children)])
)
}

let root = try await(doc.getRoot().tree as? JSONTree)?.getIndexTree().root
let pbTreeNodes = Converter.toTreeNodes(root)
_ = try Converter.fromTreeNodes(pbTreeNodes)
} catch {
XCTAssert(false, "\(error)")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for conversion failures.

While the benchmark method is well-structured, it silently fails on conversion errors. Consider either:

  1. Propagating the errors for better debugging, or
  2. Adding assertions to verify successful conversion.
 func benchmarkTreeConvert(_ size: Int) async {
     let doc = Document(key: "test-doc")

     do {
         try await doc.update { root, _ in
             var children: [JSONTreeTextNode] = []
             for _ in 1 ... size {
                 children.append(JSONTreeTextNode(value: "a"))
             }

             root.tree = JSONTree(initialRoot:
                 JSONTreeElementNode(type: "doc",
                                     children: [JSONTreeElementNode(type: "p", children: children)])
             )
         }

         let root = try await(doc.getRoot().tree as? JSONTree)?.getIndexTree().root
         let pbTreeNodes = Converter.toTreeNodes(root)
-        _ = try Converter.fromTreeNodes(pbTreeNodes)
+        let convertedTree = try Converter.fromTreeNodes(pbTreeNodes)
+        XCTAssertNotNil(convertedTree, "Tree conversion failed")
     } catch {
-        XCTAssert(false, "\(error)")
+        XCTFail("Benchmark failed with error: \(error)")
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func benchmarkTreeConvert(_ size: Int) async {
let doc = Document(key: "test-doc")
do {
try await doc.update { root, _ in
var children: [JSONTreeTextNode] = []
for _ in 1 ... size {
children.append(JSONTreeTextNode(value: "a"))
}
root.tree = JSONTree(initialRoot:
JSONTreeElementNode(type: "doc",
children: [JSONTreeElementNode(type: "p", children: children)])
)
}
let root = try await(doc.getRoot().tree as? JSONTree)?.getIndexTree().root
let pbTreeNodes = Converter.toTreeNodes(root)
_ = try Converter.fromTreeNodes(pbTreeNodes)
} catch {
XCTAssert(false, "\(error)")
}
}
func benchmarkTreeConvert(_ size: Int) async {
let doc = Document(key: "test-doc")
do {
try await doc.update { root, _ in
var children: [JSONTreeTextNode] = []
for _ in 1 ... size {
children.append(JSONTreeTextNode(value: "a"))
}
root.tree = JSONTree(initialRoot:
JSONTreeElementNode(type: "doc",
children: [JSONTreeElementNode(type: "p", children: children)])
)
}
let root = try await(doc.getRoot().tree as? JSONTree)?.getIndexTree().root
let pbTreeNodes = Converter.toTreeNodes(root)
let convertedTree = try Converter.fromTreeNodes(pbTreeNodes)
XCTAssertNotNil(convertedTree, "Tree conversion failed")
} catch {
XCTFail("Benchmark failed with error: \(error)")
}
}


func testDocumentTreeEdit100() throws {
self.measure {
let exp = expectation(description: "measure")

// Put the code you want to measure the time of here.
Task { @MainActor in
await self.benchmarkTree(100)
await self.benchmarkTreeEdit(100)
exp.fulfill()
}

wait(for: [exp], timeout: 1)
}
}

func testDocumentTree1000() throws {
func testDocumentTreeEdit1000() throws {
self.measure {
let exp = expectation(description: "measure")

// Put the code you want to measure the time of here.
Task { @MainActor in
await self.benchmarkTree(1000)
await self.benchmarkTreeEdit(1000)
exp.fulfill()
}

Expand Down Expand Up @@ -227,6 +251,45 @@ final class DocumentBenchmarkTests: XCTestCase {
wait(for: [exp], timeout: 20)
}
}

func testDocumentTreeConvert10000() throws {
self.measure {
let exp = expectation(description: "measure")

Task {
await self.benchmarkTreeConvert(10000)
exp.fulfill()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add @mainactor annotation for consistency.

Other test methods use @MainActor for their task blocks, but it's missing in the new test methods.

-Task {
+Task { @MainActor in
     await self.benchmarkTreeConvert(10000)
     exp.fulfill()
 }

Also applies to: 272-275, 285-288


wait(for: [exp], timeout: 20)
}
}

func testDocumentTreeConvert20000() throws {
self.measure {
let exp = expectation(description: "measure")

Task {
await self.benchmarkTreeConvert(20000)
exp.fulfill()
}

wait(for: [exp], timeout: 20)
}
}

func testDocumentTreeConvert30000() throws {
self.measure {
let exp = expectation(description: "measure")

Task {
await self.benchmarkTreeConvert(30000)
exp.fulfill()
}

wait(for: [exp], timeout: 20)
}
}
}

extension String {
Expand Down
Loading