perf/fix v1.43.1: external audit P1 — render cost, drag threshold, geometry, backend

L1: memoized space model + open pairs (structural fingerprint key, epoch
bumped synchronously at mutation time, not inside the debounce); hoisted
per-room geometry out of the render loop; smoke asserts zero recomputation
across state pushes.

L4: openings get the 3 px drag threshold used by every other pipeline and
only write when the geometry actually changed — taps open the dialog again.

G2: interiorPoint() replaces the vertex mean, so island rooms inside
concave (U/L) parents are accepted and their evenodd holes render; traced
duplicates still are not containment.

G3: segKey rounds before ordering — one shared wall, one key.

B2: _check_write fails closed when the entry is unavailable.
B3: layout/set honours expected_rev and returns the new rev.
B4: config/set without expected_rev over a non-empty store logs a warning.
B5: coordinates reject NaN/Infinity; spaces/rooms/markers/decor/layout capped.

+2 unit tests (118), +2 backend tests (14), smoke_render_perf; docs
same-commit
This commit is contained in:
Matysh
2026-07-27 10:58:18 +03:00
parent 0fd0ba408d
commit 49b0cb4e05
16 changed files with 445 additions and 160 deletions
+44 -13
View File
@@ -38,8 +38,14 @@ export function formatLength(cm: number, imperial: boolean): string {
* normalized (0..1) coordinates need more (see roomEdges).
*/
export function segKey(a: number[], b: number[], prec = 1): string {
const [p, q] = a[0] < b[0] || (a[0] === b[0] && a[1] <= b[1]) ? [a, b] : [b, a];
return `${p[0].toFixed(prec)},${p[1].toFixed(prec)}-${q[0].toFixed(prec)},${q[1].toFixed(prec)}`;
// audit G3: ROUND FIRST, then order. Ordering on raw floats while printing
// rounded ones let one wall produce two different keys, so shared walls were
// emitted twice and the dedup invariant quietly broke.
const ax = a[0].toFixed(prec), ay = a[1].toFixed(prec);
const bx = b[0].toFixed(prec), by = b[1].toFixed(prec);
const first = ax < bx || (ax === bx && ay <= by);
const [px, py, qx, qy] = first ? [ax, ay, bx, by] : [bx, by, ax, ay];
return `${px},${py}-${qx},${qy}`;
}
/**
@@ -224,6 +230,36 @@ export function segmentsProperlyCross(
}
/** Is any area of outline `a` strictly inside `b`? Also catches nested and duplicate outlines. */
/**
* A point guaranteed to lie strictly inside the polygon (audit G2).
* The arithmetic mean of the vertices lies OUTSIDE concave shapes (U/L rooms
* are common in hand-drawn plans), which made containment tests misfire.
* Strategy: try the midpoints of the diagonals from each vertex, then a
* triangle centroid of consecutive vertices — the first point that passes
* pointStrictlyInside wins.
*/
export function interiorPoint(poly: number[][], eps = 1e-6): number[] | null {
if (!poly || poly.length < 3) return null;
const n = poly.length;
const mean = [
poly.reduce((s, p) => s + p[0], 0) / n,
poly.reduce((s, p) => s + p[1], 0) / n,
];
if (pointStrictlyInside(mean, poly, eps)) return mean;
for (let i = 0; i < n; i++) {
// centroid of the ear at vertex i
const a = poly[(i - 1 + n) % n], b = poly[i], c = poly[(i + 1) % n];
const cand = [(a[0] + b[0] + c[0]) / 3, (a[1] + b[1] + c[1]) / 3];
if (pointStrictlyInside(cand, poly, eps)) return cand;
}
for (let i = 0; i < n; i++)
for (let j = i + 2; j < n; j++) {
const cand = [(poly[i][0] + poly[j][0]) / 2, (poly[i][1] + poly[j][1]) / 2];
if (pointStrictlyInside(cand, poly, eps)) return cand;
}
return null;
}
function coversArea(a: number[][], b: number[][], eps: number): boolean {
let allOnBoundary = true;
for (const v of a) {
@@ -232,11 +268,8 @@ function coversArea(a: number[][], b: number[][], eps: number): boolean {
}
// every vertex sits on b's outline → a duplicate or traced outline: probe the middle
if (allOnBoundary) {
const c = [
a.reduce((s, p) => s + p[0], 0) / a.length,
a.reduce((s, p) => s + p[1], 0) / a.length,
];
return pointStrictlyInside(c, b, eps);
const c = interiorPoint(a, eps); // audit G2: NOT the vertex mean
return !!c && pointStrictlyInside(c, b, eps);
}
return false;
}
@@ -250,12 +283,10 @@ export function polyContainsPoly(outer: number[][], inner: number[][], eps = 1e-
return false;
for (const v of inner)
if (!pointStrictlyInside(v, outer, eps) && !pointOnBoundary(v, outer, eps)) return false;
// identical/traced outlines are NOT containment — probe the centroid strictness both ways
const c = [
inner.reduce((s, p) => s + p[0], 0) / inner.length,
inner.reduce((s, p) => s + p[1], 0) / inner.length,
];
return pointStrictlyInside(c, outer, eps) && polygonArea(inner) < polygonArea(outer) - eps;
// identical/traced outlines are NOT containment — probe a real interior point
// (audit G2: the vertex mean lies outside concave rooms)
const c = interiorPoint(inner, eps);
return !!c && pointStrictlyInside(c, outer, eps) && polygonArea(inner) < polygonArea(outer) - eps;
}
/**