diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..7a62eab --- /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 2fd6de5..4dad85d 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; } diff --git a/src/services/locked-auth-storage.test.ts b/src/services/locked-auth-storage.test.ts index d000f26..50cd930 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(