Skip to content

Commit

Permalink
Update data structure related to Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonGyu1 authored Aug 25, 2023
1 parent 9bcfa93 commit a3170de
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions design/data-structure.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: data-structure
target-version: 0.3.1
target-version: 0.4.6
---

# Data Structures
Expand Down Expand Up @@ -28,7 +28,9 @@ The `json` and `crdt` package has data structures for representing the contents

Below is the dependency graph of data structures used in a JSON-like document.

![data-structure](./media/data-structure.png)

<img src="https://github.com/yorkie-team/yorkie/assets/78714820/d91aebad-4637-4873-803e-cda025ade1f6" width="700" />


The data structures can be divided into three groups:

Expand All @@ -45,7 +47,9 @@ JSON-like data strucutres are used when editing JSON-like documents.
- `Primitive`: represents primitive data like `string`, `number`, `boolean`, `null`, etc.
- `Object`: represents [object type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) of JavaScript. Just like JavaScript, you can use `Object` as [hash table](https://en.wikipedia.org/wiki/Hash_table).
- `Array`: represents [array type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) of JavaScript. You can also use `Array` as [list](https://en.wikipedia.org/wiki/List_(abstract_data_type)).
- `Text`: represents text with style attributes in rich text editors such as [Quill](https://github.com/yorkie-team/yorkie-js-sdk/blob/main/examples/quill.html). Users can express styles such as bold, italic, and underline to text content. Of course, it can represent just a plain text in text-based editors such as [CodeMirror](https://github.com/yorkie-team/yorkie-js-sdk/blob/main/examples/index.html). It supports collaborative editing; multiple users can modify parts of the contents without conflict.
- `Text`: represents text with style attributes in rich text editors such as [Quill](https://quilljs.com/). Users can express styles such as bold, italic, and underline to text content. Of course, it can represent just a plain text in text-based editors such as [CodeMirror](https://codemirror.net). It supports collaborative editing; multiple users can modify parts of the contents without conflict.
- `Counter`: represents a counter in the document. As a proxy for the CRDT counter, it is used when the user manipulates the counter from the outside.
- `Tree`: represents CRDT-based tree structure that is used to represent the document tree of text-based editor such as [ProseMirror](https://prosemirror.net/).

JSON-like data structures can be edited through proxies. For example:

Expand All @@ -72,15 +76,16 @@ CRDT data structures are used by JSON-like group to resolve conflicts in concurr
- `ElementRHT`: similar to `RHT`, but has elements as values.
- `RGATreeList`: extended `RGA(Replicated Growable Array)` with an additional index tree. The index tree manages the indices of elements and provides faster access to elements at the int-based index.
- `RGATreeSplit`: extended `RGATreeList` allowing characters to be represented as blocks rather than each single character.

- `CRDTTree`: represents the CRDT tree with an index tree structure'. It resolves conflicts arising from concurrent editing.
### Common Group

Common data structures can be used for general purposes.

- [`SplayTree`](https://en.wikipedia.org/wiki/Splay_tree): A tree that moves nodes to the root by splaying. This is effective when user frequently access the same location, such as text editing. We use `SplayTree` as an index tree to give each node a weight, and to quickly access the node based on the index.
- [`LLRBTree`](https://en.wikipedia.org/wiki/Left-leaning_red%E2%80%93black_tree): A tree simpler than Red-Black Tree. Newly added `floor` method finds the node of the largest key less than or equal to the given key.
- [`Trie`](https://en.wikipedia.org/wiki/Trie): A data structure that can quickly search for prefixes of sequence data such as strings. We use `Trie` to remove nested events when the contents of the `Document` are modified at once.

- `IndexTree`: A tree implementation to represent a document of text-based editors.

### Risks and Mitigation

We can replace the data structures with better ones for some reason, such as performance. For example, `SplayTree` used in `RGATreeList` can be replaced with [TreeList](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/list/TreeList.html).

1 comment on commit a3170de

@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: a3170de Previous: e0e7b17 Ratio
BenchmarkDocument/constructor_test - ns/op 1850 ns/op 1631 ns/op 1.13
BenchmarkDocument/constructor_test - B/op 984 B/op 984 B/op 1
BenchmarkDocument/constructor_test - allocs/op 16 allocs/op 16 allocs/op 1
BenchmarkDocument/status_test - ns/op 1045 ns/op 1568 ns/op 0.67
BenchmarkDocument/status_test - B/op 952 B/op 952 B/op 1
BenchmarkDocument/status_test - allocs/op 14 allocs/op 14 allocs/op 1
BenchmarkDocument/equals_test - ns/op 10540 ns/op 9842 ns/op 1.07
BenchmarkDocument/equals_test - B/op 6192 B/op 6192 B/op 1
BenchmarkDocument/equals_test - allocs/op 106 allocs/op 106 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 26133 ns/op 25128 ns/op 1.04
BenchmarkDocument/nested_update_test - B/op 11689 B/op 11689 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 248 allocs/op 248 allocs/op 1
BenchmarkDocument/delete_test - ns/op 34526 ns/op 33223 ns/op 1.04
BenchmarkDocument/delete_test - B/op 14913 B/op 14913 B/op 1
BenchmarkDocument/delete_test - allocs/op 327 allocs/op 327 allocs/op 1
BenchmarkDocument/object_test - ns/op 12301 ns/op 11854 ns/op 1.04
BenchmarkDocument/object_test - B/op 6449 B/op 6449 B/op 1
BenchmarkDocument/object_test - allocs/op 110 allocs/op 110 allocs/op 1
BenchmarkDocument/array_test - ns/op 48870 ns/op 40251 ns/op 1.21
BenchmarkDocument/array_test - B/op 11546 B/op 11546 B/op 1
BenchmarkDocument/array_test - allocs/op 264 allocs/op 264 allocs/op 1
BenchmarkDocument/text_test - ns/op 46577 ns/op 42988 ns/op 1.08
BenchmarkDocument/text_test - B/op 14618 B/op 14618 B/op 1
BenchmarkDocument/text_test - allocs/op 470 allocs/op 470 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 45682 ns/op 44497 ns/op 1.03
BenchmarkDocument/text_composition_test - B/op 18002 B/op 18002 B/op 1
BenchmarkDocument/text_composition_test - allocs/op 471 allocs/op 471 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 119356 ns/op 121385 ns/op 0.98
BenchmarkDocument/rich_text_test - B/op 36807 B/op 36807 B/op 1
BenchmarkDocument/rich_text_test - allocs/op 1131 allocs/op 1131 allocs/op 1
BenchmarkDocument/counter_test - ns/op 25866 ns/op 24778 ns/op 1.04
BenchmarkDocument/counter_test - B/op 9969 B/op 9971 B/op 1.00
BenchmarkDocument/counter_test - allocs/op 235 allocs/op 235 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 4583946 ns/op 4953916 ns/op 0.93
BenchmarkDocument/text_edit_gc_100 - B/op 1553303 B/op 1553154 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17162 allocs/op 17162 allocs/op 1
BenchmarkDocument/text_edit_gc_1000 - ns/op 366482110 ns/op 415539332 ns/op 0.88
BenchmarkDocument/text_edit_gc_1000 - B/op 136629474 B/op 136654077 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 210686 allocs/op 210807 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 5289847 ns/op 5163744 ns/op 1.02
BenchmarkDocument/text_split_gc_100 - B/op 2217961 B/op 2217867 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16591 allocs/op 16589 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 415439747 ns/op 459261506 ns/op 0.90
BenchmarkDocument/text_split_gc_1000 - B/op 214874269 B/op 214825368 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 211499 allocs/op 211278 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 21684538 ns/op 21640628 ns/op 1.00
BenchmarkDocument/text_delete_all_10000 - B/op 5903595 B/op 5904350 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 41123 allocs/op 41125 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 271782334 ns/op 293915504 ns/op 0.92
BenchmarkDocument/text_delete_all_100000 - B/op 53831596 B/op 53849444 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 415943 allocs/op 416019 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 379109 ns/op 368364 ns/op 1.03
BenchmarkDocument/text_100 - B/op 118211 B/op 118211 B/op 1
BenchmarkDocument/text_100 - allocs/op 5074 allocs/op 5074 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 4174589 ns/op 4020698 ns/op 1.04
BenchmarkDocument/text_1000 - B/op 1152831 B/op 1152828 B/op 1.00
BenchmarkDocument/text_1000 - allocs/op 50078 allocs/op 50078 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 2057883 ns/op 1982625 ns/op 1.04
BenchmarkDocument/array_1000 - B/op 1102649 B/op 1102720 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11867 allocs/op 11867 allocs/op 1
BenchmarkDocument/array_10000 - ns/op 23647028 ns/op 24133350 ns/op 0.98
BenchmarkDocument/array_10000 - B/op 9906521 B/op 9908265 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120716 allocs/op 120725 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 221741 ns/op 218206 ns/op 1.02
BenchmarkDocument/array_gc_100 - B/op 98165 B/op 98170 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1243 allocs/op 1243 allocs/op 1
BenchmarkDocument/array_gc_1000 - ns/op 2395364 ns/op 2439002 ns/op 0.98
BenchmarkDocument/array_gc_1000 - B/op 1170499 B/op 1170334 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12906 allocs/op 12906 allocs/op 1
BenchmarkDocument/counter_1000 - ns/op 355198 ns/op 348837 ns/op 1.02
BenchmarkDocument/counter_1000 - B/op 198535 B/op 198533 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 6503 allocs/op 6503 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 3847450 ns/op 3797783 ns/op 1.01
BenchmarkDocument/counter_10000 - B/op 2165439 B/op 2165466 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 69510 allocs/op 69510 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 2189626 ns/op 2176596 ns/op 1.01
BenchmarkDocument/object_1000 - B/op 1451620 B/op 1451413 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9916 allocs/op 9915 allocs/op 1.00
BenchmarkDocument/object_10000 - ns/op 27891239 ns/op 29075180 ns/op 0.96
BenchmarkDocument/object_10000 - B/op 12367596 B/op 12368142 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 101214 allocs/op 101216 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 511210428 ns/op 623741911 ns/op 0.82
BenchmarkRPC/client_to_server - B/op 12482900 B/op 12446728 B/op 1.00
BenchmarkRPC/client_to_server - allocs/op 177308 allocs/op 177074 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 843296038 ns/op 1007493091 ns/op 0.84
BenchmarkRPC/client_to_client_via_server - B/op 22612484 B/op 22360952 B/op 1.01
BenchmarkRPC/client_to_client_via_server - allocs/op 331064 allocs/op 315734 allocs/op 1.05
BenchmarkRPC/attach_large_document - ns/op 1768706460 ns/op 1462175961 ns/op 1.21
BenchmarkRPC/attach_large_document - B/op 1811296976 B/op 1799019544 B/op 1.01
BenchmarkRPC/attach_large_document - allocs/op 9888 allocs/op 9450 allocs/op 1.05
BenchmarkRPC/adminCli_to_server - ns/op 730511170 ns/op 824637910 ns/op 0.89
BenchmarkRPC/adminCli_to_server - B/op 20393376 B/op 20415996 B/op 1.00
BenchmarkRPC/adminCli_to_server - allocs/op 321613 allocs/op 321647 allocs/op 1.00
BenchmarkLocker - ns/op 148.6 ns/op 144.4 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 149.1 ns/op 142.7 ns/op 1.04
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 419.6 ns/op 330.2 ns/op 1.27
BenchmarkLockerMoreKeys - B/op 14 B/op 15 B/op 0.93
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkSync/memory_sync_10_test - ns/op 8766 ns/op 8751 ns/op 1.00
BenchmarkSync/memory_sync_10_test - B/op 1284 B/op 1284 B/op 1
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 81294 ns/op 79832 ns/op 1.02
BenchmarkSync/memory_sync_100_test - B/op 8721 B/op 8908 B/op 0.98
BenchmarkSync/memory_sync_100_test - allocs/op 278 allocs/op 289 allocs/op 0.96
BenchmarkSync/memory_sync_1000_test - ns/op 800857 ns/op 763146 ns/op 1.05
BenchmarkSync/memory_sync_1000_test - B/op 81775 B/op 82936 B/op 0.99
BenchmarkSync/memory_sync_1000_test - allocs/op 2580 allocs/op 2647 allocs/op 0.97
BenchmarkSync/memory_sync_10000_test - ns/op 8666963 ns/op 8311071 ns/op 1.04
BenchmarkSync/memory_sync_10000_test - B/op 870397 B/op 879987 B/op 0.99
BenchmarkSync/memory_sync_10000_test - allocs/op 27088 allocs/op 28295 allocs/op 0.96
BenchmarkTextEditing - ns/op 31161618328 ns/op 32727791991 ns/op 0.95
BenchmarkTextEditing - B/op 8457658640 B/op 8456442864 B/op 1.00
BenchmarkTextEditing - allocs/op 20617743 allocs/op 20611911 allocs/op 1.00

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

Please sign in to comment.