Skip to content

Commit a2beab0

Browse files
Cache playground Lambda diffs and bound token work by block
Keep the Lambda view mounted, cache its rendered comparison by input text, and render collapsed context only when expanded. Preserve expansion for unchanged regions across edits. Count shared edges once in the line budget, prioritize atoms in token alignment, and admit whole changed blocks before charging the shared token budget. Cover the algorithm and rendering regressions, and document the API v9 capture boundary. Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 59af03d commit a2beab0

8 files changed

Lines changed: 296 additions & 69 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,18 @@ $ node
428428
> eval(result.js_code);
429429
```
430430

431+
### Lambda debug outputs (API v9)
432+
433+
`comp.rescript.compileWithDebug(source)` returns `lambda` before the backend's
434+
Lambda optimization passes and `lambda_optimized` after the whole-term passes,
435+
before export grouping and JavaScript lowering. Both use the same Lambda printer
436+
so their text can be compared directly. JavaScript lowering performs further
437+
transformations; this snapshot is not the final JavaScript IR.
438+
439+
Regular `comp.rescript.compile(source)` does not capture these debug outputs.
440+
Older bundles do not provide `lambda_optimized`; consumers should treat it as
441+
optional when supporting multiple API versions.
442+
431443
### Testing the Playground bundle
432444

433445
Run `yarn workspace playground test` for a quick sanity check to see if all the build artifacts are working together correctly. When releasing the playground bundle, the test will always be executed before publishing to catch regressions.

‎packages/dev-playground/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"prepare-pages-site": "node scripts/prepare-pages-site.mjs",
1010
"res:build": "rescript",
1111
"res:watch": "rescript -w",
12-
"test": "rescript && node --test scripts/source-map-navigation.test.mjs scripts/pane-layout.test.mjs scripts/lambda-diff.test.mjs",
12+
"test": "rescript && node --test scripts/source-map-navigation.test.mjs scripts/pane-layout.test.mjs scripts/lambda-diff.test.mjs",
1313
"dev": "vite --host 127.0.0.1",
1414
"build": "rescript && vite build",
1515
"preview": "vite preview --host 127.0.0.1"

‎packages/dev-playground/scripts/lambda-diff.test.mjs‎

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from "node:assert/strict";
2+
import { register } from "node:module";
23
import test from "node:test";
4+
import * as Signal from "xote/src/Signal.res.mjs";
35
import {
46
compare,
57
maxCharacters,
@@ -8,6 +10,11 @@ import {
810
} from "../src/LambdaDiff.res.mjs";
911
import * as Inline from "../src/LambdaInlineDiff.res.mjs";
1012

13+
// Match vite.config.js: Xote and the playground use the published v12 runtime.
14+
register(new URL("./runtime-loader.mjs", import.meta.url));
15+
const LambdaView = await import("../src/LambdaView.res.mjs");
16+
const { renderToString } = await import("xote/src/SSR.res.mjs");
17+
1118
function changes(before, after) {
1219
const result = compare(before, after);
1320
assert.equal(result.TAG, "Changes");
@@ -96,7 +103,7 @@ test("bounds comparison work and output size", () => {
96103
});
97104

98105
test("trims common edges before applying the comparison budget", () => {
99-
const edge = "(unchanged)\n".repeat(2000);
106+
const edge = "(unchanged)\n".repeat(5000);
100107
const lines = changes(`${edge}before\n${edge}`, `${edge}after\n${edge}`);
101108
assert.equal(lines.filter(line => line.kind !== "Same").length, 2);
102109
});
@@ -108,6 +115,7 @@ test("collapsed regions preserve every line and keep context around changes", ()
108115
`${context}\nnew\n${context}\nnew\n${context}`,
109116
);
110117
const grouped = sections(lines);
118+
assert.ok(grouped.every(section => section._0.length > 0));
111119
assert.deepEqual(
112120
grouped.flatMap(section => section._0),
113121
lines,
@@ -174,6 +182,12 @@ test("inline diff handles token insertion, deletion and repeated tokens", () =>
174182
assert.deepEqual(changedText(inserted.after[1]), ["42"]);
175183
});
176184

185+
test("inline alignment prioritizes shared atoms over repeated punctuation", () => {
186+
const result = inlineChanges("((a", "a((");
187+
assert.deepEqual(changedText(result.before[1]), ["(", "("]);
188+
assert.deepEqual(changedText(result.after[1]), ["(", "("]);
189+
});
190+
177191
test("inline pairing skips unmatched lines in an uneven changed block", () => {
178192
const result = inlineChanges(
179193
"(unused z/9)\n (+ x/1 10)\n (- y/2 30)",
@@ -239,3 +253,117 @@ test("inline token work is budgeted across blocks and resets for each dump", ()
239253
assert.ok(Object.keys(result.before).length < 30);
240254
assert.deepEqual(inlineChanges(before, after), result);
241255
});
256+
257+
test("an oversized candidate block leaves the token budget for later changes", () => {
258+
const block = Array.from(
259+
{ length: 16 },
260+
(_, i) => `(apply f/${i} ${"x ".repeat(100)}10)`,
261+
).join("\n");
262+
const result = inlineChanges(
263+
`${block}\nseparator\n(+ value/1 10)`,
264+
`${block.replaceAll("10)", "20)")}\nseparator\n(+ value/1 20)`,
265+
);
266+
assert.deepEqual(Object.keys(result.before), ["18"]);
267+
assert.deepEqual(changedText(result.before[18]), ["10"]);
268+
assert.deepEqual(changedText(result.after[18]), ["20"]);
269+
});
270+
271+
function elements(node) {
272+
switch (node.TAG) {
273+
case "Element":
274+
return [node, ...node.children.flatMap(elements)];
275+
case "Fragment":
276+
return node._0.flatMap(elements);
277+
case "SignalFragment":
278+
return Signal.peek(node._0).flatMap(elements);
279+
case "LazyComponent":
280+
return elements(node._0());
281+
case "Keyed":
282+
return elements(node.child);
283+
default:
284+
return [];
285+
}
286+
}
287+
288+
function lambdaView(contextLines = 20) {
289+
const context = "unchanged\n".repeat(contextLines);
290+
const input = Signal.make({
291+
before: `${context}old\n${context}`,
292+
after: `${context}new\n${context}`,
293+
});
294+
const mode = Signal.make("Before");
295+
const visible = Signal.make(true);
296+
const view = LambdaView.make({
297+
input: () => Signal.get(input),
298+
mode,
299+
visible: () => Signal.get(visible),
300+
});
301+
const html = () => renderToString(() => view);
302+
const diff = () =>
303+
elements(view).find(
304+
node =>
305+
node.tag === "div" &&
306+
renderToString(() => node).startsWith('<div class="lambda-diff">'),
307+
);
308+
return { input, mode, visible, view, html, diff };
309+
}
310+
311+
test("Lambda view defers diff work and does not render collapsed lines", () => {
312+
const { mode, html, diff } = lambdaView(5000);
313+
assert.equal(diff(), undefined);
314+
Signal.set(mode, "Diff");
315+
assert.ok(diff());
316+
assert.equal(
317+
(html().match(/class="lambda-diff-line(?: |")/g) ?? []).length,
318+
8,
319+
);
320+
assert.equal((html().match(/<details/g) ?? []).length, 2);
321+
});
322+
323+
test("Lambda view retains its diff across identical compiles and tab switches", () => {
324+
const { input, mode, visible, diff } = lambdaView();
325+
Signal.set(mode, "Diff");
326+
const initial = diff();
327+
assert.ok(initial);
328+
Signal.set(input, { ...Signal.peek(input) });
329+
assert.equal(diff(), initial);
330+
Signal.set(mode, "Before");
331+
Signal.set(input, { ...Signal.peek(input), after: "changed while hidden" });
332+
assert.equal(diff(), initial);
333+
Signal.set(mode, "Diff");
334+
assert.notEqual(diff(), initial);
335+
const changed = diff();
336+
Signal.set(visible, false);
337+
Signal.set(visible, true);
338+
assert.equal(diff(), changed);
339+
});
340+
341+
test("expanded unchanged regions survive edits to a changed line", () => {
342+
const { input, mode, view, html } = lambdaView();
343+
Signal.set(mode, "Diff");
344+
const summary = elements(view).find(node => node.tag === "summary");
345+
const click = summary.events.find(([name]) => name === "click")[1];
346+
let prevented = false;
347+
click({
348+
preventDefault: () => {
349+
prevented = true;
350+
},
351+
});
352+
assert.equal(prevented, true);
353+
const openDetails = () =>
354+
(html().match(/<details[^>]* open(?: |>|=)/g) ?? []).length;
355+
assert.equal(openDetails(), 1);
356+
Signal.set(input, {
357+
...Signal.peek(input),
358+
after: Signal.peek(input).after.replace("new", "edited"),
359+
});
360+
assert.equal(openDetails(), 1);
361+
});
362+
363+
test("older bundles retain the optimized Lambda availability notice", () => {
364+
const { input, mode, html } = lambdaView();
365+
Signal.set(input, { ...Signal.peek(input), after: undefined });
366+
Signal.set(mode, "Diff");
367+
assert.match(html(), /does not provide optimized Lambda/);
368+
assert.doesNotMatch(html(), /class="lambda-diff"/);
369+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Keep Node rendering tests on the same runtime as vite.config.js.
2+
export function resolve(specifier, context, nextResolve) {
3+
if (specifier.startsWith("@rescript/runtime/")) {
4+
return nextResolve(
5+
new URL(`../node_modules/${specifier}`, import.meta.url).href,
6+
context,
7+
);
8+
}
9+
return nextResolve(specifier, context);
10+
}

‎packages/dev-playground/src/LambdaDiff.res‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ let compare = (before, after) => {
4646
}
4747
let rows = n - prefix.contents - suffix.contents
4848
let columns = m - prefix.contents - suffix.contents
49-
if n + m > maxLines || (rows + 1) * (columns + 1) > maxCells {
49+
50+
// Shared edges appear once in the output, not once for each input.
51+
if (
52+
n + m - prefix.contents - suffix.contents > maxLines || (rows + 1) * (columns + 1) > maxCells
53+
) {
5054
TooLarge
5155
} else {
5256
// Longest common subsequence on the changed middle only. Keeping the
@@ -151,9 +155,13 @@ let sections = lines => {
151155
if tail - head <= 3 {
152156
result->Array.push(Visible(Array.slice(lines, ~start, ~end=end_)))
153157
} else {
154-
result->Array.push(Visible(Array.slice(lines, ~start, ~end=head)))
158+
if head > start {
159+
result->Array.push(Visible(Array.slice(lines, ~start, ~end=head)))
160+
}
155161
result->Array.push(Collapsed(Array.slice(lines, ~start=head, ~end=tail)))
156-
result->Array.push(Visible(Array.slice(lines, ~start=tail, ~end=end_)))
162+
if tail < end_ {
163+
result->Array.push(Visible(Array.slice(lines, ~start=tail, ~end=end_)))
164+
}
157165
}
158166
}
159167
}

‎packages/dev-playground/src/LambdaInlineDiff.res‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Inline refinement runs only inside consecutive Removed/Added line blocks.
2-
// For each candidate line pair, an LCS of non-whitespace Lambda tokens marks
2+
// For each candidate line pair, an LCS prioritizing Lambda atoms marks
33
// the shared tokens. Pairing requires shared atoms (not just punctuation) to
44
// cover at least half the atom count of the larger line. A second dynamic
55
// programming table chooses the highest-scoring order-preserving line pairs,
66
// allowing unmatched lines on either side. Unmatched tokens in chosen pairs
77
// receive stronger shading; whitespace and the original text stay intact.
88
// Oversized or dissimilar pairs keep whole-line highlighting. Token comparison
9-
// cells share a budget across the entire dump, and block size caps line-pairing
10-
// work, so this refinement cannot introduce unbounded quadratic comparisons.
9+
// cells share a budget across the entire dump. Admit whole blocks before doing
10+
// token comparisons, so an oversized block cannot consume the budget partially
11+
// and starve later, smaller changes. Block size also caps line-pairing work.
1112
type part = {text: string, changed: bool}
1213
type highlights = {before: Dict.t<array<part>>, after: Dict.t<array<part>>}
1314
type tokens = {all: array<string>, significant: array<string>, atoms: int}
@@ -71,11 +72,10 @@ let compareTokens = (left, right, budget) => {
7172
let j = ref(m - 1)
7273
while j.contents >= 0 {
7374
let cell = i.contents * stride + j.contents
74-
table[cell] = if (
75-
left.significant->Array.getUnsafe(i.contents) ===
76-
right.significant->Array.getUnsafe(j.contents)
77-
) {
78-
1 + table->Array.getUnsafe(cell + stride + 1)
75+
let token = left.significant->Array.getUnsafe(i.contents)
76+
table[cell] = if token === right.significant->Array.getUnsafe(j.contents) {
77+
// One shared atom outweighs every possible punctuation match.
78+
(isAtom(token) ? maxTokens + 1 : 1) + table->Array.getUnsafe(cell + stride + 1)
7979
} else {
8080
Math.Int.max(table->Array.getUnsafe(cell + stride), table->Array.getUnsafe(cell + 1))
8181
}
@@ -121,6 +121,15 @@ let compareTokens = (left, right, budget) => {
121121
}
122122
}
123123
124+
let tokenRows = tokens =>
125+
tokens->Array.reduce(0, (sum, tokens) =>
126+
sum +
127+
switch tokens {
128+
| Some({significant, atoms}) if atoms > 0 => Array.length(significant) + 1
129+
| Some(_) | None => 0
130+
}
131+
)
132+
124133
let highlight = (lines: array<LambdaDiff.line>): highlights => {
125134
let result = {before: Dict.make(), after: Dict.make()}
126135
let budget = ref(maxCells)
@@ -142,9 +151,9 @@ let highlight = (lines: array<LambdaDiff.line>): highlights => {
142151
let after = block->Array.filter(line => line.kind === Added)
143152
let n = Array.length(before)
144153
let m = Array.length(after)
145-
if n > 0 && m > 0 {
146-
let left = before->Array.map(line => tokenize(line.text))
147-
let right = after->Array.map(line => tokenize(line.text))
154+
let left = before->Array.map(line => tokenize(line.text))
155+
let right = after->Array.map(line => tokenize(line.text))
156+
if n > 0 && m > 0 && tokenRows(left) * tokenRows(right) <= budget.contents {
148157
let candidates = Array.make(~length=n * m, None)
149158
for i in 0 to n - 1 {
150159
for j in 0 to m - 1 {

0 commit comments

Comments
 (0)