Skip to content

Commit f261697

Browse files
author
jun
committed
perf(cursor): resolve the fast-id helper once per request, not per model
The listing branches called await import() inside the row mapper, so a request with N models paid N dynamic imports on a hot path. Hoist it to one resolution per request; when the switch is off the value is null and the adapter module is never loaded at all. Raised in review of #3233.
1 parent 13b007e commit f261697

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

‎src/server/index.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,15 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
15281528
inputModalities: nativeInputModalities(metadataId),
15291529
}),
15301530
});
1531+
// Resolved once per request, not per model: the global fast switch offers the fast
1532+
// identity to clients that have no Fast toggle of their own. Null when the switch is
1533+
// off, so the row mapper does no work and loads no adapter module.
1534+
const cursorFastIdForListing = config.fastMode === true
1535+
? await (async () => {
1536+
const { cursorFastIdFor } = await import("../adapters/cursor/catalog");
1537+
return (modelId: string, provider = "cursor") => provider === "cursor" ? cursorFastIdFor(modelId) : undefined;
1538+
})()
1539+
: null;
15311540
// Selector-active discovery follows the same complete supported set as the Codex catalog
15321541
// for both bare and qualified rows. Without selectors, the live catalog continues to own
15331542
// bare availability.
@@ -1555,9 +1564,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
15551564
// Same rule as the anthropic branch: with the global fast switch on, a client
15561565
// that has no Fast toggle is offered the fast identity directly. An operator
15571566
// alias is an explicit decision and still wins.
1558-
const fastModelId = config.fastMode === true && m.provider === "cursor"
1559-
? (await import("../adapters/cursor/catalog")).cursorFastIdFor(m.id)
1560-
: undefined;
1567+
const fastModelId = cursorFastIdForListing?.(m.id, m.provider);
15611568
const publicId = m.alias ?? `${m.provider}/${fastModelId ?? m.id}`;
15621569
const isCombo = m.provider === "combo" && exactComboSlugs.has(publicId);
15631570
const provider = config.providers[m.provider];

‎src/server/management/agent-settings-routes.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,19 +1041,19 @@ export async function handleAgentSettingsRoutes(ctx: ManagementContext): Promise
10411041
...models.filter(m => !isDisabled(m.provider, m.id)).map(m => `${m.provider}/${m.id}`),
10421042
];
10431043
const aliases: { id: string; display_name: string }[] = [];
1044+
// Resolved once, not per model: with the global fast switch on, Claude Code discovers the
1045+
// fast identity, so the dashboard must list the same id rather than the umbrella one.
1046+
const cursorFastIdFor = config.fastMode === true
1047+
? (await import("../../adapters/cursor/catalog")).cursorFastIdFor
1048+
: undefined;
10441049
for (const slug of listCatalogNativeSlugs()) {
10451050
// Readable CLI-surface alias with hash fallback (devlog 050 / audit 051 #2) —
10461051
// the same shared helper the /v1/models ?ids=cli path uses.
10471052
if (!disabled.has(slug)) aliases.push({ id: claudeCodeNativeAlias(slug), display_name: `${slug} (native)` });
10481053
}
10491054
for (const m of models) {
10501055
if (isDisabled(m.provider, m.id)) continue;
1051-
// Match what Claude Code will actually discover: with the global fast switch on,
1052-
// /v1/models lists the fast identity for a fast-capable cursor base, so the
1053-
// dashboard must show the same id rather than the umbrella one.
1054-
const listedId = config.fastMode === true && m.provider === "cursor"
1055-
? (await import("../../adapters/cursor/catalog")).cursorFastIdFor(m.id) ?? m.id
1056-
: m.id;
1056+
const listedId = (m.provider === "cursor" ? cursorFastIdFor?.(m.id) : undefined) ?? m.id;
10571057
aliases.push({ id: claudeCodeAlias(m.provider, listedId), display_name: `${listedId} (${m.provider})` });
10581058
}
10591059
const contextWindows = buildClaudeContextWindows([...visibleNativeSlugs(config)], models, nativeContextLimits(config));

0 commit comments

Comments
 (0)