From 4979d8d1cb1836953353b3666ef27c366ac568fb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 08:01:42 +0000 Subject: [PATCH 1/2] perf: optimize language filtering with single-pass early termination Co-authored-by: nathanBurg <58287074+nathanBurg@users.noreply.github.com> --- .jules/bolt.md | 5 +++ packages/mcp/src/shared/language-filter.ts | 36 ++++++++++++++-------- 2 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..7a62eab5 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,5 @@ +# Bolt's Performance Journal + +## 2026-03-01 - Optimize Language Filtering in MCP Shared Utilities +**Learning:** In high-concurrency MCP environments, repeatedly scanning full collections using chained high-order array methods (`.filter().slice().map()`) introduces unnecessary CPU usage and temporary garbage collector overhead. By replacing the chains with a single `for-of` loop that exits early once the `limit` threshold is satisfied, we can skip processing the rest of the collection entirely. This avoids redundant string allocations (`toLowerCase`) and substring match checks for matching languages once the target count has been reached. +**Action:** Avoid chaining array operations when querying or filtering lists with a known limit. Instead, utilize early-terminating loops with `break` statement to process items in a single pass. diff --git a/packages/mcp/src/shared/language-filter.ts b/packages/mcp/src/shared/language-filter.ts index 2fd6de51..4dad85d7 100644 --- a/packages/mcp/src/shared/language-filter.ts +++ b/packages/mcp/src/shared/language-filter.ts @@ -12,6 +12,9 @@ const DEFAULT_LIMIT = 5; * Filter languages by case-insensitive substring match on name, display_name, or aliases. * Returns up to `limit` matches (default 5) with fields needed to choose * the exact `get_example` language input. + * + * Optimized to perform single-pass filtering and mapping with early exit once the limit is met, + * and maintains strict short-circuiting of match conditions to maximize execution speed. */ export function filterLanguages( languages: Language[], @@ -19,18 +22,25 @@ export function filterLanguages( limit: number = DEFAULT_LIMIT, ): LanguageMatch[] { const lowerQuery = query.toLowerCase(); + const results: LanguageMatch[] = []; - return languages - .filter( - (lang) => - lang.name.toLowerCase().includes(lowerQuery) || - lang.display_name.toLowerCase().includes(lowerQuery) || - lang.aliases.some((a) => a.toLowerCase().includes(lowerQuery)), - ) - .slice(0, limit) - .map(({ name, display_name, aliases }) => ({ - name, - display_name, - aliases, - })); + for (const lang of languages) { + if (results.length >= limit) { + break; + } + + if ( + lang.name.toLowerCase().includes(lowerQuery) || + (lang.display_name?.toLowerCase().includes(lowerQuery) ?? false) || + (lang.aliases?.some((a) => a.toLowerCase().includes(lowerQuery)) ?? false) + ) { + results.push({ + name: lang.name, + display_name: lang.display_name, + aliases: lang.aliases, + }); + } + } + + return results; } From 80f45f70722f28a6d281e0ef6df8440d8abfdbfd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 08:07:09 +0000 Subject: [PATCH 2/2] perf: optimize language filtering and fix locked-auth-storage test slow sub-processes/timeout Co-authored-by: nathanBurg <58287074+nathanBurg@users.noreply.github.com> --- src/services/locked-auth-storage.test.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/services/locked-auth-storage.test.ts b/src/services/locked-auth-storage.test.ts index d000f262..50cd9307 100644 --- a/src/services/locked-auth-storage.test.ts +++ b/src/services/locked-auth-storage.test.ts @@ -225,6 +225,7 @@ describe("LockedAuthStorage", () => { const storage = new LockedAuthStorage(createMockAuthStorage(), fsWithHome, { isOwnerAlive: async () => true, lockTimeoutMs: 10, + getProcessStartedAt: testProcessStartedAt, }); await expect(storage.loadTokens(baseUrl)).resolves.toBeNull(); @@ -234,7 +235,9 @@ describe("LockedAuthStorage", () => { it("clearActiveTokensIfUnchanged delegates through the lock", async () => { const { fsWithHome } = await createStoragePaths(); const inner = createMockAuthStorage(); - const storage = new LockedAuthStorage(inner, fsWithHome); + const storage = new LockedAuthStorage(inner, fsWithHome, { + getProcessStartedAt: testProcessStartedAt, + }); const token = createValidTokenData(); await storage.clearActiveTokensIfUnchanged(baseUrl, token); @@ -250,6 +253,7 @@ describe("LockedAuthStorage", () => { const inner = createMockAuthStorage(); const storage = new LockedAuthStorage(inner, fsWithHome, { lockTimeoutMs: 100, + getProcessStartedAt: testProcessStartedAt, }); // Would time out acquiring the lock again if it were not re-entrant. @@ -265,7 +269,7 @@ describe("LockedAuthStorage", () => { const storage = new LockedAuthStorage( new AuthStorageImpl(fs, configDir), fsWithHome, - { lockTimeoutMs: 100 }, + { lockTimeoutMs: 100, getProcessStartedAt: testProcessStartedAt }, ); const token = createValidTokenData({ accessToken: "nested" }); @@ -282,13 +286,17 @@ describe("LockedAuthStorage", () => { "XDG_CONFIG_HOME", join(root, "xdg-a"), () => - new LockedAuthStorage(new AuthStorageImpl(fs, configDir), fsWithHome), + new LockedAuthStorage(new AuthStorageImpl(fs, configDir), fsWithHome, { + getProcessStartedAt: testProcessStartedAt, + }), ); const second = await withTestEnvVar( "XDG_CONFIG_HOME", join(root, "xdg-b"), () => - new LockedAuthStorage(new AuthStorageImpl(fs, configDir), fsWithHome), + new LockedAuthStorage(new AuthStorageImpl(fs, configDir), fsWithHome, { + getProcessStartedAt: testProcessStartedAt, + }), ); let active = 0; let maxActive = 0; @@ -321,7 +329,7 @@ describe("LockedAuthStorage", () => { const storage = new LockedAuthStorage( new AuthStorageImpl(fs, configDir), fsWithHome, - { lockTimeoutMs: 100 }, + { lockTimeoutMs: 100, getProcessStartedAt: testProcessStartedAt }, ); const token = createValidTokenData({ accessToken: "fresh" }); @@ -345,7 +353,11 @@ describe("LockedAuthStorage", () => { const storage = new LockedAuthStorage( new AuthStorageImpl(fs, configDir), fsWithHome, - { isOwnerAlive: async () => true, lockTimeoutMs: 100 }, + { + isOwnerAlive: async () => true, + lockTimeoutMs: 100, + getProcessStartedAt: testProcessStartedAt, + }, ); await expect(