Skip to content

Commit

Permalink
Revise codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jan 17, 2024
1 parent 68b4e32 commit df46de3
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions pkg/document/crdt/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (p *Counter) ValueType() CounterType {
}

// Value returns the value.
// TODO(hackerwins): We need to use generics to avoid using interface{}.
func (p *Counter) Value() interface{} {
return p.value
}
Expand Down
1 change: 0 additions & 1 deletion pkg/document/crdt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func (r *Root) GarbageLen() int {
switch elem := pair.elem.(type) {
case Container:
elem.Descendants(func(elem Element, parent Container) bool {

seen[elem.CreatedAt().Key()] = true
return false
})
Expand Down
10 changes: 5 additions & 5 deletions pkg/document/json/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ func (p *Array) moveBeforeInternal(nextCreatedAt, createdAt *time.Ticket) {
}
}

// buildArrayMember return the element slice of the given array.
func buildArrayMember(
// buildArrayElements return the element slice of the given array.
func buildArrayElements(
context *change.Context,
elements []interface{},
) []crdt.Element {
members := make([]crdt.Element, len(elements))
elems := make([]crdt.Element, len(elements))

for idx, value := range elements {
ticket := context.IssueTimeTicket()
members[idx] = buildCRDTElement(context, value, ticket)
elems[idx] = buildCRDTElement(context, value, ticket)
}

return members
return elems
}
12 changes: 3 additions & 9 deletions pkg/document/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func buildCRDTElement(
ticket *time.Ticket,
) crdt.Element {
switch elem := value.(type) {
case string, int, int32, int64, float32, float64, []byte, bool, gotime.Time:
case nil, string, int, int32, int64, float32, float64, []byte, bool, gotime.Time:
primitive, err := crdt.NewPrimitive(elem, ticket)
if err != nil {
panic(err)
Expand All @@ -90,17 +90,11 @@ func buildCRDTElement(
}
return counter
case []interface{}:
array := crdt.NewArray(crdt.NewRGATreeList(), ticket, buildArrayMember(context, elem))
array := crdt.NewArray(crdt.NewRGATreeList(), ticket, buildArrayElements(context, elem))
return array
case map[string]interface{}:
obj := crdt.NewObject(crdt.NewElementRHT(), ticket, buildObjectMember(context, elem))
obj := crdt.NewObject(crdt.NewElementRHT(), ticket, buildObjectMembers(context, elem))
return obj
case nil:
primitive, err := crdt.NewPrimitive(nil, ticket)
if err != nil {
panic(err)
}
return primitive
default:
panic("unsupported type")
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/document/json/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (p *Object) SetNewObject(k string, v ...map[string]interface{}) *Object {

// SetNewArray sets a new Array for the given key.
func (p *Object) SetNewArray(k string, v ...[]interface{}) *Array {
// TODO(hackerwins): For now, users can only set initial values with []interface{} without
// the type information of the elements. We need to support the type information.
value := p.setInternal(k, func(ticket *time.Ticket) crdt.Element {
elements := crdt.NewRGATreeList()
if len(v) == 0 {
Expand Down Expand Up @@ -338,8 +340,11 @@ func (p *Object) setInternal(
return elem
}

// buildObjectMember return the element map of the given json.
func buildObjectMember(
// buildObjectMembers constructs an object where all values from the
// user-provided object are transformed into CRDTElements.
// This function takes an object and iterates through its values,
// converting each value into a corresponding CRDTElement.
func buildObjectMembers(
context *change.Context,
json map[string]interface{},
) map[string]crdt.Element {
Expand Down
3 changes: 1 addition & 2 deletions test/integration/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func TestGarbageCollection(t *testing.T) {
assert.Equal(t, d2.GarbageCollect(time.MaxTicket), 3)
})

t.Run("Deregister nested object gc test", func(t *testing.T) {
t.Run("deregister nested object gc test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
err := c1.Attach(ctx, d1)
Expand All @@ -449,7 +449,6 @@ func TestGarbageCollection(t *testing.T) {

assert.Equal(t, 5, d1.GarbageLen())
assert.Equal(t, 5, d1.GarbageCollect(time.MaxTicket))

})

}
2 changes: 1 addition & 1 deletion test/integration/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestObject(t *testing.T) {
})
assert.NoError(t, err)

// 03. garbage collect ( 3 elements: array, 1, 2)
// 03. garbage collect (3 elements: array, 1, 2)
assert.Equal(t, 3, d1.GarbageLen())
assert.Equal(t, 3, d1.GarbageCollect(time.MaxTicket))
})
Expand Down

1 comment on commit df46de3

@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: df46de3 Previous: a8c58f9 Ratio
BenchmarkDocument/constructor_test - ns/op 1432 ns/op 1457 ns/op 0.98
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 911.2 ns/op 849.5 ns/op 1.07
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 7857 ns/op 7524 ns/op 1.04
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 16698 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 22539 ns/op 22335 ns/op 1.01
BenchmarkDocument/delete_test - B/op 15284 B/op 15284 B/op 1
BenchmarkDocument/delete_test - allocs/op 339 allocs/op 339 allocs/op 1
BenchmarkDocument/object_test - ns/op 8465 ns/op 8516 ns/op 0.99
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 28896 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 31018 ns/op 30487 ns/op 1.02
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 28877 ns/op 28865 ns/op 1.00
BenchmarkDocument/text_composition_test - B/op 18430 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 79722 ns/op 80677 ns/op 0.99
BenchmarkDocument/rich_text_test - B/op 38676 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 16862 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 2878081 ns/op 2887897 ns/op 1.00
BenchmarkDocument/text_edit_gc_100 - B/op 1658323 B/op 1655169 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17093 allocs/op 17094 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 228805503 ns/op 229415441 ns/op 1.00
BenchmarkDocument/text_edit_gc_1000 - B/op 144376521 B/op 144344868 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 200893 allocs/op 200908 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 3394834 ns/op 3374708 ns/op 1.01
BenchmarkDocument/text_split_gc_100 - B/op 2316538 B/op 2313351 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16194 allocs/op 16195 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 288122750 ns/op 287430465 ns/op 1.00
BenchmarkDocument/text_split_gc_1000 - B/op 228909404 B/op 228891160 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 203889 allocs/op 203934 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 10374829 ns/op 10779697 ns/op 0.96
BenchmarkDocument/text_delete_all_10000 - B/op 5810879 B/op 5809238 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 40676 allocs/op 40669 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 189959129 ns/op 188925896 ns/op 1.01
BenchmarkDocument/text_delete_all_100000 - B/op 81907186 B/op 81910706 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 411651 allocs/op 411662 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 218220 ns/op 229354 ns/op 0.95
BenchmarkDocument/text_100 - B/op 120139 B/op 118514 B/op 1.01
BenchmarkDocument/text_100 - allocs/op 5082 allocs/op 5082 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 2360148 ns/op 2502759 ns/op 0.94
BenchmarkDocument/text_1000 - B/op 1169124 B/op 1153102 B/op 1.01
BenchmarkDocument/text_1000 - allocs/op 50086 allocs/op 50086 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1205096 ns/op 1262770 ns/op 0.95
BenchmarkDocument/array_1000 - B/op 1091372 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 13002771 ns/op 13221008 ns/op 0.98
BenchmarkDocument/array_10000 - B/op 9800403 B/op 9799195 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120297 allocs/op 120291 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 146549 ns/op 172372 ns/op 0.85
BenchmarkDocument/array_gc_100 - B/op 132657 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 1388390 ns/op 1651513 ns/op 0.84
BenchmarkDocument/array_gc_1000 - B/op 1159025 B/op 1241466 B/op 0.93
BenchmarkDocument/array_gc_1000 - allocs/op 12874 allocs/op 14897 allocs/op 0.86
BenchmarkDocument/counter_1000 - ns/op 204245 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 2200883 ns/op 2217464 ns/op 0.99
BenchmarkDocument/counter_10000 - B/op 2087846 B/op 2087814 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 59774 allocs/op 59774 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 1357701 ns/op 1442631 ns/op 0.94
BenchmarkDocument/object_1000 - B/op 1427970 B/op 1428083 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9847 allocs/op 9847 allocs/op 1
BenchmarkDocument/object_10000 - ns/op 15075103 ns/op 15149865 ns/op 1.00
BenchmarkDocument/object_10000 - B/op 12166597 B/op 12166338 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 100560 allocs/op 100562 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1055146 ns/op 1068412 ns/op 0.99
BenchmarkDocument/tree_100 - B/op 943781 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 75937840 ns/op 79029252 ns/op 0.96
BenchmarkDocument/tree_1000 - B/op 86460430 B/op 86460602 B/op 1.00
BenchmarkDocument/tree_1000 - allocs/op 60116 allocs/op 60116 allocs/op 1
BenchmarkDocument/tree_10000 - ns/op 9318329106 ns/op 9661149264 ns/op 0.96
BenchmarkDocument/tree_10000 - B/op 8580984336 B/op 8580973784 B/op 1.00
BenchmarkDocument/tree_10000 - allocs/op 600246 allocs/op 600230 allocs/op 1.00
BenchmarkDocument/tree_delete_all_1000 - ns/op 72446432 ns/op 79816852 ns/op 0.91
BenchmarkDocument/tree_delete_all_1000 - B/op 86991200 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 3619941 ns/op 3866691 ns/op 0.94
BenchmarkDocument/tree_edit_gc_100 - B/op 4121083 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 293424622 ns/op 326051394 ns/op 0.90
BenchmarkDocument/tree_edit_gc_1000 - B/op 383466794 B/op 383466086 B/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 145414 allocs/op 145407 allocs/op 1.00
BenchmarkDocument/tree_split_gc_100 - ns/op 2428268 ns/op 2611696 ns/op 0.93
BenchmarkDocument/tree_split_gc_100 - B/op 2386948 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 178717383 ns/op 196739556 ns/op 0.91
BenchmarkDocument/tree_split_gc_1000 - B/op 221990180 B/op 221991590 B/op 1.00
BenchmarkDocument/tree_split_gc_1000 - allocs/op 112247 allocs/op 112260 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 355513429 ns/op 356144469 ns/op 1.00
BenchmarkRPC/client_to_server - B/op 18061021 B/op 17801216 B/op 1.01
BenchmarkRPC/client_to_server - allocs/op 166913 allocs/op 166911 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 601364199 ns/op 613079232 ns/op 0.98
BenchmarkRPC/client_to_client_via_server - B/op 32019328 B/op 31722656 B/op 1.01
BenchmarkRPC/client_to_client_via_server - allocs/op 311993 allocs/op 313048 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1345381105 ns/op 1479934941 ns/op 0.91
BenchmarkRPC/attach_large_document - B/op 1879540640 B/op 1890029520 B/op 0.99
BenchmarkRPC/attach_large_document - allocs/op 7513 allocs/op 7567 allocs/op 0.99
BenchmarkRPC/adminCli_to_server - ns/op 534995965 ns/op 537051132 ns/op 1.00
BenchmarkRPC/adminCli_to_server - B/op 35993616 B/op 36806452 B/op 0.98
BenchmarkRPC/adminCli_to_server - allocs/op 289703 allocs/op 289659 allocs/op 1.00
BenchmarkLocker - ns/op 69.3 ns/op 65.34 ns/op 1.06
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 38.11 ns/op 38.48 ns/op 0.99
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 146 ns/op 145.1 ns/op 1.01
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 3775275 ns/op 3768343 ns/op 1.00
BenchmarkChange/Push_10_Changes - B/op 125170 B/op 126223 B/op 0.99
BenchmarkChange/Push_10_Changes - allocs/op 1253 allocs/op 1254 allocs/op 1.00
BenchmarkChange/Push_100_Changes - ns/op 13915164 ns/op 14005221 ns/op 0.99
BenchmarkChange/Push_100_Changes - B/op 640536 B/op 648654 B/op 0.99
BenchmarkChange/Push_100_Changes - allocs/op 6537 allocs/op 6539 allocs/op 1.00
BenchmarkChange/Push_1000_Changes - ns/op 111793224 ns/op 114047673 ns/op 0.98
BenchmarkChange/Push_1000_Changes - B/op 6037650 B/op 6036337 B/op 1.00
BenchmarkChange/Push_1000_Changes - allocs/op 62158 allocs/op 62157 allocs/op 1.00
BenchmarkChange/Pull_10_Changes - ns/op 2814301 ns/op 2839363 ns/op 0.99
BenchmarkChange/Pull_10_Changes - B/op 100210 B/op 100881 B/op 0.99
BenchmarkChange/Pull_10_Changes - allocs/op 951 allocs/op 952 allocs/op 1.00
BenchmarkChange/Pull_100_Changes - ns/op 4259687 ns/op 4308547 ns/op 0.99
BenchmarkChange/Pull_100_Changes - B/op 257202 B/op 258255 B/op 1.00
BenchmarkChange/Pull_100_Changes - allocs/op 3155 allocs/op 3154 allocs/op 1.00
BenchmarkChange/Pull_1000_Changes - ns/op 8151693 ns/op 8372656 ns/op 0.97
BenchmarkChange/Pull_1000_Changes - B/op 1394511 B/op 1396187 B/op 1.00
BenchmarkChange/Pull_1000_Changes - allocs/op 26872 allocs/op 26871 allocs/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - ns/op 16407804 ns/op 16777319 ns/op 0.98
BenchmarkSnapshot/Push_3KB_snapshot - B/op 801290 B/op 809995 B/op 0.99
BenchmarkSnapshot/Push_3KB_snapshot - allocs/op 6542 allocs/op 6542 allocs/op 1
BenchmarkSnapshot/Push_30KB_snapshot - ns/op 115738948 ns/op 117069671 ns/op 0.99
BenchmarkSnapshot/Push_30KB_snapshot - B/op 6335251 B/op 6250016 B/op 1.01
BenchmarkSnapshot/Push_30KB_snapshot - allocs/op 62276 allocs/op 62161 allocs/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - ns/op 6422314 ns/op 6563310 ns/op 0.98
BenchmarkSnapshot/Pull_3KB_snapshot - B/op 905056 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 14526154 ns/op 15000336 ns/op 0.97
BenchmarkSnapshot/Pull_30KB_snapshot - B/op 6979797 B/op 6977754 B/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - allocs/op 144143 allocs/op 144148 allocs/op 1.00
BenchmarkSync/memory_sync_10_test - ns/op 6771 ns/op 6824 ns/op 0.99
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 51096 ns/op 51635 ns/op 0.99
BenchmarkSync/memory_sync_100_test - B/op 8653 B/op 8659 B/op 1.00
BenchmarkSync/memory_sync_100_test - allocs/op 273 allocs/op 274 allocs/op 1.00
BenchmarkSync/memory_sync_1000_test - ns/op 588400 ns/op 582724 ns/op 1.01
BenchmarkSync/memory_sync_1000_test - B/op 74408 B/op 74925 B/op 0.99
BenchmarkSync/memory_sync_1000_test - allocs/op 2114 allocs/op 2141 allocs/op 0.99
BenchmarkSync/memory_sync_10000_test - ns/op 6965997 ns/op 7320373 ns/op 0.95
BenchmarkSync/memory_sync_10000_test - B/op 763125 B/op 761425 B/op 1.00
BenchmarkSync/memory_sync_10000_test - allocs/op 20593 allocs/op 20593 allocs/op 1
BenchmarkTextEditing - ns/op 18570741282 ns/op 18585017456 ns/op 1.00
BenchmarkTextEditing - B/op 9042151392 B/op 9037385240 B/op 1.00
BenchmarkTextEditing - allocs/op 19923952 allocs/op 19920435 allocs/op 1.00

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

Please sign in to comment.