Skip to content

Commit

Permalink
perf(json-crdt-extensions): ⚡️ speed up range text materialization
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 7, 2024
1 parent 84aa7e9 commit ac56314
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/json-crdt-extensions/peritext/rga/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ export class Range<T = string> implements Pick<Stateful, 'refresh'>, Printable {
const endId = end.anchor === Anchor.After ? end.id : end.prevId();
if (!startId || !endId) return '';
let result = '';
rga.range0(undefined, startId, endId, (chunk: Chunk<T>, from: number, length: number) => {
if (chunk.data) result += chunk.view().slice(from, from + length);
rga.range0(start.chunk(), startId, endId, (chunk: Chunk<T>, from: number, length: number) => {
const data = chunk.data as string;
if (data) result += data.slice(from, from + length);
});
return result;
}
Expand Down
32 changes: 32 additions & 0 deletions src/json-crdt-extensions/peritext/rga/__tests__/Range.text.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Kit, setupHelloWorldKit, setupHelloWorldWithFewEditsKit, setupNumbersKit, setupNumbersWithTombstonesKit} from '../../__tests__/setup';

const run = (setup: () => Kit) => {
test('can produce text for all possible ranges', () => {
const {peritext} = setup();
const length = peritext.strApi().length();
const str = peritext.strApi().view();
for (let i = 0; i < length - 1; i++) {
for (let j = 1; j <= length - i - i; j++) {
const range = peritext.rangeAt(i, j);
expect(range.text()).toBe(str.slice(i, i + j));
// console.log(str.slice(i, i + j));
}
}
});
};

describe('no edits "hello world"', () => {
run(setupHelloWorldKit);
});

describe('some edits "hello world"', () => {
run(setupHelloWorldWithFewEditsKit);
});

describe('no edits "number"', () => {
run(setupNumbersKit);
});

describe('heavy edits "number"', () => {
run(setupNumbersWithTombstonesKit);
});

0 comments on commit ac56314

Please sign in to comment.