Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(cli): share a running catalog gather between searches (#9460)

Cindy Zhangc117b0c7d188
diff --git a/.changeset/cli-search-shared-gather.md b/.changeset/cli-search-shared-gather.md
new file mode 100644
index 0000000000..ad205c3cb1
--- /dev/null
+++ b/.changeset/cli-search-shared-gather.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[perf] `search()` shares a catalog gather that is still running: searches that start together in one long-running process, such as a docs site's search right after it starts, now gather once instead of once each
+@cixzhang
diff --git a/packages/cli/api/search/_adapter.mjs b/packages/cli/api/search/_adapter.mjs
index 30aa50660f..61faf58512 100644
--- a/packages/cli/api/search/_adapter.mjs
+++ b/packages/cli/api/search/_adapter.mjs
@@ -9,7 +9,10 @@
import {readFileSync} from 'node:fs';
import * as path from 'node:path';
import {requireCoreDir} from '../../foundation/fs/paths.mjs';
-import {recordReads, unchangedSince} from '../../foundation/fs/read-record.mjs';
+import {
+ reusedWhileUnchanged,
+ unchangedSince,
+} from '../../foundation/fs/read-record.mjs';
import {isOutsideProject} from '../../foundation/env/project-scope.mjs';
import {coreDirOrThrow} from '../error.mjs';
import {CORELESS_DOMAINS} from './_query.mjs';
@@ -95,7 +98,7 @@ export async function gatherCandidates(options) {
/**
* @typedef {Awaited<ReturnType<typeof gatherCandidates>>} Gathered
- * @type {Map<string, {reads: Map<string, string>, value: Promise<Gathered>}>}
+ * @type {Map<string, (options?: {fresh?: boolean}) => Promise<Gathered>>}
*/
const gathered = new Map();
@@ -104,30 +107,27 @@ const gathered = new Map();
* folder, for the same `type`, ranks the candidates an earlier search
* gathered when none of the files that gather read, and none of the folders
* it listed or probed, changed since (each is checked by its change time and
- * size, so an edited doc in a linked package is seen on the next call). A
+ * size, so an edited doc in a linked package is seen on the next call). That
+ * holds for a gather that is still running too, so searches that start
+ * together (a docs site's search right after it starts) share one gather. A
* long-running caller (a docs site's search, an agent harness) then pays the
* gather once. A one-shot `astryx search` gathers once, as before.
* @param {{cwd?: string, type?: string}} options
* @returns {Promise<Gathered>}
*/
-export async function gatherCandidatesReused(options) {
+export function gatherCandidatesReused(options) {
const cwd = path.resolve(options.cwd ?? process.cwd());
- const key = `${cwd}\0${options.type ?? ''}`;
- const hit = gathered.get(key);
- if (hit && unchangedSince(hit.reads)) return hit.value;
- /** @type {Map<string, string>} */
- const seen = new Map();
- const value = recordReads(seen, () => gatherCandidates({...options, cwd}));
- try {
- await value;
- } catch (error) {
- if (gathered.get(key)?.value === value) gathered.delete(key);
- throw error;
+ const type = options.type;
+ const key = `${cwd}\0${type ?? ''}`;
+ let gather = gathered.get(key);
+ if (!gather) {
+ gather = reusedWhileUnchanged(
+ () => gatherCandidates({cwd, type}),
+ unchangedSince,
+ );
+ gathered.set(key, gather);
}
- // Each path's state was taken as it was first read, so an edit made while
- // the gather ran shows as a change on the next call.
- gathered.set(key, {reads: seen, value});
- return value;
+ return gather();
}
/**
diff --git a/packages/cli/api/search/_adapter.test.mjs b/packages/cli/api/search/_adapter.test.mjs
index d0d7960cb3..3e8d095a47 100644
--- a/packages/cli/api/search/_adapter.test.mjs
+++ b/packages/cli/api/search/_adapter.test.mjs
@@ -2,8 +2,9 @@
/**
* @file Tests for search's adapter: a long-running caller reuses the gathered
- * candidates while their inputs are unchanged (gatherCandidatesReused), and
- * gathers again when an input changes or for another folder or `type`.
+ * candidates while their inputs are unchanged (gatherCandidatesReused), shares
+ * a gather that is still running, and gathers again when an input changes or
+ * for another folder or `type`.
*
* Whether inputs changed is read-record's answer (tested in
* read-record.test.mjs). Other tests running at the same time change shared
@@ -52,6 +53,22 @@ describe('gatherCandidatesReused', () => {
SLOW,
);
+ it(
+ 'shares one gather between searches that start while it runs',
+ async () => {
+ const cwd = scratchProject();
+ const all = await Promise.all(
+ Array.from({length: 9}, () =>
+ gatherCandidatesReused({cwd, type: 'doc'}),
+ ),
+ );
+ // One gather: every search got the same candidates object.
+ for (const each of all) expect(each).toBe(all[0]);
+ expect(all[0].all.length).toBeGreaterThan(0);
+ },
+ SLOW,
+ );
+
it(
'gathers again when an input file changes',
async () => {
diff --git a/packages/cli/foundation/fs/read-record.mjs b/packages/cli/foundation/fs/read-record.mjs
index 0ef1f39c33..de4ee87de4 100644
--- a/packages/cli/foundation/fs/read-record.mjs
+++ b/packages/cli/foundation/fs/read-record.mjs
@@ -170,19 +170,23 @@ export function recordReads(seen, fn) {
* read counts as read by the operation that asks for it.
* @template T
* @param {() => Promise<T>} load
+ * @param {(recorded: Map<string, string>) => boolean} [isUnchanged] - Whether
+ * recorded paths are unchanged; `unchangedSince` unless a caller passes the
+ * one it imports (so a test of that caller can hold the answer).
* @returns {(options?: {fresh?: boolean}) => Promise<T>}
*/
-export function reusedWhileUnchanged(load) {
+export function reusedWhileUnchanged(load, isUnchanged = unchangedSince) {
/** @type {{reads: Map<string, string>, value: Promise<T>} | undefined} */
let last;
return ({fresh = false} = {}) => {
- if (!fresh && last && unchangedSince(last.reads)) {
+ if (!fresh && last && isUnchanged(last.reads)) {
const {reads: recorded, value} = last;
return value.then(result => {
const runs = reads.getStore();
- if (runs) for (const [file, state] of recorded) {
- for (const seen of runs) if (!seen.has(file)) seen.set(file, state);
- }
+ if (runs)
+ for (const [file, state] of recorded) {
+ for (const seen of runs) if (!seen.has(file)) seen.set(file, state);
+ }
return result;
});
}
diff --git a/packages/cli/foundation/fs/read-record.test.mjs b/packages/cli/foundation/fs/read-record.test.mjs
index f77566ce10..3ab2e7c2eb 100644
--- a/packages/cli/foundation/fs/read-record.test.mjs
+++ b/packages/cli/foundation/fs/read-record.test.mjs
@@ -151,6 +151,31 @@ describe('reusedWhileUnchanged', () => {
expect(unchangedSince(seen)).toBe(false);
});
+ it('shares a load that is still running, unless what it read has changed since', async () => {
+ const {file} = scratch();
+ let loads = 0;
+ /** @type {(() => void)[]} */
+ const finish = [];
+ const get = reusedWhileUnchanged(async () => {
+ loads += 1;
+ const text = fs.readFileSync(file, 'utf8');
+ await new Promise(resolve => finish.push(() => resolve(undefined)));
+ return text;
+ });
+ const running = get();
+ const joined = get();
+ expect(loads).toBe(1);
+ // An edit to a file the running load already read: a later call loads
+ // again instead of sharing the pre-edit value.
+ fs.writeFileSync(file, 'two, longer');
+ const afterEdit = get();
+ expect(loads).toBe(2);
+ for (const done of finish) done();
+ expect(await running).toBe('one');
+ expect(await joined).toBe('one');
+ expect(await afterEdit).toBe('two, longer');
+ });
+
it('tries a failed load again', async () => {
let calls = 0;
const get = reusedWhileUnchanged(async () => {