Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

perf: index label route clearance candidates

skyseeked274164123c
diff --git a/archify/renderers/shared/geometry.mjs b/archify/renderers/shared/geometry.mjs
index 733129c2..a5b2c979 100644
--- a/archify/renderers/shared/geometry.mjs
+++ b/archify/renderers/shared/geometry.mjs
@@ -104,8 +104,76 @@ export function segmentRectIntersectionLength(segment, rect) {
return length * Math.max(0, leave - enter);
}
+const ROUTE_INDEX_CELL_SIZE = 128;
+const ROUTE_INDEX_MAX_CELLS_PER_ROUTE = 256;
+
+function routeBounds(route) {
+ let x1 = Infinity;
+ let y1 = Infinity;
+ let x2 = -Infinity;
+ let y2 = -Infinity;
+ for (const [x, y] of route.points) {
+ x1 = Math.min(x1, x);
+ y1 = Math.min(y1, y);
+ x2 = Math.max(x2, x);
+ y2 = Math.max(y2, y);
+ }
+ return { x1, y1, x2, y2 };
+}
+
+function createRouteCandidateIndex(routes) {
+ const cells = new Map();
+ const globalRouteIndexes = [];
+
+ routes.forEach((route, routeIndex) => {
+ const bounds = routeBounds(route);
+ const x1 = Math.floor(bounds.x1 / ROUTE_INDEX_CELL_SIZE);
+ const y1 = Math.floor(bounds.y1 / ROUTE_INDEX_CELL_SIZE);
+ const x2 = Math.floor(bounds.x2 / ROUTE_INDEX_CELL_SIZE);
+ const y2 = Math.floor(bounds.y2 / ROUTE_INDEX_CELL_SIZE);
+ const cellCount = (x2 - x1 + 1) * (y2 - y1 + 1);
+
+ // Very long routes are cheaper to test once per label than to duplicate
+ // across a large part of the index.
+ if (!Number.isSafeInteger(cellCount) || cellCount > ROUTE_INDEX_MAX_CELLS_PER_ROUTE) {
+ globalRouteIndexes.push(routeIndex);
+ return;
+ }
+ for (let y = y1; y <= y2; y += 1) {
+ for (let x = x1; x <= x2; x += 1) {
+ const key = `${x}:${y}`;
+ const bucket = cells.get(key);
+ if (bucket) bucket.push(routeIndex);
+ else cells.set(key, [routeIndex]);
+ }
+ }
+ });
+
+ return function candidates(rect, threshold) {
+ const x1 = Math.floor((rect.x - threshold) / ROUTE_INDEX_CELL_SIZE);
+ const y1 = Math.floor((rect.y - threshold) / ROUTE_INDEX_CELL_SIZE);
+ const x2 = Math.floor((rect.x + rect.width + threshold) / ROUTE_INDEX_CELL_SIZE);
+ const y2 = Math.floor((rect.y + rect.height + threshold) / ROUTE_INDEX_CELL_SIZE);
+ const cellCount = (x2 - x1 + 1) * (y2 - y1 + 1);
+
+ // A huge threshold offers no selectivity and can overflow cell iteration.
+ if (!Number.isSafeInteger(cellCount) || cellCount > Math.max(64, cells.size * 4)) return routes;
+
+ const routeIndexes = new Set(globalRouteIndexes);
+ for (let y = y1; y <= y2; y += 1) {
+ for (let x = x1; x <= x2; x += 1) {
+ for (const routeIndex of cells.get(`${x}:${y}`) || []) routeIndexes.add(routeIndex);
+ }
+ }
+ return [...routeIndexes]
+ .sort((left, right) => left - right)
+ .map((routeIndex) => routes[routeIndex]);
+ };
+}
+
export function collectLabelRouteClearance({ labels, routedRelations, threshold }) {
if (!Number.isFinite(threshold) || threshold < 0) return [];
+ if (threshold === 0) return [];
const routeCandidates = asArray(routedRelations).map((entry, fallbackIndex) => {
const relation = entry?.relation || entry;
const points = normalizeRoutePoints(entry?.points || relation?.routePoints);
@@ -125,6 +193,7 @@ export function collectLabelRouteClearance({ labels, routedRelations, threshold
});
const hits = [];
const seenLabels = new Set();
+ const candidateRoutes = createRouteCandidateIndex(routes);
for (const [fallbackIndex, label] of asArray(labels).entries()) {
const rect = label?.rect || label;
@@ -133,7 +202,7 @@ export function collectLabelRouteClearance({ labels, routedRelations, threshold
const labelIdentity = relationshipIdentity(label?.relation, relationIndex);
if (seenLabels.has(labelIdentity)) continue;
seenLabels.add(labelIdentity);
- for (const route of routes) {
+ for (const route of candidateRoutes(rect, threshold)) {
if (relationIndex === route.relationIndex || sameRelationship(label?.relation, route.relation)) continue;
let nearest = null;
for (let segmentIndex = 0; segmentIndex < route.points.length - 1; segmentIndex += 1) {
diff --git a/archify/test/geometry.test.mjs b/archify/test/geometry.test.mjs
index 2b93f8c4..ecc1b67a 100644
--- a/archify/test/geometry.test.mjs
+++ b/archify/test/geometry.test.mjs
@@ -151,6 +151,58 @@ test('collectLabelRouteClearance exempts only the owning relationship at an exac
assert.equal(hits[0].otherRelation, sharedSource);
});
+test('collectLabelRouteClearance indexes sparse routes without changing hit order', () => {
+ const owner = { id: 'owner', from: 'owner-a', to: 'owner-b' };
+ const routes = Array.from({ length: 500 }, (_, index) => ({
+ relation: { id: `route-${index}`, from: `a-${index}`, to: `b-${index}` },
+ relationIndex: index + 1,
+ points: [[0, index * 256], [100, index * 256]],
+ }));
+ const nearFirst = { relation: routes[12].relation, relationIndex: 13, points: [[0, 101], [100, 101]] };
+ const nearSecond = { relation: routes[4].relation, relationIndex: 5, points: [[0, 103], [100, 103]] };
+ routes[12] = nearFirst;
+ routes[4] = nearSecond;
+ const labels = [{ relation: owner, relationIndex: 0, label: 'indexed', ...rect(20, 100, 20, 1) }];
+
+ const hits = collectLabelRouteClearance({ labels, routedRelations: routes, threshold: 4 });
+ assert.deepEqual(hits.map((hit) => hit.otherRelationIndex), [5, 13]);
+ assert.deepEqual(hits.map((hit) => hit.clearance), [2, 0]);
+});
+
+test('collectLabelRouteClearance keeps very long routes in the global candidate set', () => {
+ const owner = { id: 'owner', from: 'a', to: 'b' };
+ const long = { id: 'long', from: 'c', to: 'd' };
+ const hits = collectLabelRouteClearance({
+ labels: [{ relation: owner, relationIndex: 0, label: 'long route', ...rect(50000, 10, 20, 10) }],
+ routedRelations: [{ relation: long, relationIndex: 1, points: [[0, 15], [100000, 15]] }],
+ threshold: 4,
+ });
+ assert.equal(hits.length, 1);
+ assert.equal(hits[0].otherRelation, long);
+ assert.equal(hits[0].clearance, 0);
+});
+
+test('collectLabelRouteClearance stays bounded for thousands of sparse routes', () => {
+ const count = 3000;
+ const routedRelations = Array.from({ length: count }, (_, index) => ({
+ relation: { id: `route-${index}`, from: `a-${index}`, to: `b-${index}` },
+ relationIndex: index,
+ points: [[0, index * 20], [100, index * 20]],
+ }));
+ const labels = routedRelations.map((route, index) => ({
+ relation: route.relation,
+ relationIndex: index,
+ label: `label-${index}`,
+ ...rect(20, index * 20 - 2, 20, 4),
+ }));
+ const started = performance.now();
+ const hits = collectLabelRouteClearance({ labels, routedRelations, threshold: 2 });
+ const elapsed = performance.now() - started;
+
+ assert.deepEqual(hits, []);
+ assert.ok(elapsed < 1500, `expected sparse clearance scan under 1500ms, received ${elapsed.toFixed(1)}ms`);
+});
+
test('endpoint-side direction distinguishes perpendicular entry from a tangent border run', () => {
const clean = [[350, 160], [350, 200], [150, 200], [150, 240]];
const tangent = [[350, 160], [350, 200], [100, 200], [100, 240], [150, 240]];