Skip to content

Commit

Permalink
Revise the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed May 29, 2023
1 parent db07bc5 commit 3594fb9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pkg/document/crdt/rga_tree_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ type RGATreeSplit[V RGATreeSplitValue] struct {
treeByIndex *splay.Tree[*RGATreeSplitNode[V]]
treeByID *llrb.Tree[*RGATreeSplitNodeID, *RGATreeSplitNode[V]]

// when the edit operation is executed.
// removedNodeMap is a map to store removed nodes. It is used to
// delete the node physically when the garbage collection is executed.
removedNodeMap map[string]*RGATreeSplitNode[V]
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,19 @@ func TestDocument(t *testing.T) {
text.StructureAsString(),
)

from, _, _ := text.CreateRange(0, 0)
from, _ := text.CreateRange(0, 0)
assert.Equal(t, "0:0:00:0:0", from.StructureAsString())

from, _, _ = text.CreateRange(1, 1)
from, _ = text.CreateRange(1, 1)
assert.Equal(t, "1:2:00:0:1", from.StructureAsString())

from, _, _ = text.CreateRange(2, 2)
from, _ = text.CreateRange(2, 2)
assert.Equal(t, "1:3:00:0:1", from.StructureAsString())

from, _, _ = text.CreateRange(3, 3)
from, _ = text.CreateRange(3, 3)
assert.Equal(t, "1:3:00:0:2", from.StructureAsString())

from, _, _ = text.CreateRange(4, 4)
from, _ = text.CreateRange(4, 4)
assert.Equal(t, "1:2:00:3:1", from.StructureAsString())
return nil
})
Expand Down
9 changes: 9 additions & 0 deletions pkg/document/json/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func NewText(ctx *change.Context, text *crdt.Text) *Text {
}
}

// CreateRange creates a range from the given positions.
func (p *Text) CreateRange(from, to int) (*crdt.RGATreeSplitNodePos, *crdt.RGATreeSplitNodePos) {
fromPos, toPos, err := p.Text.CreateRange(from, to)
if err != nil {
panic(err)
}
return fromPos, toPos
}

