Skip to content

Commit d070db0

Browse files
authored
Merge pull request #369 from streamich/json-crdt-refactor
JSON CRDT refactors
2 parents 6a0245a + 35b5ede commit d070db0

File tree

80 files changed

+1016
-1101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1016
-1101
lines changed

src/json-crdt-patch/codec/binary/Decoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export class Decoder extends CborDecoder<CrdtReader> {
4545

4646
protected decodeId(): ITimestampStruct {
4747
const reader = this.reader;
48-
const [isRelativeTime, x] = reader.b1vu56();
49-
return isRelativeTime ? new Timestamp(this.patchSid!, x) : new Timestamp(x, reader.vu57());
48+
const [isSessionDifferent, x] = reader.b1vu56();
49+
return isSessionDifferent ? new Timestamp(reader.vu57(), x) : new Timestamp(this.patchSid!, x);
5050
}
5151

5252
protected decodeTss(): ITimespanStruct {

src/json-crdt-patch/codec/binary/Encoder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export class Encoder extends CborEncoder<CrdtWriter> {
5252
const time = id.time;
5353
const writer = this.writer;
5454
if (sessionId === this.patchSid) {
55-
writer.b1vu56(1, time);
55+
writer.b1vu56(0, time);
5656
} else {
57-
writer.b1vu56(0, sessionId);
58-
writer.vu57(time);
57+
writer.b1vu56(1, time);
58+
writer.vu57(sessionId);
5959
}
6060
}
6161

src/json-crdt/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@ and random positions of a 10,000-character newly created string.
1313

1414
```
1515
node benchmarks/json-crdt/strings-short.js
16-
json-crdt StringRga type x 228,880 ops/sec ±0.07% (99 runs sampled)
16+
json-crdt StrNode type x 228,880 ops/sec ±0.07% (99 runs sampled)
1717
json-crdt x 187,700 ops/sec ±0.17% (98 runs sampled)
1818
Y.js x 18,288 ops/sec ±0.19% (100 runs sampled)
1919
Automerge x 23.64 ops/sec ±1.54% (44 runs sampled)
20-
Fastest is json-crdt StringRga type
20+
Fastest is json-crdt StrNode type
2121
```
2222

2323
Inserting one 10-char string and deleting one 10-char string range at random
2424
positions from a 10,000-char string, which was beforehand already edited 5,000 times.
2525

2626
```
2727
node benchmarks/json-crdt/strings-long.js
28-
json-crdt StringRga type x 54,250 ops/sec ±20.68% (23 runs sampled)
28+
json-crdt StrNode type x 54,250 ops/sec ±20.68% (23 runs sampled)
2929
json-crdt x 59,628 ops/sec ±14.39% (32 runs sampled)
3030
Y.js x 16,358 ops/sec ±10.79% (45 runs sampled)
3131
Automerge x 1,777 ops/sec ±5.97% (76 runs sampled)
32-
Fastest is json-crdt,json-crdt StringRga type
32+
Fastest is json-crdt,json-crdt StrNode type
3333
```
3434

3535
Editing a string at 10 repeating positions with +/- 6 characters random variance
3636
from those positions.
3737

3838
```
3939
node benchmarks/json-crdt/strings-repeat-insert-positions.js
40-
json-crdt StringRga type x 8,313 ops/sec ±1.52% (93 runs sampled)
40+
json-crdt StrNode type x 8,313 ops/sec ±1.52% (93 runs sampled)
4141
json-crdt x 6,292 ops/sec ±2.22% (77 runs sampled)
4242
Y.js x 3,104 ops/sec ±1.40% (78 runs sampled)
4343
Automerge x 246 ops/sec ±1.04% (85 runs sampled)
44-
Fastest is json-crdt StringRga type
44+
Fastest is json-crdt StrNode type
4545
```
4646

4747
### Real-world comparisons
@@ -114,22 +114,22 @@ String length: 104852 , Chunk count: 12487
114114
JSON CRDT: 134.596ms
115115
String length: 104852 , Chunk count: 12487
116116
---------------------------------------------
117-
JSON CRDT StringRga: 85.254ms
117+
JSON CRDT StrNode: 85.254ms
118118
String length: 104852 , Chunk count: 12387
119119
---------------------------------------------
120-
JSON CRDT StringRga: 86.487ms
120+
JSON CRDT StrNode: 86.487ms
121121
String length: 104852 , Chunk count: 12387
122122
---------------------------------------------
123-
JSON CRDT StringRga: 73.346ms
123+
JSON CRDT StrNode: 73.346ms
124124
String length: 104852 , Chunk count: 12387
125125
---------------------------------------------
126-
JSON CRDT StringRga: 74.109ms
126+
JSON CRDT StrNode: 74.109ms
127127
String length: 104852 , Chunk count: 12387
128128
---------------------------------------------
129-
JSON CRDT StringRga: 73.593ms
129+
JSON CRDT StrNode: 73.593ms
130130
String length: 104852 , Chunk count: 12387
131131
---------------------------------------------
132-
JSON CRDT StringRga: 74.138ms
132+
JSON CRDT StrNode: 74.138ms
133133
String length: 104852 , Chunk count: 12387
134134
---------------------------------------------
135135
diamond-types-node: 58.114ms

src/json-crdt/__bench__/bench.traces.crdt-libs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ runTraceMatrix({
1414
'automerge-paper',
1515
],
1616
editors: [
17-
'StringRga (json-joy)',
17+
'StrNode (json-joy)',
1818
'json-joy',
1919
// 'Y.js',
2020
// 'Y.rs',

src/json-crdt/__bench__/bench.traces.non-crdt-libs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ runTraceMatrix({
1414
'automerge-paper',
1515
],
1616
editors: [
17-
'StringRga (json-joy)',
17+
'StrNode (json-joy)',
1818
'diamond-types-node',
1919
'rope.js',
2020
'V8 strings',

src/json-crdt/__bench__/profiler/automerge-paper.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
*/
55

66
const {traces} = require('../../data/editing-traces');
7-
const {StringRga} = require('../../../es2020/json-crdt/types/rga-string/StringRga');
7+
const {StrNode} = require('../../../es2020/json-crdt/types/str/StrNode');
88
const {ts} = require('../../../es2020/json-crdt-patch/clock/logical');
99

1010
const patches = traces.get('automerge-paper').txns.map((txn) => txn.patches[0]);
1111
const length = patches.length;
1212
console.log('Document operations:', length, patches);
1313

14-
const runStringRga = () => {
14+
const runStrNode = () => {
1515
console.log('---------------------------------------------');
16-
console.time('JSON CRDT StringRga');
16+
console.time('JSON CRDT StrNode');
1717
let time = 0;
18-
const rga = new StringRga(ts(1, time++));
18+
const rga = new StrNode(ts(1, time++));
1919
for (let i = 0; i < length; i++) {
2020
const [pos, del, c] = patches[i];
2121
if (del) {
@@ -27,14 +27,14 @@ const runStringRga = () => {
2727
}
2828
rga.view();
2929
// console.log(rga.view());
30-
console.timeEnd('JSON CRDT StringRga');
30+
console.timeEnd('JSON CRDT StrNode');
3131
console.log('String length:', rga.length(), ', Chunk count:', rga.size());
3232
// console.log(rga.toString());
3333
};
3434

35-
runStringRga();
36-
runStringRga();
37-
runStringRga();
38-
runStringRga();
39-
runStringRga();
40-
runStringRga();
35+
runStrNode();
36+
runStrNode();
37+
runStrNode();
38+
runStrNode();
39+
runStrNode();
40+
runStrNode();

src/json-crdt/__bench__/strings-long.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Benchmark = require('benchmark');
22
const {Model} = require('../../es2020/json-crdt');
3-
const {StringRga} = require('../../es2020/json-crdt/types/rga-string/StringRga');
3+
const {StrNode} = require('../../es2020/json-crdt/types/str/StrNode');
44
const Y = require('yjs');
55
const Automerge = require('automerge');
66
const {randomU32} = require('hyperdyperid/lib/randomU32');
@@ -14,10 +14,10 @@ const len2 = str2.length;
1414

1515
const operations = 1;
1616

17-
const type = new StringRga(ts(1, 1));
17+
const type = new StrNode(ts(1, 1));
1818
type.ins(ts(1, 1), ts(1, 2), str1);
1919
let time = str1.length + 100;
20-
const editStringRga = () => {
20+
const editStrNode = () => {
2121
for (let i = 0; i < operations; i++) {
2222
const pos1 = randomU32(0, len1 - len2);
2323
const pos2 = randomU32(0, len1 - len2);
@@ -70,7 +70,7 @@ const editAutomerge = () => {
7070
};
7171

7272
for (let i = 0; i < 5000; i++) {
73-
editStringRga();
73+
editStrNode();
7474
editJsonCrdt();
7575
editYjs();
7676
editAutomerge();
@@ -79,8 +79,8 @@ for (let i = 0; i < 5000; i++) {
7979
const suite = new Benchmark.Suite();
8080

8181
suite
82-
.add('json-crdt StringRga type', function () {
83-
editStringRga();
82+
.add('json-crdt StrNode type', function () {
83+
editStrNode();
8484
})
8585
.add('json-crdt', function () {
8686
editJsonCrdt();

src/json-crdt/__bench__/strings-repeat-insert-positions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Benchmark = require('benchmark');
22
const {Model} = require('../../es2020/json-crdt');
3-
const {StringRga} = require('../../es2020/json-crdt/types/rga-string/StringRga');
3+
const {StrNode} = require('../../es2020/json-crdt/types/str/StrNode');
44
const Y = require('yjs');
55
const Automerge = require('automerge');
66
const {randomU32} = require('hyperdyperid/lib/randomU32');
@@ -15,10 +15,10 @@ const str2 = '1234567890';
1515
const len1 = str1.length;
1616
const len2 = str2.length;
1717

18-
const type = new StringRga(ts(1, 1));
18+
const type = new StrNode(ts(1, 1));
1919
type.ins(ts(1, 1), ts(1, 2), str1);
2020
let time = str1.length + 100;
21-
const editStringRga = () => {
21+
const editStrNode = () => {
2222
for (let i = 0; i < insertPositions.length; i++) {
2323
const pos1 = insertPositions[i] + randomU32(0, variance);
2424
const pos2 = randomU32(0, len1 - len2);
@@ -71,7 +71,7 @@ const editAutomerge = () => {
7171
};
7272

7373
for (let i = 0; i < 1000; i++) {
74-
editStringRga();
74+
editStrNode();
7575
editJsonCrdt();
7676
editYjs();
7777
editAutomerge();
@@ -80,8 +80,8 @@ for (let i = 0; i < 1000; i++) {
8080
const suite = new Benchmark.Suite();
8181

8282
suite
83-
.add('json-crdt StringRga type', function () {
84-
editStringRga();
83+
.add('json-crdt StrNode type', function () {
84+
editStrNode();
8585
})
8686
.add('json-crdt', function () {
8787
editJsonCrdt();

src/json-crdt/__bench__/strings-short.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Benchmark = require('benchmark');
22
const {Model} = require('../../es2020/json-crdt');
3-
const {StringRga} = require('../../es2020/json-crdt/types/rga-string/StringRga');
3+
const {StrNode} = require('../../es2020/json-crdt/types/str/StrNode');
44
const Y = require('yjs');
55
const Automerge = require('automerge');
66
const {randomU32} = require('hyperdyperid/lib/randomU32');
@@ -14,8 +14,8 @@ const str2 = '1234567890';
1414
const len1 = str1.length;
1515
const len2 = str2.length;
1616

17-
const editStringRga = () => {
18-
const type = new StringRga(ts(1, 1));
17+
const editStrNode = () => {
18+
const type = new StrNode(ts(1, 1));
1919
type.ins(ts(1, 1), ts(1, 2), str1);
2020
let time = str1.length + 100;
2121
for (let i = 0; i < 10; i++) {
@@ -71,7 +71,7 @@ const editAutomerge = () => {
7171
};
7272

7373
for (let i = 0; i < 10; i++) {
74-
editStringRga();
74+
editStrNode();
7575
editJsonCrdt();
7676
editYjs();
7777
editAutomerge();
@@ -86,8 +86,8 @@ suite
8686
// arr.splice(pos1, {i: null, r: null, p: null, content: {}, deleted: false, id: [1 ,2]}, {i: null, r: null, p: null, content: {}, deleted: false, id: [1 ,2]});
8787
// arr.splice(pos2, 2);
8888
// })
89-
.add('json-crdt StringRga type', function () {
90-
editStringRga();
89+
.add('json-crdt StrNode type', function () {
90+
editStrNode();
9191
})
9292
.add('json-crdt', function () {
9393
editJsonCrdt();

src/json-crdt/__bench__/util/editors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Timestamp} from '../../../json-crdt-patch/clock';
2-
import {StringRga} from '../../types/rga-string/StringRga';
2+
import {StrNode} from '../../nodes';
33
import {Model} from '../../model';
44
import {Doc} from 'diamond-types-node';
55
import * as Y from 'yjs';
@@ -11,11 +11,11 @@ const AutomergeUnstable = require('@automerge/automerge/dist/cjs/unstable');
1111
const Rope = require('rope.js');
1212

1313
export const editors = {
14-
'StringRga (json-joy)': {
15-
name: 'StringRga (json-joy)',
14+
'StrNode (json-joy)': {
15+
name: 'StrNode (json-joy)',
1616
factory: () => {
1717
let time = 0;
18-
const rga = new StringRga(new Timestamp(1, time++));
18+
const rga = new StrNode(new Timestamp(1, time++));
1919
return {
2020
ins: (pos: number, insert: string) => {
2121
rga.insAt(pos, new Timestamp(1, time), insert);

0 commit comments

Comments
 (0)