Skip to content

Commit

Permalink
Further reduce instance creations
Browse files Browse the repository at this point in the history
  • Loading branch information
skhugh committed May 31, 2024
1 parent 3fe3a6f commit 857269d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 63 deletions.
2 changes: 1 addition & 1 deletion yorkie/src/main/kotlin/dev/yorkie/api/ElementConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ internal fun CrdtObject.toPBJsonObject(): PBJsonElement {
}
}

internal fun List<ElementRht.Node<CrdtElement>>.toPBRhtNodes(): List<PBRhtNode> {
internal fun Iterable<ElementRht.Node<CrdtElement>>.toPBRhtNodes(): List<PBRhtNode> {
return map {
rHTNode {
key = it.strKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ internal data class ChangeContext(
val root: CrdtRoot,
val message: String? = null,
var presenceChange: PresenceChange? = null,
private val _operations: MutableList<Operation> = mutableListOf(),
private val operations: MutableList<Operation> = mutableListOf(),
private var delimiter: UInt = TimeTicket.INITIAL_DELIMITER,
) {
val operations: List<Operation>
get() = _operations.toList()

/**
* Returns whether this context has change or not.
*/
Expand All @@ -40,7 +37,7 @@ internal data class ChangeContext(
* Pushes the given operation to this context.
*/
fun push(operation: Operation) {
_operations.add(operation)
operations.add(operation)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions yorkie/src/main/kotlin/dev/yorkie/document/crdt/CrdtObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal data class CrdtObject(
override var _removedAt: TimeTicket? = null,
private val rht: ElementRht<CrdtElement> = ElementRht(),
) : CrdtContainer(), Iterable<Pair<String, CrdtElement>> {
val memberNodes
get() = rht.toList()
val memberNodes: Iterable<ElementRht.Node<CrdtElement>>
get() = rht

/**
* Returns the array of keys in this object.
Expand Down
14 changes: 7 additions & 7 deletions yorkie/src/main/kotlin/dev/yorkie/document/crdt/CrdtTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ internal data class CrdtTreeNode private constructor(

var insNextID: CrdtTreeNodeID? = null

val rhtNodes: List<RhtNode>
get() = _attributes.toList()
val rhtNodes: Iterable<RhtNode>
get() = _attributes

init {
_value?.let { value = it }
Expand Down Expand Up @@ -860,13 +860,13 @@ internal data class CrdtTreeNode private constructor(
* Copies itself deeply.
*/
fun deepCopy(): CrdtTreeNode {
val childNodes = mutableListOf<CrdtTreeNode>()
allChildren.forEach {
childNodes.add(it.deepCopy())
}
return copy(
_value = _value,
childNodes = IndexTreeNodeList(
allChildren.map { child ->
child.deepCopy()
}.toMutableList(),
),
childNodes = IndexTreeNodeList(childNodes),
_attributes = _attributes.deepCopy(),
).also {
it.allChildren.forEach { child ->
Expand Down
41 changes: 21 additions & 20 deletions yorkie/src/main/kotlin/dev/yorkie/document/crdt/RgaTreeSplit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ internal class RgaTreeSplit<T : RgaTreeSplitValue<T>> :
val removedNodes = mutableMapOf<RgaTreeSplitNodeID, RgaTreeSplitNode<T>>()

// First, we need to collect indexes for change.
val changes = makeChanges(nodesToKeep, executedAt).toMutableList()
val changes = makeChanges(nodesToKeep, executedAt)
nodesToDelete.forEach { node ->
// Then, make nodes be tombstones and map that.
val actorID = node.createdAt.actorID
Expand Down Expand Up @@ -269,26 +269,27 @@ internal class RgaTreeSplit<T : RgaTreeSplitValue<T>> :
private fun makeChanges(
boundaries: List<RgaTreeSplitNode<T>?>,
executedAt: TimeTicket,
): List<ContentChange> {
return buildList {
var (fromIndex, toIndex) = 0 to 0
for (index in 0 until boundaries.lastIndex) {
val leftBoundary = boundaries[index]
val rightBoundary = boundaries[index + 1]
if (leftBoundary?.next == rightBoundary) continue

fromIndex =
findIndexesFromRange(requireNotNull(leftBoundary?.next).createPosRange()).first
toIndex = if (rightBoundary == null) {
treeByIndex.length
} else {
findIndexesFromRange(requireNotNull(rightBoundary.prev).createPosRange()).second
}
}
if (fromIndex < toIndex) {
add(ContentChange(executedAt.actorID, fromIndex, toIndex))
): MutableList<ContentChange> {
val changes = mutableListOf<ContentChange>()
var (fromIndex, toIndex) = 0 to 0
for (index in 0 until boundaries.lastIndex) {
val leftBoundary = boundaries[index]
val rightBoundary = boundaries[index + 1]
if (leftBoundary?.next == rightBoundary) continue

fromIndex =
findIndexesFromRange(requireNotNull(leftBoundary?.next).createPosRange()).first
toIndex = if (rightBoundary == null) {
treeByIndex.length
} else {
findIndexesFromRange(requireNotNull(rightBoundary.prev).createPosRange()).second
}
}.reversed()
}
if (fromIndex < toIndex) {
changes.add(ContentChange(executedAt.actorID, fromIndex, toIndex))
}
changes.reverse()
return changes
}

fun findIndexesFromRange(range: RgaTreeSplitPosRange): Pair<Int, Int> {
Expand Down
4 changes: 2 additions & 2 deletions yorkie/src/main/kotlin/dev/yorkie/document/crdt/TextInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal data class TextValue(
val attributes
get() = _attributes.nodeKeyValueMap

val attributesWithTimeTicket
get() = _attributes.toList()
val attributesWithTimeTicket: Iterable<RhtNode>
get() = _attributes

override val length: Int by content::length

Expand Down
12 changes: 6 additions & 6 deletions yorkie/src/main/kotlin/dev/yorkie/document/json/JsonTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class JsonTree internal constructor(

val fromPos = target.pathToPos(fromPath)
val toPos = target.pathToPos(toPath)
editInternal(fromPos, toPos, contents.toList(), splitLevel)
editInternal(fromPos, toPos, splitLevel, *contents)
}

/**
Expand Down Expand Up @@ -152,19 +152,19 @@ public class JsonTree internal constructor(

val fromPos = target.findPos(fromIndex)
val toPos = target.findPos(toIndex)
editInternal(fromPos, toPos, contents.toList(), splitLevel)
editInternal(fromPos, toPos, splitLevel, *contents)
}

private fun editInternal(
fromPos: CrdtTreePos,
toPos: CrdtTreePos,
contents: List<TreeNode>,
splitLevel: Int = 0,
vararg contents: TreeNode,
) {
if (contents.isNotEmpty()) {
validateTreeNodes(contents)
validateTreeNodes(*contents)
if (contents.first().type != DEFAULT_TEXT_TYPE) {
contents.forEach { validateTreeNodes(listOf(it)) }
contents.forEach { validateTreeNodes(it) }
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public class JsonTree internal constructor(
/**
* Ensures that treeNodes consists of only one type.
*/
private fun validateTreeNodes(treeNodes: List<TreeNode>) {
private fun validateTreeNodes(vararg treeNodes: TreeNode) {
if (treeNodes.isEmpty()) return

val firstTreeNodeType = treeNodes.first().type
Expand Down
34 changes: 14 additions & 20 deletions yorkie/src/main/kotlin/dev/yorkie/util/IndexTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ internal class IndexTree<T : IndexTreeNode<T>>(val root: T) {
}

private fun addSizeOfLeftSiblings(parent: T, offset: Int): Int {
return parent.children.take(offset).fold(0) { acc, leftSibling ->
return parent.children.subList(0, offset).fold(0) { acc, leftSibling ->
acc + if (leftSibling.isRemoved) 0 else leftSibling.paddedSize
}
}
Expand Down Expand Up @@ -411,7 +411,7 @@ internal abstract class IndexTreeNode<T : IndexTreeNode<T>>() {
* Returns the children of the node including tombstones.
*/
val allChildren: List<T>
get() = childNodes.toList()
get() = childNodes

/**
* Returns true if the node's children consist of only text children.
Expand Down Expand Up @@ -443,7 +443,7 @@ internal abstract class IndexTreeNode<T : IndexTreeNode<T>>() {

// If nodes are removed, the offset of the removed node is the number of
// nodes before the node excluding the removed nodes.
return allChildren.take(index).filterNot { it.isRemoved }.size
return allChildren.subList(0, index).filterNot { it.isRemoved }.size
}
return children.indexOf(node)
}
Expand All @@ -467,20 +467,18 @@ internal abstract class IndexTreeNode<T : IndexTreeNode<T>>() {
}

/**
* Prepends the given nodes to the children.
* Prepends the given node to the children.
*/
fun prepend(vararg newNode: T) {
fun prepend(newNode: T) {
check(!isText) {
"Text node cannot have children"
}

childNodes.addAll(0, newNode.toList())
newNode.forEach { node ->
node.parent = this as T
childNodes.add(0, newNode)
newNode.parent = this as T

if (!node.isRemoved) {
node.updateAncestorSize()
}
if (!newNode.isRemoved) {
newNode.updateAncestorSize()
}
}

Expand Down Expand Up @@ -571,23 +569,19 @@ internal abstract class IndexTreeNode<T : IndexTreeNode<T>>() {
parent?.insertAfterInternal(this as T, clone)
clone.updateAncestorSize()

val leftChildren = childNodes.take(offset)
val rightChildren = childNodes.drop(offset)
childNodes.clear()
childNodes.addAll(leftChildren)
clone.childNodes.clear()
clone.childNodes.addAll(rightChildren)
repeat(childNodes.size - offset) {
val rightChild = childNodes.removeAt(offset + it)
clone.childNodes.add(rightChild)
rightChild.parent = clone
}
size = childNodes.fold(0) { acc, child ->
acc + child.paddedSize
}
clone.size = clone.childNodes.fold(0) { acc, child ->
acc + child.paddedSize
}

clone.childNodes.forEach { child ->
child.parent = clone
}

return clone
}

Expand Down

0 comments on commit 857269d

Please sign in to comment.