Skip to content

Commit

Permalink
Ensure find performs splay (#1017)
Browse files Browse the repository at this point in the history
Enhance splay tree efficiency by incorporating splay operations after node
location in Find and IndexOf functions. This change maintains the tree's
self-balancing property and ensures amortized O(log n) time complexity for
future operations. Simplify IndexOf() by leveraging the splay operation,
reducing additional traversal logic and improving overall performance.
  • Loading branch information
m4ushold authored and hackerwins committed Sep 30, 2024
1 parent 77e24f8 commit fcf6fbf
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 11 deletions.
14 changes: 3 additions & 11 deletions pkg/splay/splay.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,8 @@ func (t *Tree[V]) IndexOf(node *Node[V]) int {
return -1
}

index := 0
current := node
var prev *Node[V]
for current != nil {
if prev == nil || prev == current.right {
index += current.value.Len() + current.leftWeight()
}
prev = current
current = current.parent
}
return index - node.value.Len()
t.Splay(node)
return t.root.leftWeight()
}

// Find returns the Node and offset of the given index.
Expand All @@ -209,6 +200,7 @@ func (t *Tree[V]) Find(index int) (*Node[V], int, error) {
return nil, 0, fmt.Errorf("node length %d, index %d: %w", node.value.Len(), offset, ErrOutOfIndex)
}

t.Splay(node)
return node, offset, nil
}

Expand Down
163 changes: 163 additions & 0 deletions test/bench/splay_tree_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bench

import (
"crypto/rand"
gojson "encoding/json"
"fmt"
"io"
"math/big"
"os"
"testing"

"github.com/yorkie-team/yorkie/pkg/splay"
)

type stringValue struct {
content string
removed bool
}

func newSplayNode(content string) *splay.Node[*stringValue] {
return splay.NewNode(&stringValue{
content: content,
})
}

func (v *stringValue) Len() int {
if v.removed {
return 0
}
return len(v.content)
}

func (v *stringValue) String() string {
return v.content
}

type editingTrace struct {
Edits [][]interface{} `json:"edits"`
FinalText string `json:"finalText"`
}

func BenchmarkSplayTree(b *testing.B) {
for _, cnt := range []int{100000, 200000, 300000} {
b.Run(fmt.Sprintf("stress test %d", cnt), func(b *testing.B) {
// find, insert, delete
tree := splay.NewTree[*stringValue](nil)
treeSize := 1
for i := 0; i < cnt; i++ {
maxVal := big.NewInt(3)
operation, err := rand.Int(rand.Reader, maxVal)
if err != nil {
b.Fatal(err)
}

if int(operation.Int64()) == 0 {
tree.Insert(newSplayNode("A"))
treeSize++
} else if int(operation.Int64()) == 1 {
maxVal = big.NewInt(int64(treeSize))
index, err := rand.Int(rand.Reader, maxVal)
if err != nil {
b.Fatal(err)
}
_, _, _ = tree.Find(int(index.Int64()))
} else {
maxVal = big.NewInt(int64(treeSize))
index, err := rand.Int(rand.Reader, maxVal)
if err != nil {
b.Fatal(err)
}
node, _, _ := tree.Find(int(index.Int64()))
if node != nil {
tree.Delete(node)
treeSize--
}
}
}
})
}

for _, cnt := range []int{100000, 200000, 300000} {
b.Run(fmt.Sprintf("random access %d", cnt), func(_ *testing.B) {
// Create a skewed tree by inserting characters only at the very end.
b.StopTimer()
tree := splay.NewTree[*stringValue](nil)
for i := 0; i < cnt; i++ {
tree.Insert(newSplayNode("A"))
}
b.StartTimer()

// 1000 times random access
for i := 0; i < 1000; i++ {
maxVal := big.NewInt(int64(cnt))
index, err := rand.Int(rand.Reader, maxVal)
if err != nil {
b.Fatal(err)
}
_, _, _ = tree.Find(int(index.Int64()))
}
})
}

b.Run("editing trace bench", func(b *testing.B) {
b.StopTimer()

var editingTrace editingTrace

file, err := os.Open("./editing-trace.json")
if err != nil {
b.Fatal(err)
}
defer func() {
if err = file.Close(); err != nil {
b.Fatal(err)
}
}()

byteValue, err := io.ReadAll(file)
if err != nil {
b.Fatal(err)
}

if err = gojson.Unmarshal(byteValue, &editingTrace); err != nil {
b.Fatal(err)
}

b.StartTimer()
tree := splay.NewTree[*stringValue](nil)
for _, edit := range editingTrace.Edits {
cursor := int(edit[0].(float64))
mode := int(edit[1].(float64))

if mode == 0 {
strValue, ok := edit[2].(string)
node, _, err := tree.Find(cursor)
if ok && err != nil && node != nil {
tree.InsertAfter(node, newSplayNode(strValue))
}
} else {
node, _, err := tree.Find(cursor)
if err != nil && node != nil {
tree.Delete(node)
}
}
}
})
}

0 comments on commit fcf6fbf

Please sign in to comment.