Skip to content

Commit

Permalink
Replace CreateTree with NewTree
Browse files Browse the repository at this point in the history
  • Loading branch information
highcloud100 committed Jan 16, 2024
1 parent b03df91 commit 0563131
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
8 changes: 5 additions & 3 deletions pkg/document/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func toElement(ctx *change.Context, elem crdt.Element) crdt.Element {
counter := NewCounter(elem.Value(), elem.ValueType())
return counter.Initialize(ctx, elem)
case *crdt.Tree:
return NewTree(ctx, elem)
tree := NewTree()
return tree.Initialize(ctx, elem)
case *crdt.Primitive:
return elem
}
Expand All @@ -77,8 +78,9 @@ func buildCRDTElement(
panic(err)
}
return primitive
case *TreeNode:
return crdt.NewTree(buildRoot(context, elem, ticket), ticket)
case *Tree:
crdtTree := crdt.NewTree(buildRoot(context, elem.initialRoot, ticket), ticket)
return crdtTree
case *Text:
return crdt.NewText(crdt.NewRGATreeSplit(crdt.InitialTextNode()), ticket)
case *Counter:
Expand Down
10 changes: 4 additions & 6 deletions pkg/document/json/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,8 @@ func (p *Object) SetNewTree(k string, initialRoot ...*TreeNode) *Tree {
if len(initialRoot) > 0 {
root = initialRoot[0]
}

return NewTree(
p.context,
crdt.NewTree(buildRoot(p.context, root, ticket), ticket),
)
tree := NewTree(root)
return tree.Initialize(p.context, crdt.NewTree(buildRoot(p.context, root, ticket), ticket))
})

