The page has one head for whatever node is the page (crumbs from the library's title, a byline of
sections and nodes, its flags and ⋯); in panes the first pane is the page and every other pane has
a section's head, whatever node it shows. A prompt (a node whose sections cite its sentences) is
folded wherever it shows. The library root's sections open in place like any others; an address
outside the hoisted node un-hoists to the library, and Esc at a hoisted page's top steps out to its
parent's.
What acts on a node and everything under it acts on the node the reader is on (the focused
section, or the one a focused note or selection is in): M moves it under any node (the jump box as
a picker, the root offered too), /duplicate copies it just after itself, /export saves it, /import
reads a file in as its last child (place() prefixes the titles down to the parent), /linkhere scans
it, S lists its links (on the root, the whole library's), history's f filters to it and r reverts
it. Every node's ⋯ offers them.
The documents menu, its mode and keys, /new, /open, /rename and /linkdoc are gone; m is free and
the top bar's left is a plain label with the library's name. Addresses are /n/<node id>, and / the
root; an old /doc/ address shows the library.
Server: /api/docs gives way to /api/nodes/<id>/{tree,export,import}; link jobs take scope "node" or
"library" and pending lists ?node=<id>&subtree=1. Step-1 shims removed: docOf/documents, all of
core/library.ts (IMPORT_MAX and importTree move to seed.ts, address and resolve to address.ts),
docArg, and the old single-document replica check. revertDocTo is revertSubtreeTo.
The page scrolls the focus into view only when the focus, the page or the mode changed, not on a
render for something that arrived in the background; row flags give way on a narrow screen.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
109 lines
5.4 KiB
TypeScript
109 lines
5.4 KiB
TypeScript
/**
|
|
* A library of about 5,000 nodes in 100 documents: an edit must cost what it touches, not the size
|
|
* of the library. Timings are printed; the assertions are budgets well inside one 16 ms frame for
|
|
* the work a keystroke does, and a comparison with a library a tenth the size.
|
|
*/
|
|
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
import { LibraryStore } from "../server/library";
|
|
import { linkIndex } from "../src/core/links";
|
|
import { Doc, isStale, staleness, type NodeId, type Op } from "../src/core/model";
|
|
import { renderContext } from "../src/llm/prompts";
|
|
|
|
const SENTENCE = "This sentence is about something that matters here. Another follows it, with a few more words. ";
|
|
|
|
/** `docs` documents of 50 nodes each: 7 sections of 6 leaves, and a note. */
|
|
function synthetic(docs: number): Doc {
|
|
const doc = Doc.library("scale");
|
|
const lib = doc.snap().root;
|
|
for (let d = 0; d < docs; d++) {
|
|
const ops: Op[] = [{ op: "create", parent: lib, title: `Document ${d}`, body: SENTENCE.repeat(3) }];
|
|
let n = 1;
|
|
for (let s = 0; s < 7; s++) {
|
|
const sec = n++;
|
|
ops.push({ op: "create", parent: "$0" as NodeId, title: `Section ${d}.${s}`, body: SENTENCE.repeat(2) });
|
|
for (let k = 0; k < 6; k++, n++) ops.push({ op: "create", parent: `$${sec}` as NodeId, title: `Leaf ${d}.${s}.${k}`, body: SENTENCE.repeat(2) });
|
|
}
|
|
ops.push({ op: "create", parent: "$0" as NodeId, kind: "note", quote: "something that matters", body: "A note." });
|
|
// And a link to an article, shared by every tenth document, for the backlinks index to follow.
|
|
ops.push({ op: "create", parent: "$0" as NodeId, kind: "note", quote: "Another follows", ref: { kind: "wiki", title: `Topic ${d % 10}`, lang: "en" } });
|
|
for (let i = 0; i < n; i++) ops.push({ op: "bless", id: `$${i}` as NodeId });
|
|
doc.apply(ops, `Document ${d}`, { actor: "llm" });
|
|
}
|
|
return doc;
|
|
}
|
|
|
|
const median = (xs: number[]) => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)];
|
|
|
|
/** Time a body edit end to end: the op, its commit (with the state hash), the snapshot, staleness along the path, and the context. */
|
|
function editCost(doc: Doc, rounds = 25) {
|
|
const snap = doc.snap();
|
|
const leaves = Object.values(snap.nodes).filter((n) => n.title.startsWith("Leaf"));
|
|
const ms: number[] = [];
|
|
for (let i = 0; i < rounds; i++) {
|
|
const leaf = leaves[(i * 977) % leaves.length];
|
|
const t = performance.now();
|
|
doc.apply([{ op: "update", id: leaf.id, body: `Edited ${i}. ${SENTENCE}` }], "Edit");
|
|
const s = doc.snap();
|
|
for (let p: NodeId | null = leaf.id; p; p = s.nodes[p].parent) staleness(s, p);
|
|
renderContext(s, leaf.id);
|
|
ms.push(performance.now() - t);
|
|
}
|
|
return median(ms);
|
|
}
|
|
|
|
describe("a large library", () => {
|
|
it("costs what an edit touches: 5,000 nodes in 100 documents", () => {
|
|
let t = performance.now();
|
|
const big = synthetic(100);
|
|
const built = performance.now() - t;
|
|
const nodes = Object.keys(big.snap().nodes).length;
|
|
expect(nodes).toBeGreaterThanOrEqual(5000);
|
|
|
|
// Boot: what a browser or the server does with the stored library.
|
|
const bytes = big.snapshotBytes();
|
|
t = performance.now();
|
|
const loaded = Doc.load(bytes, "reader");
|
|
const first = loaded.snap();
|
|
const boot = performance.now() - t;
|
|
t = performance.now();
|
|
loaded.stateHash();
|
|
const firstHash = performance.now() - t;
|
|
expect(Object.keys(first.nodes)).toHaveLength(nodes);
|
|
expect(Object.keys(first.nodes).filter((id) => isStale(first, id as NodeId))).toEqual([]);
|
|
|
|
// Navigation reads the snapshot without changing it: it must be the same object, at no cost.
|
|
t = performance.now();
|
|
for (let i = 0; i < 1000; i++) loaded.snap();
|
|
const reread = (performance.now() - t) / 1000;
|
|
expect(loaded.snap()).toBe(first);
|
|
|
|
// Server start: open the stored library and list its documents, as server/index.ts does.
|
|
const data = mkdtempSync(join(tmpdir(), "ie-scale-"));
|
|
mkdirSync(join(data, "library"));
|
|
writeFileSync(join(data, "library", "main.loro"), bytes);
|
|
t = performance.now();
|
|
const store = new LibraryStore(data, () => {}).open();
|
|
expect(store.titles()).toHaveLength(100);
|
|
const serverStart = performance.now() - t;
|
|
|
|
// The backlinks index follows every edit below; it is built once, here.
|
|
t = performance.now();
|
|
const ix = linkIndex(loaded);
|
|
const indexed = performance.now() - t;
|
|
expect(ix.mentions("en", "Topic 3")).toHaveLength(10);
|
|
const edit = editCost(loaded);
|
|
expect(ix.mentions("en", "Topic 3")).toHaveLength(10);
|
|
const small = editCost(Doc.load(synthetic(10).snapshotBytes(), "reader"));
|
|
console.log(
|
|
`scale: ${nodes} nodes, ${(bytes.length / 1024).toFixed(0)} KB · built in ${built.toFixed(0)} ms · loaded and read in ${boot.toFixed(0)} ms · first state hash ${firstHash.toFixed(0)} ms · server store opened and listed in ${serverStart.toFixed(0)} ms · cached snapshot ${(reread * 1000).toFixed(1)} µs · backlinks index built in ${indexed.toFixed(1)} ms · body edit (op, commit, snapshot, staleness, context) median ${edit.toFixed(2)} ms at 100 documents, ${small.toFixed(2)} ms at 10`,
|
|
);
|
|
expect(edit).toBeLessThan(8);
|
|
// An edit at a tenth of the size costs about the same: nothing it does walks the whole library.
|
|
expect(edit).toBeLessThan(small * 3 + 2);
|
|
expect(boot).toBeLessThan(2000);
|
|
}, 120_000);
|
|
});
|