Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"bench:immer": "node --loader ts-node/esm --experimental-specifier-resolution=node examples/bench-immer-apply.ts"
},
"dependencies": {
"@cocalc/diff-match-patch": "^1.0.0",
"@cocalc/diff-match-patch": "^1.1.0",
"events": "^3.3.0",
"fast-deep-equal": "^3.1.3",
"fast-json-stable-stringify": "^2.1.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
packages:
- "."

# @cocalc/diff-match-patch is our own package (published by us via npm
# trusted publishing), so the new-release age guard does not apply to it.
minimumReleaseAgeExclude:
- "@cocalc/diff-match-patch"

overrides:
"ajv@>=6.0.0 <6.14.0": ">=6.14.0 <7"
"baseline-browser-mapping@>=2.0.0 <2.11.0": ">=2.11.0 <3"
Expand Down
50 changes: 50 additions & 0 deletions src/dmp-work-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { applyPatch, diffMain, DIFF_WORK_LIMIT, makePatch } from "./dmp";

// Texts whose diff is too costly to finish within the budget: unrelated
// random text, so the cutoff decides the result.
function random(n: number, seed: number): string {
let s = seed;
let out = "";
for (let i = 0; i < n; i++) {
s = (s * 1103515245 + 12345) % 2 ** 31;
out += String.fromCharCode(97 + (s % 26));
}
return out;
}

describe("diffs are bounded by work, not the clock", () => {
const a = random(100_000, 1);
const b = random(100_000, 2);
const small = ["the quick brown fox", "the quick red fox jumps"];

afterEach(() => vi.restoreAllMocks());

it("uses diffWorkLimit (25M)", () => {
expect(DIFF_WORK_LIMIT).toBe(25_000_000);
});

it("never reads the clock: makePatch, diffMain and applyPatch", () => {
vi.spyOn(Date, "now").mockImplementation(() => {
throw Error("the clock was read");
});
const patch = makePatch(small[0], small[1]);
expect(applyPatch(patch, small[0])).toEqual([small[1], true]);
expect(diffMain(small[0], small[1]).length).toBeGreaterThan(0);
// ... nor for a diff cut off by the budget.
expect(applyPatch(makePatch(a, b), a)).toEqual([b, true]);
});

it("the same texts give the same patch whatever the clock says", () => {
const reference = JSON.stringify(makePatch(a, b));
let t = 0;
for (const clock of [
() => 0, // frozen
() => (t += 60_000), // racing: a minute per read
() => Date.UTC(2100, 0, 1), // far ahead
]) {
vi.spyOn(Date, "now").mockImplementation(clock);
expect(JSON.stringify(makePatch(a, b))).toBe(reference);
vi.restoreAllMocks();
}
});
});
9 changes: 8 additions & 1 deletion src/dmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { DiffMatchPatch, PatchObject } from "@cocalc/diff-match-patch";

export type CompressedPatch = [[-1 | 0 | 1, string][], number, number, number, number][];

// A diff (and so a patch) is cut off by a deterministic work budget, never by
// the clock: the same two texts give the same patch on any machine, however
// fast or loaded (diffTimeout = 0.2 made a commit's patch, and its size,
// depend on the machine). 25M units take about 0.2 s on V8 on an AMD EPYC
// core (@cocalc/diff-match-patch README): measured there, not guaranteed.
export const DIFF_WORK_LIMIT = 25_000_000;

const dmp = new DiffMatchPatch();
dmp.diffTimeout = 0.2;
dmp.diffWorkLimit = DIFF_WORK_LIMIT;

export const diffMain = dmp.diff_main.bind(dmp);
export const patchMake = dmp.patch_make.bind(dmp);
Expand Down
Loading