// Edit edits the given range with the given content and attributes.
func (p *Text) Edit(from, to int, content string, attributes ...map[string]string) *Text {
if from > to {
Expand Down
5 changes: 4 additions & 1 deletion pkg/splay/splay.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"strings"
)

// ErrOutOfIndex is returned when the given index is out of index.
var ErrOutOfIndex = fmt.Errorf("out of index")

// Value represents the data stored in the nodes of Tree.
type Value interface {
Len() int
Expand Down Expand Up @@ -203,7 +206,7 @@ func (t *Tree[V]) Find(index int) (*Node[V], int, error) {
}

if offset > node.value.Len() {
return nil, 0, fmt.Errorf("out of bound of text index: node.length %d, pos %d", node.value.Len(), offset)
return nil, 0, fmt.Errorf("node length %d, index %d: %w", node.value.Len(), offset, ErrOutOfIndex)
}

return node, offset, nil
Expand Down
12 changes: 8 additions & 4 deletions pkg/splay/splay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func TestSplayTree(t *testing.T) {
t.Run("insert and splay test", func(t *testing.T) {
tree := splay.NewTree[*stringValue](nil)

node, idx, _ := tree.Find(0)
node, idx, err := tree.Find(0)
assert.Nil(t, node)
assert.NoError(t, err)
assert.Equal(t, 0, idx)

nodeA := tree.Insert(newSplayNode("A2"))
Expand All @@ -71,17 +72,20 @@ func TestSplayTree(t *testing.T) {
assert.Equal(t, 5, tree.IndexOf(nodeC))
assert.Equal(t, 9, tree.IndexOf(nodeD))

node, offset, _ := tree.Find(1)
node, offset, err := tree.Find(1)
assert.Equal(t, nodeA, node)
assert.Equal(t, 1, offset)
assert.NoError(t, err)

node, offset, _ = tree.Find(7)
node, offset, err = tree.Find(7)
assert.Equal(t, nodeC, node)
assert.Equal(t, 2, offset)
assert.NoError(t, err)

node, offset, _ = tree.Find(11)
node, offset, err = tree.Find(11)
assert.Equal(t, nodeD, node)
assert.Equal(t, 2, offset)
assert.NoError(t, err)
})

t.Run("deletion test", func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions test/bench/document_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@ func BenchmarkDocument(b *testing.B) {
text.StructureAsString(),
)

from, _, _ := text.CreateRange(0, 0)
from, _ := text.CreateRange(0, 0)
assert.Equal(b, "0:0:00:0:0", from.StructureAsString())

from, _, _ = text.CreateRange(1, 1)
from, _ = text.CreateRange(1, 1)
assert.Equal(b, "1:2:00:0:1", from.StructureAsString())

from, _, _ = text.CreateRange(2, 2)
from, _ = text.CreateRange(2, 2)
assert.Equal(b, "1:3:00:0:1", from.StructureAsString())

from, _, _ = text.CreateRange(3, 3)
from, _ = text.CreateRange(3, 3)
assert.Equal(b, "1:3:00:0:2", from.StructureAsString())

from, _, _ = text.CreateRange(4, 4)
from, _ = text.CreateRange(4, 4)
assert.Equal(b, "1:2:00:3:1", from.StructureAsString())
return nil
})
Expand Down

1 comment on commit 3594fb9

@github-actions
Copy link

Choose a reason for hiding this comment

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

Go Benchmark

Benchmark suite Current: 3594fb9 Previous: db07bc5 Ratio
BenchmarkDocument/constructor_test 1240 ns/op 752 B/op 12 allocs/op 1258 ns/op 752 B/op 12 allocs/op 0.99
BenchmarkDocument/status_test 633.8 ns/op 720 B/op 10 allocs/op 646.5 ns/op 720 B/op 10 allocs/op 0.98
BenchmarkDocument/equals_test 7370 ns/op 5072 B/op 85 allocs/op 7152 ns/op 5072 B/op 85 allocs/op 1.03
BenchmarkDocument/nested_update_test 20710 ns/op 11033 B/op 235 allocs/op 26789 ns/op 11033 B/op 235 allocs/op 0.77
BenchmarkDocument/delete_test 27317 ns/op 14162 B/op 310 allocs/op 26619 ns/op 14162 B/op 310 allocs/op 1.03
BenchmarkDocument/object_test 9539 ns/op 5793 B/op 97 allocs/op 9250 ns/op 5792 B/op 97 allocs/op 1.03
BenchmarkDocument/array_test 37547 ns/op 10889 B/op 251 allocs/op 33158 ns/op 10889 B/op 251 allocs/op 1.13
BenchmarkDocument/text_test 41066 ns/op 14058 B/op 456 allocs/op 36060 ns/op 14058 B/op 456 allocs/op 1.14
BenchmarkDocument/text_composition_test 37879 ns/op 17538 B/op 461 allocs/op 36575 ns/op 17538 B/op 461 allocs/op 1.04
BenchmarkDocument/rich_text_test 99525 ns/op 36007 B/op 1108 allocs/op 96327 ns/op 36013 B/op 1108 allocs/op 1.03
BenchmarkDocument/counter_test 20051 ns/op 9058 B/op 212 allocs/op 19054 ns/op 9057 B/op 212 allocs/op 1.05
BenchmarkDocument/text_edit_gc_100 4030842 ns/op 1552665 B/op 17149 allocs/op 3872660 ns/op 1552385 B/op 17147 allocs/op 1.04
BenchmarkDocument/text_edit_gc_1000 315389545 ns/op 136610380 B/op 210628 allocs/op 310613494 ns/op 136632456 B/op 210685 allocs/op 1.02
BenchmarkDocument/text_split_gc_100 4601923 ns/op 2217353 B/op 16576 allocs/op 4409468 ns/op 2217089 B/op 16574 allocs/op 1.04
BenchmarkDocument/text_split_gc_1000 372465815 ns/op 214825176 B/op 211267 allocs/op 362853284 ns/op 214858706 B/op 211434 allocs/op 1.03
BenchmarkDocument/text_delete_all_10000 16094437 ns/op 5903132 B/op 41118 allocs/op 19425023 ns/op 5904855 B/op 41126 allocs/op 0.83
BenchmarkDocument/text_delete_all_100000 189188029 ns/op 53847656 B/op 416014 allocs/op 306114309 ns/op 53857464 B/op 416054 allocs/op 0.62
BenchmarkDocument/text_100 309656 ns/op 117749 B/op 5064 allocs/op 299096 ns/op 117749 B/op 5064 allocs/op 1.04
BenchmarkDocument/text_1000 3330805 ns/op 1152305 B/op 50068 allocs/op 3544378 ns/op 1152351 B/op 50068 allocs/op 0.94
BenchmarkDocument/array_1000 1710505 ns/op 1102007 B/op 11854 allocs/op 1677179 ns/op 1102219 B/op 11855 allocs/op 1.02
BenchmarkDocument/array_10000 18546211 ns/op 9905408 B/op 120703 allocs/op 19733031 ns/op 9906578 B/op 120707 allocs/op 0.94
BenchmarkDocument/array_gc_100 176869 ns/op 97417 B/op 1226 allocs/op 169964 ns/op 97414 B/op 1226 allocs/op 1.04
BenchmarkDocument/array_gc_1000 1956681 ns/op 1169631 B/op 12889 allocs/op 1930783 ns/op 1169590 B/op 12889 allocs/op 1.01
BenchmarkDocument/counter_1000 272370 ns/op 197876 B/op 6490 allocs/op 280621 ns/op 197878 B/op 6490 allocs/op 0.97
BenchmarkDocument/counter_10000 2964470 ns/op 2164798 B/op 69497 allocs/op 3107799 ns/op 2164797 B/op 69497 allocs/op 0.95
BenchmarkDocument/object_1000 1879734 ns/op 1450321 B/op 9901 allocs/op 1860886 ns/op 1450571 B/op 9902 allocs/op 1.01
BenchmarkDocument/object_10000 21756187 ns/op 12366422 B/op 101197 allocs/op 24031318 ns/op 12368665 B/op 101205 allocs/op 0.91
BenchmarkRPC/client_to_server 414092585 ns/op 17943538 B/op 308878 allocs/op 456451742 ns/op 17930872 B/op 308654 allocs/op 0.91
BenchmarkRPC/client_to_client_via_server 682933242 ns/op 32745636 B/op 563632 allocs/op 767261817 ns/op 32989652 B/op 568130 allocs/op 0.89
BenchmarkRPC/attach_large_document 1586522161 ns/op 2135054072 B/op 9923 allocs/op 1793196318 ns/op 2136446360 B/op 10114 allocs/op 0.88
BenchmarkRPC/adminCli_to_server 540592496 ns/op 19671984 B/op 321602 allocs/op 570034097 ns/op 19661752 B/op 321590 allocs/op 0.95
BenchmarkLocker 121.7 ns/op 16 B/op 1 allocs/op 129.2 ns/op 16 B/op 1 allocs/op 0.94
BenchmarkLockerParallel 113.2 ns/op 0 B/op 0 allocs/op 151 ns/op 0 B/op 0 allocs/op 0.75
BenchmarkLockerMoreKeys 322 ns/op 14 B/op 0 allocs/op 404.4 ns/op 13 B/op 0 allocs/op 0.80
BenchmarkSync/memory_sync_10_test 7030 ns/op 1341 B/op 39 allocs/op 7483 ns/op 1338 B/op 39 allocs/op 0.94
BenchmarkSync/memory_sync_100_test 59319 ns/op 9096 B/op 299 allocs/op 67005 ns/op 8808 B/op 281 allocs/op 0.89
BenchmarkSync/memory_sync_1000_test 617547 ns/op 84042 B/op 2719 allocs/op 670410 ns/op 81871 B/op 2585 allocs/op 0.92
BenchmarkSync/memory_sync_10000_test 6724334 ns/op 871321 B/op 27803 allocs/op 7672228 ns/op 861485 B/op 26553 allocs/op 0.88
BenchmarkTextEditing 24653998571 ns/op 8436169968 B/op 19835289 allocs/op 30145327317 ns/op 8436116168 B/op 19834828 allocs/op 0.82

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.