perf(apps): read the uid alias only after a direct lookup misses

Every AppDriver uid lookup fired an uncached system-KV GetItem for the
merge alias in parallel with the direct read, even when the direct read
hit. The alias is now read only on a miss.
This commit is contained in:
Daniel Salazar committed 2026-10-10 19:37:12 -04:00
1 parent 825e51715d
commit b0ef9f4130
2 files changed
+35 -4

No files matched your search

+2 -4
View File
@@ -862,14 +862,12 @@ export class AppDriver extends PuterDriver {
* keeps resolving to the joined row. Mirrors v1 AppES's `#read` alias
* plumbing.
*
* The alias query is fired in parallel with the direct lookup so the common
* (no-alias) case pays only one round-trip.
* The alias is an uncached system KV read, so it waits for a direct miss.
*/
async #getByUidWithAlias(uid) {
const aliasPromise = this.#readCanonicalAppUidAlias(uid);
const direct = await this.appStore.getByUid(uid);
if (direct) return direct;
const canonicalUid = await aliasPromise;
const canonicalUid = await this.#readCanonicalAppUidAlias(uid);
if (
typeof canonicalUid === 'string' &&
canonicalUid &&
@@ -371,6 +371,39 @@ describe('AppDriver.read', () => {
withActor(actor, () => driver.read({ uid: 'app-nonexistent' })),
).rejects.toMatchObject({ statusCode: 404 });
});
it('reads the uid alias only when the direct lookup misses', async () => {
const { actor } = await makeUser();
const created = await withActor(actor, () =>
driver.create({
object: {
name: uniqueName('alias-read'),
title: 't',
index_url: uniqueIndexUrl(),
},
}),
);
const kvGet = vi.spyOn(server.stores.kv, 'get');
const aliasReads = () =>
kvGet.mock.calls.filter((call) =>
String((call[0] as { key?: string })?.key).startsWith(
'app:canonicalUidAlias:',
),
).length;
try {
await withActor(actor, () => driver.read({ uid: created.uid }));
expect(aliasReads()).toBe(0);
await expect(
withActor(actor, () =>
driver.read({ uid: `app-${uuidv4()}` }),
),
).rejects.toMatchObject({ statusCode: 404 });
expect(aliasReads()).toBe(1);
} finally {
kvGet.mockRestore();
}
});
});
// ── select ──────────────────────────────────────────────────────────