[K4][G10] Merge: Aufräumen (tote TS-/Rust-Doppelspuren), id-Index, Commit ohne Kopie, Punkttyp im Kern; Tests unter Last stabil

CLAUDE.md: Dokument im Kern, Modulkarte nachgeführt.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-29 13:21:24 +00:00
commit 34e77b607c
53 files changed
+1293 -1043

No files matched your search

+3 -3
View File
@@ -236,7 +236,7 @@ export class ProjectDocument {
}
return { problems };
}
const project = applyOps(base, ops);
const project = applyOps(base, ops, core().doc_last_hits(this.handle));
this.mirror = project;
return { project };
}
@@ -255,7 +255,7 @@ export class ProjectDocument {
const r = fromMarkedJson<{ ops: CoreOp[]; notice?: string } | { problems: string[] }>(out);
if ("problems" in r) return r;
if (r.ops.length === 0) return { project: base, ops: r.ops, notice: r.notice };
const project = applyOps(base, r.ops);
const project = applyOps(base, r.ops, core().doc_last_hits(this.handle));
this.mirror = project;
return { project, ops: r.ops, notice: r.notice };
}
@@ -338,7 +338,7 @@ export class ProjectDocument {
}
const json = core()[fn](this.handle);
if (!json) return null;
const project = applyOps(base, JSON.parse(json) as CoreOp[]);
const project = applyOps(base, JSON.parse(json) as CoreOp[], core().doc_last_hits(this.handle));
this.mirror = project;
return project;
}
+1 -1
View File
@@ -18,7 +18,7 @@ export { exportObj, exportStl, exportGlb, exportGltf } from "../export/exportMes
export { exportProjectStep } from "../export/exportStep";
export { planToSvgString } from "../export/planToSvgString";
export { setWasmBytesProvider } from "../engine/wasmSource";
export { initCore, coreReady } from "../engine/coreWasm";
export { initCore } from "../engine/coreWasm";
export { applyOps, validateProject, validateOps } from "./ops";
export { projectWarnings } from "./ops";
export type { CoreOp } from "./ops";
-4
View File
@@ -9,7 +9,6 @@ export {
bulgeArc,
bulgeArcPoints,
bulgePolylinePoints,
bulgeBetweenAngles,
bulgeFromSagittaPoint,
bulgePointAt,
bulgeThroughPoint,
@@ -80,7 +79,6 @@ export {
} from "../geometry/lineSegments";
export {
openingVerticalExtent,
} from "../geometry/opening";
export { autoExteriorDimensions } from "../geometry/autoDimensions";
export * as polygonBool from "../geometry/polygonBool";
@@ -95,7 +93,6 @@ export {
type PolylineVertexType,
canSetVertexType,
vertexTypeOf,
withVertexType,
} from "../geometry/polylineVertexType";
export {
@@ -238,7 +235,6 @@ export {
meetScale,
mmToPx,
pointInPolygon,
pointSegDistPx,
printStrokeVb,
scaleFromView,
toScreen,
+41
View File
@@ -128,3 +128,44 @@ describe("applyOps (schnelle Fassung) = Referenz", () => {
expect(out.walls).toEqual([{ id: "a", z: 1 }, { id: "b" }, { id: "c" }]);
});
});
describe("applyOps: id-Index über die Aufrufe hinweg", () => {
it("Ketten und Verzweigungen grosser Listen (geteilter Index) = Referenz", () => {
const walls = Array.from({ length: 200 }, (_, i) => ({ ...sampleProject.walls[0], id: `w${i}` }));
const big: Project = { ...sampleProject, walls, drawings2d: [] };
const r = rng(7);
const history: Project[] = [big];
for (let step = 0; step < 600; step++) {
// Meist auf dem letzten Stand weiter, manchmal auf einem älteren (Undo-Basis, fremde Projekte).
const base = r() < 0.7 ? history[history.length - 1] : history[Math.floor(r() * history.length)];
const ids = ["w0", "w5", "w199", "w150", `n${Math.floor(r() * 40)}`, `n${Math.floor(r() * 40)}`];
const ops: CoreOp[] = [];
for (let k = 0, n = 1 + Math.floor(r() * 4); k < n; k++) {
const x = r();
const id = ids[Math.floor(r() * ids.length)];
if (x < 0.6) ops.push({ kind: "replace", collection: "walls", entity: { id, v: step } });
else if (x < 0.8) ops.push({ kind: "add", collection: "walls", items: [{ id: `n${Math.floor(r() * 40)}` }] });
else if (x < 0.9) ops.push({ kind: "add", collection: "walls", items: [{ id }], at: Math.floor(r() * 300) });
else ops.push({ kind: "remove", collection: "walls", ids: [id] });
}
// Positions-Hinweise des Kerns (erstes Vorkommen, wie `Document::last_hits`): richtig, auf ein
// anderes Element, ausserhalb oder fehlend — das Ergebnis bleibt gleich.
let hits: number[] | undefined;
if (r() < 0.6) {
hits = [];
let cur = base;
for (const op of ops) {
const list = cur.walls as unknown as Entity[];
const first = op.kind === "replace" ? list.findIndex((e) => e.id === op.entity.id) : -1;
const other = list.findIndex((e, i) => i >= Math.floor(r() * list.length) && op.kind === "replace" && e.id !== op.entity.id);
const x = r();
hits.push(x < 0.6 ? first : x < 0.8 ? other : x < 0.9 ? list.length + 3 : -1);
cur = referenceApplyOps(cur, [op]);
}
}
const a = applyOps(base, ops, hits);
expect(JSON.stringify(a.walls)).toBe(JSON.stringify(referenceApplyOps(base, ops).walls));
history.push(a);
}
});
});
+76 -35
View File
@@ -37,25 +37,79 @@ export type CoreOp =
/**
* Wendet eine Op-Folge rein an (kein Zugriff auf Store/UI). Unberührte Listen und Elemente behalten
* ihre Identität. Innerhalb eines Aufrufs wird jede Liste höchstens einmal kopiert und für
* `replace`-Serien (Auswahl verschieben: hunderte Elemente) über einen id-Index angesprochen —
* Ergebnis gleich der einfachen Fassung (Test `ops.apply.test.ts`).
* ihre Identität. Innerhalb eines Aufrufs wird jede Liste höchstens einmal kopiert. `replace` findet
* sein Element über `hits` (je Op die Position, die der Kern beim Anwenden getroffen hat,
* `ProjectDocument`; wird nachgeprüft) oder sonst über {@link indexOfId} — ein Griff-Schritt an einer
* grossen Liste sucht so nicht linear. Ergebnis gleich der einfachen Fassung (Test `ops.apply.test.ts`).
*/
export function applyOps(project: Project, ops: readonly CoreOp[]): Project {
export function applyOps(project: Project, ops: readonly CoreOp[], hits?: ArrayLike<number>): Project {
if (ops.length === 0) return project;
const st: ApplyState = { cur: { ...(project as unknown as Record<string, unknown>) }, owned: new Set(), index: new Map() };
for (const op of ops) applyOne(st, op);
const st: ApplyState = { cur: { ...(project as unknown as Record<string, unknown>) }, owned: new Set(), hit: -1 };
for (let k = 0; k < ops.length; k++) {
st.hit = hits?.[k] ?? -1;
applyOne(st, ops[k]);
}
return st.cur as unknown as Project;
}
/** Zustand eines `applyOps`-Aufrufs: `cur` ist eine eigene Kopie der obersten Ebene; Listen in
* `owned` wurden in diesem Aufruf neu angelegt (dürfen verändert werden), `index` = id → Position
* (erstes Vorkommen) solcher Listen. */
* `owned` wurden in diesem Aufruf neu angelegt (dürfen verändert werden); `hit` = vermutete Position
* des Elements der laufenden `replace`-Op (−1 = keine). */
interface ApplyState {
cur: Record<string, unknown>;
owned: Set<string>;
/** `null` = einmal linear gesucht, der Index wird bei der nächsten Suche gebaut. */
index: Map<string, Map<string, number> | null>;
hit: number;
}
// ── id-Index der Listen ──────────────────────────────────────────────────────
/**
* id → Position (erstes Vorkommen) je Liste, für Suchen ohne Positions-Hinweis des Kerns (Op-Bauer,
* `findItem`). Listen der Leseansicht ändern sich nie (jede Änderung legt eine neue an); eine Kopie mit
* denselben Positionen (`replace`, `add` am Ende) übernimmt den Index der Vorlage samt der neuen ids.
* Teilen sich so mehrere Listen einen Index, enthält er die ids jeder von ihnen; ein Treffer wird an
* der Liste nachgeprüft (sonst lineare Suche), kein Eintrag heisst „nicht enthalten". Der Index kostet
* in V8 etwa das Zwanzigfache einer linearen Suche (20 000 Elemente ≈ 6 ms): gebaut wird er erst bei
* der zweiten Suche in derselben Listen-Linie (`null` = einmal linear gesucht).
*/
const ID_INDEX = new WeakMap<readonly unknown[], Map<unknown, number> | null>();
/** Ab dieser Länge wird ein Index gebaut (darunter ist die lineare Suche billiger). */
const INDEX_MIN_LEN = 32;
/** Position des ersten Elements mit `id` in `list` (−1 = keins); Index je Listen-Linie gemerkt. */
export function indexOfId(list: readonly unknown[], id: unknown): number {
const linear = () => list.findIndex((e) => (e as Entity | null)?.id === id);
if (list.length < INDEX_MIN_LEN) return linear();
let idx = ID_INDEX.get(list);
if (idx === undefined) {
ID_INDEX.set(list, null);
return linear();
}
if (idx === null) {
idx = new Map();
for (let i = 0; i < list.length; i++) {
const k = (list[i] as Entity | null)?.id;
if (!idx.has(k)) idx.set(k, i);
}
ID_INDEX.set(list, idx);
}
const i = idx.get(id);
if (i === undefined) return -1;
return (list[i] as Entity | null)?.id === id ? i : linear();
}
/** `copy` hat dieselben Positionen wie `src` (plus Elemente am Ende): Index (bzw. Such-Marke) übernehmen. */
function shareIndex(src: readonly unknown[], copy: readonly unknown[]): void {
const idx = ID_INDEX.get(src);
if (idx === undefined) return;
if (idx !== null) {
for (let i = src.length; i < copy.length; i++) {
const k = (copy[i] as Entity | null)?.id;
if (!idx.has(k)) idx.set(k, i);
}
}
ID_INDEX.set(copy, idx);
}
function listOf(cur: Record<string, unknown>, collection: string): Entity[] {
@@ -63,11 +117,10 @@ function listOf(cur: Record<string, unknown>, collection: string): Entity[] {
return Array.isArray(v) ? (v as Entity[]) : [];
}
/** Setzt `collection` auf eine neue (eigene) Liste; ein vorhandener Index verfällt. */
/** Setzt `collection` auf eine neue (eigene) Liste. */
function putList(st: ApplyState, collection: string, list: Entity[]): void {
st.cur[collection] = list;
st.owned.add(collection);
st.index.delete(collection);
}
function applyOne(st: ApplyState, op: CoreOp): void {
@@ -76,7 +129,6 @@ function applyOne(st: ApplyState, op: CoreOp): void {
case "setField": {
// Der Wert gehört dem Aufrufer: nie verändern.
st.owned.delete(op.key);
st.index.delete(op.key);
if (op.value !== undefined && op.at !== undefined && !(op.key in cur)) {
// Neues Feld an Position `at` (wie `shift_insert` im Kern).
const entries = Object.entries(cur);
@@ -91,7 +143,9 @@ function applyOne(st: ApplyState, op: CoreOp): void {
case "add": {
const list = listOf(cur, op.collection);
const at = op.at === undefined ? list.length : Math.max(0, Math.min(list.length, op.at));
putList(st, op.collection, [...list.slice(0, at), ...op.items, ...list.slice(at)]);
const next = [...list.slice(0, at), ...op.items, ...list.slice(at)];
if (at === list.length) shareIndex(list, next);
putList(st, op.collection, next);
return;
}
case "replace": {
@@ -99,30 +153,17 @@ function applyOne(st: ApplyState, op: CoreOp): void {
let list: Entity[];
if (st.owned.has(c)) list = cur[c] as Entity[];
else {
list = listOf(cur, c).slice();
const src = listOf(cur, c);
list = src.slice();
shareIndex(src, list);
putList(st, c, list);
}
// Erste Suche in dieser Liste linear (ein Drag-Schritt), ab der zweiten über den Index.
let idx = st.index.get(c);
let at: number | undefined;
if (idx === undefined) {
st.index.set(c, null);
const i = list.findIndex((e) => e?.id === op.entity.id);
at = i < 0 ? undefined : i;
} else {
if (idx === null) {
idx = new Map();
for (let i = 0; i < list.length; i++) {
const id = list[i]?.id;
if (!idx.has(id)) idx.set(id, i);
}
st.index.set(c, idx);
}
at = idx.get(op.entity.id);
}
if (at === undefined) {
idx?.set(op.entity.id, list.length);
const h = st.hit;
const at = h >= 0 && h < list.length && list[h]?.id === op.entity.id ? h : indexOfId(list, op.entity.id);
if (at < 0) {
list.push(op.entity);
const idx = ID_INDEX.get(list);
if (idx && !idx.has(op.entity.id)) idx.set(op.entity.id, list.length - 1);
} else list[at] = op.entity;
return;
}
+4 -2
View File
@@ -9,7 +9,7 @@
// {@link opsMutation} die Ops mit — `setProject` nimmt dann die Ops ohne Differenz.
import type { Project } from "../model/types";
import { applyOps } from "./ops";
import { applyOps, indexOfId } from "./ops";
import type { CoreOp } from "./ops";
/** Projekt-Felder, die Listen von Elementen mit `id` sind (walls, drawings2d, rooms, …). */
@@ -34,7 +34,9 @@ export function itemsOf<K extends EntityCollection>(p: Project, collection: K):
/** Element `id` aus `collection` (erstes Vorkommen). */
export function findItem<K extends EntityCollection>(p: Project, collection: K, id: string): EntityOf<K> | undefined {
return itemsOf(p, collection).find((e) => (e as { id: string }).id === id);
const list = itemsOf(p, collection);
const i = indexOfId(list, id);
return i < 0 ? undefined : list[i];
}
/** Ersetzt ein Element (gleiche id, Position bleibt). */