Skip to content

Commit

Permalink
Refactor for loop to for-in to speed
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum committed Jul 17, 2024
1 parent fbbec8e commit 228921a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
6 changes: 2 additions & 4 deletions Sources/Document/CRDT/CRDTTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,7 @@ class CRDTTree: CRDTElement {
let allChildren = realParent.innerChildren
let index = isLeftMost ? 0 : (allChildren.firstIndex(where: { $0 === leftNode }) ?? -1) + 1

for index in index ..< allChildren.count {
let next = allChildren[index]
for next in allChildren.suffix(from: index) {
if !next.id.createdAt.after(editedAt) {
break
}
Expand Down Expand Up @@ -1141,8 +1140,7 @@ class CRDTTree: CRDTElement {
// Generate ranges by accumulating consecutive nodes.
var start: TreeToken<CRDTTreeNode>?
var end: TreeToken<CRDTTreeNode>?
for index in 0 ..< candidates.count {
let cur = candidates[index]
for (index, cur) in candidates.enumerated() {
let next = candidates[safe: index + 1]
if start == nil {
start = cur
Expand Down
17 changes: 5 additions & 12 deletions Sources/Util/IndexTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func addSizeOfLeftSiblings<T: IndexTreeNode>(parent: T, offset: Int) -> Int {

let siblings = parent.children

for index in 0 ..< offset {
let leftSibling = siblings[index]

for leftSibling in siblings.prefix(upTo: offset) {
if leftSibling.isRemoved {
continue
}
Expand Down Expand Up @@ -750,9 +748,7 @@ func findTextPos<T: IndexTreeNode>(node: T, pathElement: Int) throws -> TreePos<
throw YorkieError.unexpected(message: "unacceptable path")
}

for index in 0 ..< node.children.count {
let child = node.children[index]

for child in node.children {
if child.size < pathElement {
pathElement -= child.size
} else {
Expand Down Expand Up @@ -859,14 +855,11 @@ class IndexTree<T: IndexTreeNode> {
}

var node = self.root
for index in 0 ..< path.count - 1 {
let pathElement = path[index]

if node.children[safe: pathElement] == nil {
for pathElement in path.dropLast() {
guard let child = node.children[safe: pathElement] else {
throw YorkieError.unexpected(message: "unacceptable path")
}

node = node.children[pathElement]
node = child
}

if node.hasTextChild {
Expand Down

0 comments on commit 228921a

Please sign in to comment.