Files
CatoandClaude Opus 5 bdf9a2988b perf(engine): index the corpus, and give every entity a path to the KPI (#62)
Two halves of the same idea: the web is about to get much bigger, so the
lookups have to be cheap, and the reason for holding it at all has to be
visible from any node in it.

**Indexing.** Measured before touching anything: `allEntities()` cost 22ms per
call, and every lookup called it and then linear-scanned the result, so a single
entity lookup cost 19ms. At 268 entities that is invisible; at ten times that it
is fatal, because the cost is the rebuild multiplied by the scan and grows with
the square of the corpus while feeling fine right up until it does not.

The corpus is derived from static config and cannot change within a process, so
it is built once and served from maps: by id, by kind, and by name-or-alias.
Relations are materialised by endpoint, both directions together — which is also
what makes the single declaration safe, since the forward and inverse readings
are produced from the same relation and cannot diverge.

`allEntities()` x200: 4,422ms to 0ms. 13,400 entity lookups: 43ms, from what
would have been about four minutes. A test guards the structure (identity, not
equality, proves it is built once) rather than a stopwatch reading, with bounds
set to catch a return to linear scanning — orders of magnitude, not drift. A
second test asserts the fast path answers exactly what the slow one did.

**The KPI spine.** The site's model is loops: design, build, measure, turned as
fast as the slowest thing in the way allows. So the question worth asking of any
company, rule, mandate or country is not "is it important" but "which loop does
it hold up, and through what". That is a traversal — entity, to the bottlenecks
it is joined to, to the loops those gate — and it is affordable precisely
because of the indexing above.

"What this holds up" replaces the bottleneck-only "Which loops wait on it": the
traversal is identical for every kind, so it is one module rather than one per
kind. Each row carries the evidence of the join it travelled, because the
strength of "this firm holds up that loop" is the strength of its weakest hop.

What it must never do, and a test holds: severity is a dated judgement on a 0-12
scale, and judgements are not added together. A composite reads as a measurement
of the world and is not one, so a loop reports the WORST single judgement — the
test asserts that figure always equals one of its gates.


Claude-Session: https://claude.ai/code/session_01Y6ecRd91ZsPPcjuNkKTFV2

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 06:57:53 -07:00

86 lines
3.5 KiB
TypeScript

/**
* The corpus is going to get much larger, so the lookups have to be indexed.
*
* Measured before this guard existed: `allEntities()` cost 22ms per call and
* every lookup called it and then linear-scanned the result, so one entity
* lookup cost 19ms. That is invisible at 268 entities and fatal at ten times
* that — the cost is the rebuild multiplied by the scan, so it grows with the
* square of the corpus while feeling fine right up until it does not.
*
* These tests guard the structure rather than a stopwatch reading, because a
* timing assertion alone is flaky on a shared runner. The time bounds here are
* deliberately generous: they are set to catch a return to linear scanning,
* which is orders of magnitude, not a few percent of drift.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
allEntities,
entitiesOfKind,
indexStats,
resolveEntity,
resolveIn,
} from '../lib/entities/registry';
import { connectionsOf, relationStats } from '../lib/relations/registry';
test('the corpus is built once, not per call', () => {
// Identity, not equality: a fresh array each call means a fresh build.
assert.equal(allEntities(), allEntities(), 'allEntities() rebuilds the corpus every call');
assert.equal(entitiesOfKind('company'), entitiesOfKind('company'), 'kind lookup rebuilds');
});
test('an entity lookup does not scan the corpus', () => {
const ids = allEntities().map((e) => e.id);
assert.ok(ids.length > 100, 'corpus too small for this guard to mean anything');
const started = performance.now();
for (let round = 0; round < 50; round += 1) {
for (const id of ids) assert.ok(resolveEntity(id));
}
const elapsed = performance.now() - started;
// Linear scanning made this ~250,000ms. Indexed it is ~40ms.
assert.ok(elapsed < 4000, `${ids.length * 50} lookups took ${Math.round(elapsed)}ms`);
});
test('a connection lookup does not scan the whole web', () => {
const ids = allEntities().map((e) => e.id);
const started = performance.now();
for (let round = 0; round < 50; round += 1) {
for (const id of ids) connectionsOf(id);
}
const elapsed = performance.now() - started;
assert.ok(elapsed < 4000, `${ids.length * 50} connection lookups took ${Math.round(elapsed)}ms`);
});
test('indexing did not change what the registry answers', () => {
// The fast path has to agree with the slow one it replaced.
const all = allEntities();
for (const entity of all) {
assert.equal(resolveEntity(entity.id)?.id, entity.id, `${entity.id} lost by id`);
assert.equal(resolveIn(entity.kind, entity.key)?.id, entity.id, `${entity.id} lost by key`);
assert.equal(
resolveIn(entity.kind, entity.name.toUpperCase())?.id,
entity.id,
`${entity.id} lost by name`,
);
}
const counted = all.filter((e) => e.kind === 'company').length;
assert.equal(entitiesOfKind('company').length, counted, 'kind index disagrees with the corpus');
assert.equal(resolveEntity('company:not-a-real-company'), undefined);
});
test('the index reports what it holds', () => {
// A data engine that cannot say how much data it is holding is hard to trust
// and harder to operate.
const entities = indexStats();
const relations = relationStats();
assert.equal(entities.entities, allEntities().length);
assert.ok(entities.kinds > 0 && entities.labels >= entities.entities);
assert.ok(relations.relations > 0);
assert.ok(
relations.endpoints > 0 && relations.endpoints <= entities.entities,
'every endpoint should be an entity',
);
});