Skip to content

Commit

Permalink
perf(json-crdt): ⚡️ improve minor decoding in binary codec
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 13, 2023
1 parent 9afda4e commit 896ea8b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
13 changes: 6 additions & 7 deletions src/json-crdt/codec/structural/binary/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,21 @@ export class Decoder extends CborDecoderBase<CrdtReader> {
const octet = reader.u8();
const major = octet >> 5;
const minor = octet & 0b11111;
const length = minor < 0b11111 ? minor : reader.vu57();
switch (major) {
case CRDT_MAJOR.CON:
return this.cCon(id, length);
return this.cCon(id, minor);
case CRDT_MAJOR.VAL:
return this.cVal(id);
case CRDT_MAJOR.OBJ:
return this.cObj(id, length);
return this.cObj(id, minor < 0b11111 ? minor : reader.vu57());
case CRDT_MAJOR.VEC:
return this.cVec(id, length);
return this.cVec(id, minor < 0b11111 ? minor : reader.vu57());
case CRDT_MAJOR.STR:
return this.cStr(id, length);
return this.cStr(id, minor < 0b11111 ? minor : reader.vu57());
case CRDT_MAJOR.BIN:
return this.cBin(id, length);
return this.cBin(id, minor < 0b11111 ? minor : reader.vu57());
case CRDT_MAJOR.ARR:
return this.cArr(id, length);
return this.cArr(id, minor < 0b11111 ? minor : reader.vu57());
}
throw new Error('UNKNOWN_NODE');
}
Expand Down
9 changes: 3 additions & 6 deletions src/json-crdt/codec/structural/binary/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,17 @@ export class Encoder extends CborEncoder<CrdtWriter> {
const val = node.val;
this.ts(node.id);
if (val instanceof Timestamp) {
// this.writeTL(CRDT_MAJOR_OVERLAY.CON, 1);
this.writer.u8(1);
this.writer.u8(1); // this.writeTL(CRDT_MAJOR_OVERLAY.CON, 1);
this.ts(val as Timestamp);
} else {
// this.writeTL(CRDT_MAJOR_OVERLAY.CON, 0);
this.writer.u8(0);
this.writer.u8(0); // this.writeTL(CRDT_MAJOR_OVERLAY.CON, 0);
this.writeAny(val);
}
}

protected cVal(node: nodes.ValNode): void {
this.ts(node.id);
// this.writeTL(CRDT_MAJOR_OVERLAY.VAL, 0);
this.writer.u8(0b00100000);
this.writer.u8(0b00100000); // this.writeTL(CRDT_MAJOR_OVERLAY.VAL, 0);
this.cNode(node.node());
}

Expand Down
9 changes: 0 additions & 9 deletions src/json-crdt/codec/structural/binary/README.md

This file was deleted.

0 comments on commit 896ea8b

Please sign in to comment.