perf(client): targeted shim account resolution (no full-scan on hot paths)
loadShim() read EVERY account record in the shim (SELECT over the whole anchor graph). On a shim that accumulates accounts (the shared wallet grows), that is O(accounts) and hangs the hot path (~90s at ensureAccount → loadShim). Same principle as per-doc reads: never scan a shared structure. Add resolveAccount(username): a BOUNDED SELECT anchored on the single subject accountSubject(username) → O(1), independent of account count. Cache in a per-account Map (cleared by resetRegistryCache). Hot paths now use it: ensureAccount (existence check), indexInboxNuri/@index (discovery), resolveInbox Anchor, resolveWriteGraph, createEntityDoc, listMyEntityDocs. loadShim kept only for genuine all-accounts needs (allAccounts, the listEntityDocs fan-out fallback). The 90s ensureAccount/loadShim hang is gone. 93 tests pass; tsc rc=0. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -86,11 +86,16 @@ function makeFakeNg() {
|
||||
const sparql_query = mock(async (...a: unknown[]) => {
|
||||
const query = a[1] as string;
|
||||
const anchor = a[3] as string | undefined;
|
||||
// Shim account SELECT.
|
||||
// Shim account SELECT. Two shapes: the full scan (`?acc a <Account>`) and
|
||||
// the TARGETED bounded resolve (`<subj> a <Account>`), which binds one
|
||||
// subject — honour that subject filter so the bounded query is O(1)/exact.
|
||||
if (query.includes(`<${SHIM}:username>`)) {
|
||||
const subjM = query.match(new RegExp(`GRAPH <[^>]+>\\s*\\{\\s*<([^>]+)>\\s+a\\s+<${SHIM}:Account>`));
|
||||
const onlySubject = subjM ? subjM[1]! : null;
|
||||
const bySubject = new Map<string, Record<string, string>>();
|
||||
for (const q of quads) {
|
||||
if (q.g !== anchor) continue;
|
||||
if (onlySubject !== null && q.s !== onlySubject) continue;
|
||||
const rec = bySubject.get(q.s) ?? {};
|
||||
if (q.p === `${SHIM}:username`) rec.username = q.o;
|
||||
if (q.p === `${SHIM}:docPublic`) rec.docPublic = q.o;
|
||||
|
||||
Reference in New Issue
Block a user