return v.(*Tree)
Expand Down Expand Up @@ -305,7 +302,8 @@ func (p *Object) GetTree(k string) *Tree {

switch elem := p.Object.Get(k).(type) {
case *crdt.Tree:
return NewTree(p.context, elem)
tree := NewTree(nil)
return tree.Initialize(p.context, elem)
case *Tree:
return elem
default:
Expand Down
14 changes: 8 additions & 6 deletions pkg/document/json/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,23 @@ type TreeNode struct {
// Tree is a CRDT-based tree structure that is used to represent the document
// tree of text-based editor such as ProseMirror.
type Tree struct {
initialRoot *TreeNode
*crdt.Tree
context *change.Context
}

// NewTree creates a new instance of Tree.
func NewTree(ctx *change.Context, tree *crdt.Tree) *Tree {
func NewTree(root ...*TreeNode) *Tree {
return &Tree{
Tree: tree,
context: ctx,
initialRoot: root[0],
}
}

// CreateTree creates a new instance of Tree for json literal.
func CreateTree(root *TreeNode) *TreeNode {
return root
// Initialize initializes the Tree with context, crdt.Tree
func (t *Tree) Initialize(ctx *change.Context, tree *crdt.Tree) *Tree {
t.Tree = tree
t.context = ctx
return t
}

// validateTextNode make sure that text node have non-empty string value
Expand Down
2 changes: 1 addition & 1 deletion test/integration/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestObject(t *testing.T) {
err = d1.Update(func(root *json.Object, p *presence.Presence) error {

data := map[string]interface{}{
"tree": json.CreateTree(&json.TreeNode{
"tree": json.NewTree(&json.TreeNode{
Type: "doc",
Children: []json.TreeNode{{
Type: "p", Children: []json.TreeNode{{
Expand Down

1 comment on commit 0563131

@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: 0563131 Previous: a8c58f9 Ratio
BenchmarkDocument/constructor_test - ns/op 1396 ns/op 1457 ns/op 0.96
BenchmarkDocument/constructor_test - B/op 1224 B/op 1224 B/op 1
BenchmarkDocument/constructor_test - allocs/op 21 allocs/op 21 allocs/op 1
BenchmarkDocument/status_test - ns/op 1021 ns/op 849.5 ns/op 1.20
BenchmarkDocument/status_test - B/op 1192 B/op 1192 B/op 1
BenchmarkDocument/status_test - allocs/op 19 allocs/op 19 allocs/op 1
BenchmarkDocument/equals_test - ns/op 7430 ns/op 7524 ns/op 0.99
BenchmarkDocument/equals_test - B/op 6977 B/op 6977 B/op 1
BenchmarkDocument/equals_test - allocs/op 124 allocs/op 124 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 16783 ns/op 18601 ns/op 0.90
BenchmarkDocument/nested_update_test - B/op 12059 B/op 12059 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 260 allocs/op 260 allocs/op 1
BenchmarkDocument/delete_test - ns/op 22456 ns/op 22335 ns/op 1.01
BenchmarkDocument/delete_test - B/op 15283 B/op 15284 B/op 1.00
BenchmarkDocument/delete_test - allocs/op 339 allocs/op 339 allocs/op 1
BenchmarkDocument/object_test - ns/op 8672 ns/op 8516 ns/op 1.02
BenchmarkDocument/object_test - B/op 6753 B/op 6753 B/op 1
BenchmarkDocument/object_test - allocs/op 118 allocs/op 118 allocs/op 1
BenchmarkDocument/array_test - ns/op 28799 ns/op 28781 ns/op 1.00
BenchmarkDocument/array_test - B/op 11883 B/op 11883 B/op 1
BenchmarkDocument/array_test - allocs/op 274 allocs/op 274 allocs/op 1
BenchmarkDocument/text_test - ns/op 30690 ns/op 30487 ns/op 1.01
BenchmarkDocument/text_test - B/op 14916 B/op 14828 B/op 1.01
BenchmarkDocument/text_test - allocs/op 470 allocs/op 470 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 28907 ns/op 28865 ns/op 1.00
BenchmarkDocument/text_composition_test - B/op 18428 B/op 18310 B/op 1.01
BenchmarkDocument/text_composition_test - allocs/op 479 allocs/op 479 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 79888 ns/op 80677 ns/op 0.99
BenchmarkDocument/rich_text_test - B/op 38682 B/op 38572 B/op 1.00
BenchmarkDocument/rich_text_test - allocs/op 1149 allocs/op 1149 allocs/op 1
BenchmarkDocument/counter_test - ns/op 16978 ns/op 16759 ns/op 1.01
BenchmarkDocument/counter_test - B/op 10466 B/op 10242 B/op 1.02
BenchmarkDocument/counter_test - allocs/op 238 allocs/op 238 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 2890988 ns/op 2887897 ns/op 1.00
BenchmarkDocument/text_edit_gc_100 - B/op 1658593 B/op 1655169 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17096 allocs/op 17094 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 230411323 ns/op 229415441 ns/op 1.00
BenchmarkDocument/text_edit_gc_1000 - B/op 144406060 B/op 144344868 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 201044 allocs/op 200908 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 3363835 ns/op 3374708 ns/op 1.00
BenchmarkDocument/text_split_gc_100 - B/op 2316967 B/op 2313351 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16197 allocs/op 16195 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 289368842 ns/op 287430465 ns/op 1.01
BenchmarkDocument/text_split_gc_1000 - B/op 228928984 B/op 228891160 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 203975 allocs/op 203934 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 10972208 ns/op 10779697 ns/op 1.02
BenchmarkDocument/text_delete_all_10000 - B/op 5810241 B/op 5809238 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 40673 allocs/op 40669 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 192364991 ns/op 188925896 ns/op 1.02
BenchmarkDocument/text_delete_all_100000 - B/op 81892389 B/op 81910706 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 411586 allocs/op 411662 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 223173 ns/op 229354 ns/op 0.97
BenchmarkDocument/text_100 - B/op 120138 B/op 118514 B/op 1.01
BenchmarkDocument/text_100 - allocs/op 5082 allocs/op 5082 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 2393930 ns/op 2502759 ns/op 0.96
BenchmarkDocument/text_1000 - B/op 1169125 B/op 1153102 B/op 1.01
BenchmarkDocument/text_1000 - allocs/op 50086 allocs/op 50086 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1220636 ns/op 1262770 ns/op 0.97
BenchmarkDocument/array_1000 - B/op 1091411 B/op 1091147 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11830 allocs/op 11829 allocs/op 1.00
BenchmarkDocument/array_10000 - ns/op 13231493 ns/op 13221008 ns/op 1.00
BenchmarkDocument/array_10000 - B/op 9799100 B/op 9799195 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120291 allocs/op 120291 allocs/op 1
BenchmarkDocument/array_gc_100 - ns/op 149093 ns/op 172372 ns/op 0.86
BenchmarkDocument/array_gc_100 - B/op 132651 B/op 139899 B/op 0.95
BenchmarkDocument/array_gc_100 - allocs/op 1258 allocs/op 1472 allocs/op 0.85
BenchmarkDocument/array_gc_1000 - ns/op 1385364 ns/op 1651513 ns/op 0.84
BenchmarkDocument/array_gc_1000 - B/op 1159110 B/op 1241466 B/op 0.93
BenchmarkDocument/array_gc_1000 - allocs/op 12875 allocs/op 14897 allocs/op 0.86
BenchmarkDocument/counter_1000 - ns/op 203859 ns/op 210894 ns/op 0.97
BenchmarkDocument/counter_1000 - B/op 192917 B/op 192884 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 5767 allocs/op 5767 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 2195532 ns/op 2217464 ns/op 0.99
BenchmarkDocument/counter_10000 - B/op 2087848 B/op 2087814 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 59774 allocs/op 59774 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 1357729 ns/op 1442631 ns/op 0.94
BenchmarkDocument/object_1000 - B/op 1428216 B/op 1428083 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9847 allocs/op 9847 allocs/op 1
BenchmarkDocument/object_10000 - ns/op 15198522 ns/op 15149865 ns/op 1.00
BenchmarkDocument/object_10000 - B/op 12165975 B/op 12166338 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 100561 allocs/op 100562 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1036406 ns/op 1068412 ns/op 0.97
BenchmarkDocument/tree_100 - B/op 943782 B/op 943709 B/op 1.00
BenchmarkDocument/tree_100 - allocs/op 6102 allocs/op 6101 allocs/op 1.00
BenchmarkDocument/tree_1000 - ns/op 75529392 ns/op 79029252 ns/op 0.96
BenchmarkDocument/tree_1000 - B/op 86460584 B/op 86460602 B/op 1.00
BenchmarkDocument/tree_1000 - allocs/op 60116 allocs/op 60116 allocs/op 1
BenchmarkDocument/tree_10000 - ns/op 9240214439 ns/op 9661149264 ns/op 0.96
BenchmarkDocument/tree_10000 - B/op 8580672480 B/op 8580973784 B/op 1.00
BenchmarkDocument/tree_10000 - allocs/op 600236 allocs/op 600230 allocs/op 1.00
BenchmarkDocument/tree_delete_all_1000 - ns/op 72815753 ns/op 79816852 ns/op 0.91
BenchmarkDocument/tree_delete_all_1000 - B/op 86991229 B/op 86990889 B/op 1.00
BenchmarkDocument/tree_delete_all_1000 - allocs/op 67756 allocs/op 67750 allocs/op 1.00
BenchmarkDocument/tree_edit_gc_100 - ns/op 3692947 ns/op 3866691 ns/op 0.96
BenchmarkDocument/tree_edit_gc_100 - B/op 4121123 B/op 4121023 B/op 1.00
BenchmarkDocument/tree_edit_gc_100 - allocs/op 14359 allocs/op 14358 allocs/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - ns/op 291609016 ns/op 326051394 ns/op 0.89
BenchmarkDocument/tree_edit_gc_1000 - B/op 383466380 B/op 383466086 B/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 145411 allocs/op 145407 allocs/op 1.00
BenchmarkDocument/tree_split_gc_100 - ns/op 2443222 ns/op 2611696 ns/op 0.94
BenchmarkDocument/tree_split_gc_100 - B/op 2386957 B/op 2386898 B/op 1.00
BenchmarkDocument/tree_split_gc_100 - allocs/op 10344 allocs/op 10343 allocs/op 1.00
BenchmarkDocument/tree_split_gc_1000 - ns/op 177744214 ns/op 196739556 ns/op 0.90
BenchmarkDocument/tree_split_gc_1000 - B/op 221990624 B/op 221991590 B/op 1.00
BenchmarkDocument/tree_split_gc_1000 - allocs/op 112255 allocs/op 112260 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 358468663 ns/op 356144469 ns/op 1.01
BenchmarkRPC/client_to_server - B/op 17550466 B/op 17801216 B/op 0.99
BenchmarkRPC/client_to_server - allocs/op 166920 allocs/op 166911 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 618673876 ns/op 613079232 ns/op 1.01
BenchmarkRPC/client_to_client_via_server - B/op 34373220 B/op 31722656 B/op 1.08
BenchmarkRPC/client_to_client_via_server - allocs/op 313250 allocs/op 313048 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1278180515 ns/op 1479934941 ns/op 0.86
BenchmarkRPC/attach_large_document - B/op 1878733760 B/op 1890029520 B/op 0.99
BenchmarkRPC/attach_large_document - allocs/op 7559 allocs/op 7567 allocs/op 1.00
BenchmarkRPC/adminCli_to_server - ns/op 534886584 ns/op 537051132 ns/op 1.00
BenchmarkRPC/adminCli_to_server - B/op 35981616 B/op 36806452 B/op 0.98
BenchmarkRPC/adminCli_to_server - allocs/op 289636 allocs/op 289659 allocs/op 1.00
BenchmarkLocker - ns/op 67.31 ns/op 65.34 ns/op 1.03
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 38.83 ns/op 38.48 ns/op 1.01
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 145.5 ns/op 145.1 ns/op 1.00
BenchmarkLockerMoreKeys - B/op 15 B/op 15 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkChange/Push_10_Changes - ns/op 3786246 ns/op 3768343 ns/op 1.00
BenchmarkChange/Push_10_Changes - B/op 126140 B/op 126223 B/op 1.00
BenchmarkChange/Push_10_Changes - allocs/op 1254 allocs/op 1254 allocs/op 1
BenchmarkChange/Push_100_Changes - ns/op 14274460 ns/op 14005221 ns/op 1.02
BenchmarkChange/Push_100_Changes - B/op 650650 B/op 648654 B/op 1.00
BenchmarkChange/Push_100_Changes - allocs/op 6543 allocs/op 6539 allocs/op 1.00
BenchmarkChange/Push_1000_Changes - ns/op 113881333 ns/op 114047673 ns/op 1.00
BenchmarkChange/Push_1000_Changes - B/op 6137069 B/op 6036337 B/op 1.02
BenchmarkChange/Push_1000_Changes - allocs/op 62158 allocs/op 62157 allocs/op 1.00
BenchmarkChange/Pull_10_Changes - ns/op 2823866 ns/op 2839363 ns/op 0.99
BenchmarkChange/Pull_10_Changes - B/op 100575 B/op 100881 B/op 1.00
BenchmarkChange/Pull_10_Changes - allocs/op 952 allocs/op 952 allocs/op 1
BenchmarkChange/Pull_100_Changes - ns/op 4292883 ns/op 4308547 ns/op 1.00
BenchmarkChange/Pull_100_Changes - B/op 257956 B/op 258255 B/op 1.00
BenchmarkChange/Pull_100_Changes - allocs/op 3154 allocs/op 3154 allocs/op 1
BenchmarkChange/Pull_1000_Changes - ns/op 8394075 ns/op 8372656 ns/op 1.00
BenchmarkChange/Pull_1000_Changes - B/op 1398100 B/op 1396187 B/op 1.00
BenchmarkChange/Pull_1000_Changes - allocs/op 26870 allocs/op 26871 allocs/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - ns/op 16619308 ns/op 16777319 ns/op 0.99
BenchmarkSnapshot/Push_3KB_snapshot - B/op 806480 B/op 809995 B/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - allocs/op 6541 allocs/op 6542 allocs/op 1.00
BenchmarkSnapshot/Push_30KB_snapshot - ns/op 116839888 ns/op 117069671 ns/op 1.00
BenchmarkSnapshot/Push_30KB_snapshot - B/op 6291881 B/op 6250016 B/op 1.01
BenchmarkSnapshot/Push_30KB_snapshot - allocs/op 62170 allocs/op 62161 allocs/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - ns/op 6561663 ns/op 6563310 ns/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - B/op 905681 B/op 905174 B/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - allocs/op 14885 allocs/op 14882 allocs/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - ns/op 15284257 ns/op 15000336 ns/op 1.02
BenchmarkSnapshot/Pull_30KB_snapshot - B/op 6989966 B/op 6977754 B/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - allocs/op 144154 allocs/op 144148 allocs/op 1.00
BenchmarkSync/memory_sync_10_test - ns/op 6685 ns/op 6824 ns/op 0.98
BenchmarkSync/memory_sync_10_test - B/op 1286 B/op 1286 B/op 1
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 50756 ns/op 51635 ns/op 0.98
BenchmarkSync/memory_sync_100_test - B/op 8660 B/op 8659 B/op 1.00
BenchmarkSync/memory_sync_100_test - allocs/op 274 allocs/op 274 allocs/op 1
BenchmarkSync/memory_sync_1000_test - ns/op 590768 ns/op 582724 ns/op 1.01
BenchmarkSync/memory_sync_1000_test - B/op 74555 B/op 74925 B/op 1.00
BenchmarkSync/memory_sync_1000_test - allocs/op 2120 allocs/op 2141 allocs/op 0.99
BenchmarkSync/memory_sync_10000_test - ns/op 7194796 ns/op 7320373 ns/op 0.98
BenchmarkSync/memory_sync_10000_test - B/op 755312 B/op 761425 B/op 0.99
BenchmarkSync/memory_sync_10000_test - allocs/op 20502 allocs/op 20593 allocs/op 1.00
BenchmarkTextEditing - ns/op 18928897346 ns/op 18585017456 ns/op 1.02
BenchmarkTextEditing - B/op 9042251960 B/op 9037385240 B/op 1.00
BenchmarkTextEditing - allocs/op 19924271 allocs/op 19920435 allocs/op 1.00

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

Please sign in to comment.