[K4][K3d] Plan-Cache im Kern: Zeichnungen in Stücken, übrige Domänen gemerkt, schnelles erstes Öffnen

generatePlan erzeugt Unverändertes nicht mehr neu: plan/cache.rs hält je
Argument-Satz die übrigen Domänen als fertige Abschnitte (behaltene
String-/Schraffur-Tabellen) und die 2D-Zeichnungen als inhaltsbestimmte
Stücke mit eigenem Puffer. planDomains liefert nur die Stücke, die TS
noch nicht hat (have); ein geändertes Stück kopiert seine unveränderten
Elemente fertig geschrieben (plan_buf::Kept). Dokument: Element-Stände
(Revs), typisierte Sicht nach replace elementweise (model/items.rs),
id-Positionen über Aufrufe gemerkt; fieldOps mit Schnellweg für an Ort
ersetzte Elemente. WASM-Heap wächst in grossen Schritten (wasm_alloc.rs),
gehaltene Puffer eines Plans fallen in einer Runde nicht weg.

Gleichheit: Kern-Test mit/ohne Cache über Zufallsänderungen, CLI
byte-gleich (45 Ausgaben). katbig nach Änderung 55 -> 6 ms (TS alt 13 ms),
Kataster 150k 862 -> 39 ms, erstes Öffnen 200k Zeichnungen 9,6 -> 1,8 s.
Nicht in der Tauri-App angesehen.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-29 06:56:39 +00:00
1 parent 09a1b30f2b
commit 223816966d
21 files changed
+2012 -169

No files matched your search

+17
View File
@@ -126,6 +126,23 @@ describe("Op-Bauhelfer", () => {
expect(fieldOps(sampleProject, "geoAnchor", undefined)).toEqual(projectDiffOps(sampleProject, { ...sampleProject, geoAnchor: undefined }));
});
it("fieldOps: an Ort und Stelle ersetzte Elemente = nur replace; doppelte ids = ganzes Feld", () => {
const ws = sampleProject.walls;
const next = ws.map((w, i) => (i === 1 || i === 3 ? { ...w, height: 9 } : w));
expect(fieldOps(sampleProject, "walls", next)).toEqual([
{ kind: "replace", collection: "walls", entity: next[1] },
{ kind: "replace", collection: "walls", entity: next[3] },
]);
// Nochmals vom Ergebnis aus (Eindeutigkeit gemerkt) und mit anderer id an einer Stelle.
const p2 = { ...sampleProject, walls: next };
const moved = next.map((w, i) => (i === 2 ? { ...w, id: "neu" } : w));
expect(fieldOps(p2, "walls", moved)).toEqual(projectDiffOps(p2, { ...p2, walls: moved }));
const dup = [ws[0], { ...ws[0] }];
const p3 = { ...sampleProject, walls: dup };
const dupNext = [ws[0], { ...ws[0], height: 1 }];
expect(fieldOps(p3, "walls", dupNext)).toEqual([{ kind: "setField", key: "walls", value: dupNext }]);
});
it("opsMutation: wirkt wie eine Projekt → Projekt-Funktion und trägt die Ops mit", () => {
const id = sampleProject.walls[0].id;
const m = opsMutation((p) => patchItem(p, "walls", id, { height: 4.2 }));
+35 -1
View File
@@ -192,10 +192,44 @@ export function fieldOps<K extends keyof Project>(p: Project, key: K, value: Pro
const va = p[key] as unknown;
const vb = value as unknown;
if (va === vb) return [];
if (isIdList(va) && isIdList(vb) && uniqueIds(va) && uniqueIds(vb)) return diffList(key, va, vb);
// Häufigster Fall (eine Änderung in einer grossen Liste): Elemente an Ort und Stelle ersetzt,
// gleiche ids — ein Durchgang ohne Mengen und Kopien (10 000 Zeichnungen: ~18 → ~1 ms).
if (Array.isArray(vb) && Array.isArray(va) && va.length === vb.length && uniqueIdList(va)) {
const changed = replacedInPlace(va as AnyEntity[], vb);
if (changed) {
knownUnique.set(vb, true);
return changed.map((entity) => ({ kind: "replace", collection: key, entity }));
}
}
if (uniqueIdList(va) && uniqueIdList(vb)) return diffList(key, va as AnyEntity[], vb as AnyEntity[]);
return [vb === undefined ? { kind: "setField", key } : { kind: "setField", key, value: vb }];
}
/** Liste → ob sie eine id-Liste mit eindeutigen ids ist (Projektdaten werden nicht verändert). */
const knownUnique = new WeakMap<object, boolean>();
function uniqueIdList(v: unknown): boolean {
if (!Array.isArray(v)) return false;
let r = knownUnique.get(v);
if (r === undefined) {
r = isIdList(v) && uniqueIds(v);
knownUnique.set(v, r);
}
return r;
}
/** Die ersetzten Elemente, wenn `vb` sich von `va` nur durch Elemente mit gleicher id an gleicher Stelle unterscheidet, sonst `null`. */
function replacedInPlace(va: AnyEntity[], vb: unknown[]): AnyEntity[] | null {
const out: AnyEntity[] = [];
for (let i = 0; i < va.length; i++) {
const b = vb[i];
if (b === va[i]) continue;
if (b === null || typeof b !== "object" || (b as AnyEntity).id !== va[i].id) return null;
out.push(b as AnyEntity);
}
return out;
}
const isEntityList = (v: unknown): v is AnyEntity[] =>
Array.isArray(v) && v.length > 0 && v.every((x) => x !== null && typeof x === "object" && typeof (x as AnyEntity).id === "string");