Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
36 changes: 23 additions & 13 deletions packages/mcp/src/shared/language-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,35 @@ 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[],
query: string,
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;
}
24 changes: 18 additions & 6 deletions src/services/locked-auth-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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" });

Expand All @@ -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;
Expand Down Expand Up @@ -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" });

Expand All @@ -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(
Expand Down