An open source design system that's fully customizable and agent ready
TypeScript
search: reuse the entry index while the catalog is unchanged (#7865)
Joeyf94e9622941e
diff --git a/.changeset/search-entry-cache.md b/.changeset/search-entry-cache.mdnew file mode 100644index 0000000000..6e8c97a914--- /dev/null+++ b/.changeset/search-entry-cache.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx search` reuses its entry index across searches in one process while the catalog is unchanged, so repeated searches answer faster; every answer is unchanged.++@josephfarinadiff --git a/packages/cli/api/search/_entries.mjs b/packages/cli/api/search/_entries.mjsindex 7a8d18f225..7e63f3da5e 100644--- a/packages/cli/api/search/_entries.mjs+++ b/packages/cli/api/search/_entries.mjs@@ -6,6 +6,7 @@* Built once per gathered catalog.*/+import {createHash} from 'node:crypto';import {DOMAINS, words, fold, nameWords} from './_query-words.mjs';/** @param {string[]} list @returns {Map<string, number>} */@@ -57,6 +58,62 @@ export const entryId = c =>export function entryTable(all, cards) {const hit = tables.get(all);if (hit) return hit;+ // Each search gathers a fresh candidate list, so the table is also kept by+ // the text it is built from: an unchanged catalog reuses it.+ const key = fingerprint(all, cards);+ if (last && last.key === key) {+ tables.set(all, last.table);+ return last.table;+ }+ const built = buildTable(all, cards);+ last = {key, table: built};+ tables.set(all, built);+ return built;+}++/** @type {{key: string, table: EntryTable} | null} */+let last = null;+/** @type {WeakMap<object, number>} */+const cardIds = new WeakMap();+let nextCardId = 0;++/**+ * Everything the table reads from the catalog, hashed.+ * @param {any[]} all+ * @param {object} cards+ * @returns {string}+ */+function fingerprint(all, cards) {+ if (!cardIds.has(cards)) cardIds.set(cards, nextCardId++);+ const h = createHash('sha1');+ h.update(String(cardIds.get(cards)));+ for (const c of all) {+ h.update(+ JSON.stringify([+ c.domain,+ c.name,+ c._topic,+ c._section,+ c._resultName,+ c._package,+ c.description,+ c.keywords,+ c.aliases,+ c.titles,+ (c.guidance ?? []).slice(0, 10),+ (c.prose ?? []).slice(0, 4),+ ]),+ );+ }+ return h.digest('hex');+}++/**+ * @param {any[]} all+ * @param {Record<string, {what?: string, synonyms?: string[], props?: string[]}>} cards+ * @returns {EntryTable}+ */+function buildTable(all, cards) {/** @type {Map<string, any[]>} */const by = new Map();/** @type {Map<string, string>} each id's first package */@@ -153,6 +210,5 @@ export function entryTable(all, cards) {idf.set(w, Math.log(1 + (n - d + 0.5) / (d + 0.5)));table.fields[f] = {post, idf, len, avg: len.reduce((a, b) => a + b, 0) / n};}- tables.set(all, table);return table;}